<!--

    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.

-->
<div class="tb-details-buttons">
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'open')"
          [class.!hidden]="isEdit">
    {{'rulechain.open-rulechain' | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'export')"
          [class.!hidden]="isEdit">
    {{'rulechain.export' | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'setRoot')"
          [class.!hidden]="isEdit || !((!entity?.root && ruleChainScope === 'tenant') || (!isEdgeRootRuleChain() && ruleChainScope === 'edge'))">
    {{'rulechain.set-root' | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'setEdgeTemplateRoot')"
          [class.!hidden]="isEdit || entity?.root || ruleChainScope !== 'edges'">
    {{'rulechain.set-edge-template-root-rulechain' | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'setAutoAssignToEdge')"
          [class.!hidden]="isEdit || !isNotAutoAssignToEdgeRuleChain() || ruleChainScope !== 'edges'">
    {{'rulechain.set-auto-assign-to-edge' | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'unsetAutoAssignToEdge')"
          [class.!hidden]="isEdit || !isAutoAssignToEdgeRuleChain() || ruleChainScope !== 'edges'">
    {{'rulechain.unset-auto-assign-to-edge' | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'unassignFromEdge')"
          [class.!hidden]="isEdit || isEdgeRootRuleChain() || ruleChainScope !== 'edges'">
    {{'edge.unassign-from-edge' | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'delete')"
          [class.!hidden]="hideDelete() || isEdit || ruleChainScope === 'edge'">
    {{'rulechain.delete' | translate }}
  </button>
  <div class="flex flex-row">
    <button mat-raised-button
            ngxClipboard
            (cbOnSuccess)="onRuleChainIdCopied($event)"
            [cbContent]="entity?.id?.id"
            [class.!hidden]="isEdit">
      <mat-icon svgIcon="mdi:clipboard-arrow-left"></mat-icon>
      <span translate>rulechain.copyId</span>
    </button>
  </div>
</div>
<div class="mat-padding flex flex-col">
  <form [formGroup]="entityForm">
    <fieldset [disabled]="(isLoading$ | async) || !isEdit" style="min-width: 0;">
      <mat-form-field class="mat-block" appearance="outline">
        <mat-label translate>rulechain.name</mat-label>
        <input matInput formControlName="name" required>
        @if (entityForm.get('name').hasError('required')) {
          <mat-error>{{ 'rulechain.name-required' | translate }}</mat-error>
        }
        @if (entityForm.get('name').hasError('maxlength')) {
          <mat-error>{{ 'rulechain.name-max-length' | translate }}</mat-error>
        }
      </mat-form-field>
      <mat-checkbox class="flex-1" formControlName="debugMode" style="padding-bottom: 16px;">
        {{ 'rulechain.debug-mode' | translate }}
      </mat-checkbox>
      <div formGroupName="additionalInfo">
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>rulechain.description</mat-label>
          <textarea matInput formControlName="description" cdkTextareaAutosize cdkAutosizeMinRows="1" rows="1"></textarea>
        </mat-form-field>
      </div>
    </fieldset>
  </form>
</div>
