<!--

    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 [formGroup]="fieldFormGroup" class="calculated-field-dialog-container flex flex-col">
  <mat-toolbar color="primary">
    <h2>{{ 'entity.type-calculated-field' | translate}}</h2>
    <span class="flex-1"></span>
    <div tb-help="calculatedField"></div>
    <button mat-icon-button
            [disabled]="isLoading"
            (click)="cancel()"
            type="button">
      <mat-icon class="material-icons">close</mat-icon>
    </button>
  </mat-toolbar>
  <div mat-dialog-content class="flex-1">
    <div class="tb-form-panel no-border no-padding">
      <div class="tb-form-panel no-gap">
        <div class="tb-form-panel-title mb-4">{{ 'common.general' | translate }}</div>
        <div class="flex items-center gap-2">
          <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
            <mat-label>{{ 'entity-field.title' | translate }}</mat-label>
            <input matInput maxlength="255" formControlName="name" required>
            @if (fieldFormGroup.get('name').errors && fieldFormGroup.get('name').touched) {
              <mat-error>
                @if (fieldFormGroup.get('name').hasError('required')) {
                  {{ 'common.hint.title-required' | translate }}
                } @else if (fieldFormGroup.get('name').hasError('maxlength')) {
                  {{ 'common.hint.title-max-length' | translate }}
                }
              </mat-error>
            }
            <mat-hint></mat-hint>
          </mat-form-field>
          <tb-entity-debug-settings-button
            formControlName="debugSettings"
            style="margin-bottom: 22px"
            [entityType]="EntityType.CALCULATED_FIELD"
            [additionalActionConfig]="additionalDebugActionConfig"
          />
        </div>
        @if (!data.entityId) {
          <tb-entity-select formControlName="entityId"
                            appearance="outline"
                            required
                            entityTypeLabel="{{ 'entity.entity-type' | translate }}"
                            [filterAllowedEntityTypes]="false"
                            [allowedEntityTypes]="calculatedFieldsEntityTypeList"
                            [defaultEntityType]="EntityType.DEVICE_PROFILE"
                            (entityChanged)="changeEntity($event)">
          </tb-entity-select>
        }
        <mat-form-field appearance="outline" subscriptSizing="dynamic">
          <mat-label>{{ 'common.type' | translate }}</mat-label>
          <mat-select formControlName="type">
            <mat-select-trigger>
              {{ CalculatedFieldTypeTranslations.has(fieldFormGroup.get('type').value)
                  ? (CalculatedFieldTypeTranslations.get(fieldFormGroup.get('type').value).name | translate)
                  : fieldFormGroup.get('type').value }}
            </mat-select-trigger>
            @for (type of fieldTypes; track type) {
              <mat-option [value]="type" class="py-1">
                <span matListItemTitle>
                  {{ CalculatedFieldTypeTranslations.get(type).name | translate}}
                </span>
                <br>
                <small matListItemMeta style="color: inherit">
                  {{ CalculatedFieldTypeTranslations.get(type).hint | translate }}
                </small>
              </mat-option>
            }
          </mat-select>
        </mat-form-field>
      </div>
      @switch (fieldFormGroup.get('type').value) {
        @case (CalculatedFieldType.GEOFENCING) {
          <tb-geofencing-configuration formControlName="configuration"
                                       [class.tb-form-panel-disabled]="disabledConfiguration"
                                       [entityId]="entityId"
                                       [entityName]="entityName"
                                       [ownerId]="ownerId"
                                       [tenantId]="data.tenantId"
          ></tb-geofencing-configuration>
        }
        @case (CalculatedFieldType.PROPAGATION) {
          <tb-propagation-configuration formControlName="configuration"
                                        [class.tb-form-panel-disabled]="disabledConfiguration"
                                        [entityId]="entityId"
                                        [entityName]="entityName"
                                        [tenantId]="data.tenantId"
                                        [ownerId]="ownerId"
                                        [testScript]="onTestScript.bind(this)"
          ></tb-propagation-configuration>
        }
        @case (CalculatedFieldType.RELATED_ENTITIES_AGGREGATION) {
          <tb-related-entities-aggregation-component formControlName="configuration"
                                                     [class.tb-form-panel-disabled]="disabledConfiguration"
                                                     [entityId]="entityId"
                                                     [entityName]="entityName"
                                                     [tenantId]="data.tenantId"
                                                     [testScript]="onTestScript.bind(this)"
          ></tb-related-entities-aggregation-component>
        }
        @case (CalculatedFieldType.ENTITY_AGGREGATION) {
          <tb-entity-aggregation-component formControlName="configuration"
                                           [class.tb-form-panel-disabled]="disabledConfiguration"
                                           [entityId]="entityId"
                                           [entityName]="entityName"
                                           [tenantId]="data.tenantId"
          ></tb-entity-aggregation-component>
        }
        @default {
          <tb-simple-configuration formControlName="configuration"
                                   [class.tb-form-panel-disabled]="disabledConfiguration"
                                   [entityId]="entityId"
                                   [entityName]="entityName"
                                   [ownerId]="ownerId"
                                   [tenantId]="data.tenantId"
                                   [isScript]="fieldFormGroup.get('type').value === CalculatedFieldType.SCRIPT"
                                   [testScript]="onTestScript.bind(this)"
          ></tb-simple-configuration>
        }
      }
    </div>
  </div>
  <div mat-dialog-actions class="justify-end">
    <button mat-button color="primary"
            type="button"
            cdkFocusInitial
            [disabled]="isLoading"
            (click)="cancel()">
      {{ 'action.cancel' | translate }}
    </button>
    <button mat-raised-button color="primary"
            (click)="add()"
            [disabled]="isLoading || fieldFormGroup.invalid || !fieldFormGroup.dirty">
      {{ data.buttonTitle | translate }}
    </button>
  </div>
</div>
