<!--

    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.

-->
<section [formGroup]="createAlarmConfigForm" class="flex flex-col">
  <mat-checkbox formControlName="useMessageAlarmData">
    {{ 'rule-node-config.use-message-alarm-data' | translate }}
  </mat-checkbox>
  @if (createAlarmConfigForm.get('useMessageAlarmData').value === true) {
    <mat-checkbox formControlName="overwriteAlarmDetails">
      {{ 'rule-node-config.overwrite-alarm-details' | translate }}
    </mat-checkbox>
  }
  @if (createAlarmConfigForm.get('useMessageAlarmData').value === false ||
    createAlarmConfigForm.get('overwriteAlarmDetails').value === true) {
    <section class="flex flex-col">
      @if (tbelEnabled) {
        <tb-script-lang formControlName="scriptLang"></tb-script-lang>
      }
      @if (createAlarmConfigForm.get('scriptLang').value === scriptLanguage.JS) {
        <tb-js-func
          #jsFuncComponent
          formControlName="alarmDetailsBuildJs"
          functionName="Details"
          [functionArgs]="['msg', 'metadata', 'msgType']"
          helpId="rulenode/create_alarm_node_script_fn"
          [noValidate]="true">
          <button toolbarSuffixButton
                  mat-icon-button
                  matTooltip="{{ testScriptLabel | translate }}"
                  matTooltipPosition="above"
                  class="tb-mat-32"
                  (click)="testScript()">
            <mat-icon class="material-icons" color="primary">bug_report</mat-icon>
          </button>
        </tb-js-func>
      }
      @if (createAlarmConfigForm.get('scriptLang').value === scriptLanguage.TBEL) {
        <tb-js-func
          #tbelFuncComponent
          formControlName="alarmDetailsBuildTbel"
          functionName="Details"
          [functionArgs]="['msg', 'metadata', 'msgType']"
          [disableUndefinedCheck]="true"
          [scriptLanguage]="scriptLanguage.TBEL"
          helpId="rulenode/tbel/create_alarm_node_script_fn"
          [noValidate]="true">
          <button toolbarSuffixButton
                  mat-icon-button
                  matTooltip="{{ testScriptLabel | translate }}"
                  matTooltipPosition="above"
                  class="tb-mat-32"
                  (click)="testScript()">
            <mat-icon class="material-icons" color="primary">bug_report</mat-icon>
          </button>
        </tb-js-func>
      }
      <div class="flex flex-row" style="padding-bottom: 16px;">
        <button mat-button mat-raised-button color="primary" (click)="testScript()">
          {{ testScriptLabel | translate }}
        </button>
      </div>
    </section>
  }
  @if (createAlarmConfigForm.get('useMessageAlarmData').value === false) {
    <section class="flex flex-col">
      <mat-form-field class="flex-1" subscriptSizing="dynamic" appearance="outline">
        <mat-label translate>rule-node-config.alarm-type</mat-label>
        <input required matInput formControlName="alarmType">
        @if (createAlarmConfigForm.get('alarmType').hasError('required')) {
          <mat-error>
            {{ 'rule-node-config.alarm-type-required' | translate }}
          </mat-error>
        }
        <mat-hint translate>rule-node-config.general-pattern-hint</mat-hint>
      </mat-form-field>
      <mat-checkbox formControlName="dynamicSeverity">
        {{ 'rule-node-config.use-alarm-severity-pattern' | translate }}
      </mat-checkbox>
      @if (!createAlarmConfigForm.get('dynamicSeverity').value) {
        <mat-form-field class="flex-1" appearance="outline">
          <mat-label translate>rule-node-config.alarm-severity</mat-label>
          <mat-select formControlName="severity" required>
            @for (severity of alarmSeverities; track severity) {
              <mat-option [value]="severity">
                {{ alarmSeverityTranslationMap.get(severity) | translate }}
              </mat-option>
            }
          </mat-select>
          @if (createAlarmConfigForm.get('severity').hasError('required')) {
            <mat-error>
              {{ 'rule-node-config.alarm-severity-required' | translate }}
            </mat-error>
          }
        </mat-form-field>
      }
      @if (createAlarmConfigForm.get('dynamicSeverity').value) {
        <mat-form-field class="flex-1" subscriptSizing="dynamic" appearance="outline">
          <mat-label translate>rule-node-config.alarm-severity-pattern</mat-label>
          <input matInput formControlName="severity" required>
          @if (createAlarmConfigForm.get('severity').hasError('required')) {
            <mat-error>
              {{ 'rule-node-config.alarm-severity-required' | translate }}
            </mat-error>
          }
          <mat-hint [innerHTML]="'rule-node-config.alarm-severity-pattern-hint' | translate | safe: 'html'"></mat-hint>
        </mat-form-field>
      }
      <mat-checkbox formControlName="propagate">
        {{ 'rule-node-config.propagate' | translate }}
      </mat-checkbox>
      @if (createAlarmConfigForm.get('propagate').value === true) {
        <section>
          <mat-form-field floatLabel="always" class="mat-block" subscriptSizing="dynamic" appearance="outline">
            <mat-label translate>rule-node-config.relation-types-list</mat-label>
            <mat-chip-grid #relationTypesChipList>
              @for (key of createAlarmConfigForm.get('relationTypes').value; track key) {
                <mat-chip-row
                  (removed)="removeKey(key, 'relationTypes')">
                  {{key}}
                  <mat-icon matChipRemove>close</mat-icon>
                </mat-chip-row>
              }
              <input matInput type="text" placeholder="{{'rule-node-config.relation-types-list' | translate}}"
                     [matChipInputFor]="relationTypesChipList"
                     [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
                     (matChipInputTokenEnd)="addKey($event, 'relationTypes')"
                     [matChipInputAddOnBlur]="true">
            </mat-chip-grid>
            <mat-hint translate>rule-node-config.relation-types-list-hint</mat-hint>
          </mat-form-field>
        </section>
      }
      <mat-checkbox formControlName="propagateToOwner">
        {{ 'rule-node-config.propagate-to-owner' | translate }}
      </mat-checkbox>
      <mat-checkbox formControlName="propagateToTenant">
        {{ 'rule-node-config.propagate-to-tenant' | translate }}
      </mat-checkbox>
    </section>
  }
</section>
