<!--

    Copyright © 2016-2026 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
@if (!isAdd && ruleNode.component?.configurationDescriptor?.nodeDefinition?.ruleChainNode) {
  <div class="tb-details-buttons">
    <button mat-raised-button color="primary"
            (click)="openRuleChain($event)">
      {{'rulechain.open-rulechain' | translate }}
    </button>
  </div>
}
<form [formGroup]="ruleNodeFormGroup" class="mat-padding">
  <section class="title-row align-center flex">
    <mat-form-field class="mat-block flex-1" appearance="outline">
      <mat-label translate>rulenode.name</mat-label>
      <input matInput formControlName="name" required>
      @if (ruleNodeFormGroup.get('name').hasError('required') || ruleNodeFormGroup.get('name').hasError('pattern')) {
        <mat-error>
          {{ 'rulenode.name-required' | translate }}
        </mat-error>
      }
      @if (ruleNodeFormGroup.get('name').hasError('maxlength')) {
        <mat-error>
          {{ 'rulenode.name-max-length' | translate }}
        </mat-error>
      }
    </mat-form-field>
    <section class="mb-5 flex max-w-xs flex-row">
      <tb-entity-debug-settings-button
        [additionalActionConfig]="additionalActionConfig"
        class="mr-2"
        formControlName="debugSettings"
        [entityType]="entityType.RULE_NODE"/>
      @if (isSingleton()) {
        <button mat-stroked-button
                class="tb-rounded-btn flex-1"
                color="primary"
                [disabled]="ruleNodeFormGroup.get('singletonMode').disabled || disabled"
                [class.active]="ruleNodeFormGroup.get('singletonMode').value"
                (click)="onSingleModeChange($event)">
          <mat-icon [class.invisible]="!ruleNodeFormGroup.get('singletonMode').value">checkmark</mat-icon>
          <span translate>rulenode.singleton</span>
          <input class="hidden" formControlName="singletonMode">
        </button>
      }
    </section>
  </section>
  <tb-rule-node-config #ruleNodeConfigComponent
                       formControlName="configuration"
                       [ruleNodeId]="ruleNode.ruleNodeId?.id"
                       [ruleChainId]="ruleChainId"
                       [ruleChainType]="ruleChainType"
                       [nodeDefinition]="ruleNode.component.configurationDescriptor.nodeDefinition"
                       (initRuleNode)="initRuleNode.emit($event)"
                       (changeScript)="changeScript.emit($event)">
  </tb-rule-node-config>

  <div class="description-block">
    @if (isAddQueue()) {
      <tb-queue-autocomplete
        [queueType]="serviceType"
        appearance="outline"
        subscriptSizing="dynamic"
        [autocompleteHint]="isSingleton() ? 'rulenode.queue-singleton-hint' : 'rulenode.queue-hint'"
        formControlName="queueName">
      </tb-queue-autocomplete>
    }
    <div formGroupName="additionalInfo">
      <mat-form-field class="mat-block" appearance="outline">
        <mat-label translate>rulenode.rule-node-description</mat-label>
        <textarea matInput formControlName="description" cdkTextareaAutosize cdkAutosizeMinRows="1" rows="1"></textarea>
      </mat-form-field>
    </div>
  </div>
</form>
