<!--

    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-config-panel" [formGroup]="argumentFormGroup">
  <div class="tb-config-panel-title tb-form-panel-title">{{ 'calculated-fields.argument-settings' | translate }}</div>
  <div class="tb-config-panel-content tb-form-panel no-border no-padding">
    @if (hint) {
      <div class="tb-form-hint tb-primary-fill hint-container">
        {{ hint | translate }}
      </div>
    }
    <div class="tb-form-panel no-border no-padding">
      @if (!watchKeyChange) {
        <ng-container *ngTemplateOutlet="argumentNameTemplate; context: argumentNameContext"></ng-container>
      }
      <ng-container>
        @if (!hiddenEntityTypes) {
          <div class="tb-form-row">
            <div class="fixed-title-width">{{ 'entity.entity-type' | translate }}</div>
            <mat-form-field class="tb-flex no-gap" appearance="outline" subscriptSizing="dynamic">
              <mat-select [formControl]="argumentType">
                @for (type of argumentEntityTypes; track type) {
                  <mat-option [value]="type">{{ ArgumentEntityTypeTranslations.get(type) | translate }}</mat-option>
                }
              </mat-select>
              @if (argumentType.touched && argumentType.hasError('required')) {
                <mat-icon matSuffix
                          matTooltipPosition="above"
                          matTooltipClass="tb-error-tooltip"
                          [matTooltip]="'calculated-fields.hint.entity-type-required' | translate"
                          class="tb-error">
                  warning
                </mat-icon>
              }
            </mat-form-field>
          </div>
        }
        @if (ArgumentEntityTypeParamsMap.has(entityType)) {
          <div class="tb-form-row">
            <div class="fixed-title-width tb-required">{{ ArgumentEntityTypeParamsMap.get(entityType).title | translate }}</div>
            <tb-entity-autocomplete
              class="flex flex-1"
              #entityAutocomplete
              formControlName="refEntityId"
              useFullEntityId
              inlineField
              [placeholder]="'action.set' | translate"
              [required]="true"
              [entityType]="ArgumentEntityTypeParamsMap.get(entityType).entityType"
              (entityChanged)="entityNameSubject.next($event?.name)"
            />
          </div>
        }
      </ng-container>
      <ng-container [formGroup]="refEntityKeyFormGroup">
        @if (!hiddenEntityKeyTypes) {
          <div class="tb-form-row">
            <div class="fixed-title-width tb-required">{{ 'calculated-fields.argument-type' | translate }}</div>
            <mat-form-field class="tb-flex no-gap" appearance="outline" subscriptSizing="dynamic">
              <mat-select formControlName="type">
                @for (type of argumentTypes; track type) {
                  <mat-option [value]="type">{{ ArgumentTypeTranslations.get(type) | translate }}</mat-option>
                }
              </mat-select>
              @if (refEntityKeyFormGroup.get('type').hasError('required') && refEntityKeyFormGroup.get('type').touched) {
                <mat-icon matSuffix
                          matTooltipPosition="above"
                          matTooltipClass="tb-error-tooltip"
                          [matTooltip]="'calculated-fields.hint.argument-type-required' | translate"
                          class="tb-error">
                  warning
                </mat-icon>
              }
            </mat-form-field>
          </div>
        }
        @if (entityFilter.singleEntity?.id || entityType === ArgumentEntityType.Current || entityType === ArgumentEntityType.Tenant) {
          @if (refEntityKeyFormGroup.get('type').value !== ArgumentType.Attribute) {
            <div class="tb-form-row">
              <div class="fixed-title-width tb-required">{{ 'calculated-fields.timeseries-key' | translate }}</div>
              @if (refEntityKeyFormGroup.get('type').value === ArgumentType.LatestTelemetry) {
                <ng-container [ngTemplateOutlet]="timeseriesKeyAutocomplete"/>
              } @else {
                <ng-container [ngTemplateOutlet]="timeseriesKeyAutocomplete"/>
              }
              <ng-template #timeseriesKeyAutocomplete>
                <tb-entity-key-autocomplete class="flex-1" formControlName="key" [dataKeyType]="DataKeyType.timeseries" [entityFilter]="entityFilter" [enableAutocomplete]="!enableAutocomplete"/>
              </ng-template>
            </div>
          } @else {
            @if (enableAttributeScopeSelection) {
              <div class="tb-form-row">
                <div class="fixed-title-width tb-required">{{ 'calculated-fields.attribute-scope' | translate }}</div>
                <mat-form-field appearance="outline" subscriptSizing="dynamic" class="flex-1">
                  <mat-select formControlName="scope">
                    <mat-option [value]="AttributeScope.SERVER_SCOPE">
                      {{ 'calculated-fields.server-attributes' | translate }}
                    </mat-option>
                    <mat-option [value]="AttributeScope.CLIENT_SCOPE">
                      {{ 'calculated-fields.client-attributes' | translate }}
                    </mat-option>
                    <mat-option [value]="AttributeScope.SHARED_SCOPE">
                      {{ 'calculated-fields.shared-attributes' | translate }}
                    </mat-option>
                  </mat-select>
                </mat-form-field>
              </div>
            }
            <div class="tb-form-row">
              <div class="fixed-title-width tb-required">{{ 'calculated-fields.attribute-key' | translate }}</div>
              <tb-entity-key-autocomplete
                formControlName="key"
                class="flex-1"
                [enableAutocomplete]="!enableAutocomplete"
                [dataKeyType]="DataKeyType.attribute"
                [entityFilter]="entityFilter"
                [keyScopeType]="argumentFormGroup.get('refEntityKey').get('scope').value"
              />
            </div>
          }
        }
      </ng-container>
      @if (watchKeyChange) {
        <ng-container *ngTemplateOutlet="argumentNameTemplate; context: argumentNameContext"></ng-container>
      }
      @if (refEntityKeyFormGroup.get('type').value !== ArgumentType.Rolling) {
        @if (!hiddenDefaultValue) {
          <div class="tb-form-row">
            <div class="fixed-title-width" [class.tb-required]="defaultValueRequired">{{ 'calculated-fields.default-value' | translate }}</div>
            <mat-form-field class="flex-1 overflow-hidden" appearance="outline" subscriptSizing="dynamic">
              <input matInput autocomplete="off" name="value" formControlName="defaultValue" placeholder="{{ 'action.set' | translate }}"/>
              @if (argumentFormGroup.get('defaultValue').touched && argumentFormGroup.get('defaultValue').hasError('required')) {
                <mat-icon matSuffix
                          matTooltipPosition="above"
                          matTooltipClass="tb-error-tooltip"
                          [matTooltip]="'calculated-fields.default-value-required' | translate"
                          class="tb-error">
                  warning
                </mat-icon>
              }
            </mat-form-field>
          </div>
        }
      } @else {
        <div class="tb-form-row">
          <div class="fixed-title-width tb-required">{{ 'calculated-fields.time-window' | translate }}</div>
          <tb-timeinterval
            subscriptSizing="dynamic"
            appearance="outline"
            class="time-interval-field flex-1"
            formControlName="timeWindow"
          />
        </div>
        @if (maxDataPointsPerRollingArg) {
          <div class="tb-form-row limit-field-row">
            <div class="fixed-title-width tb-required">{{ 'calculated-fields.limit' | translate }}</div>
            <div class="limit-slider-container flex w-full flex-1 flex-row items-center justify-start">
              <mat-slider class="flex-1" min="1" max="{{maxDataPointsPerRollingArg}}">
                <input matSliderThumb formControlName="limit" [value]="argumentFormGroup.get('limit').value"/>
              </mat-slider>
              <mat-form-field class="limit-slider-value" subscriptSizing="dynamic" appearance="outline">
                <input matInput formControlName="limit" type="number" step="1" [value]="argumentFormGroup.get('limit').value" min="1" max="{{maxDataPointsPerRollingArg}}"/>
              </mat-form-field>
            </div>
          </div>
        }
      }
    </div>
  </div>
  <div class="tb-config-panel-buttons">
    <button mat-button
            color="primary"
            type="button"
            (click)="cancel()">
      {{ 'action.cancel' | translate }}
    </button>
    @if (!readonly) {
      <button mat-raised-button
              color="primary"
              type="button"
              (click)="saveArgument()"
              [disabled]="argumentFormGroup.invalid || !argumentFormGroup.dirty">
        {{ buttonTitle | translate }}
      </button>
    }
  </div>
</div>

<ng-template #argumentNameTemplate let-label="label" let-required="required" let-duplicate="duplicate"
  let-pattern="pattern" let-maxlength="maxlength" let-forbidden="forbidden">
  <div class="tb-form-row" [formGroup]="argumentFormGroup">
    <div class="fixed-title-width tb-required">{{ label | translate }}</div>
    <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
      <input matInput autocomplete="off" name="value" formControlName="argumentName" maxlength="255" placeholder="{{ 'action.set' | translate }}"/>
      @if (argumentFormGroup.get('argumentName').touched && argumentFormGroup.get('argumentName').hasError('required')) {
        <mat-icon matSuffix
                  matTooltipPosition="above"
                  matTooltipClass="tb-error-tooltip"
                  [matTooltip]="required | translate"
                  class="tb-error">
          warning
        </mat-icon>
      } @else if (argumentFormGroup.get('argumentName').touched && argumentFormGroup.get('argumentName').hasError('duplicateName')) {
        <mat-icon matSuffix
                  matTooltipPosition="above"
                  matTooltipClass="tb-error-tooltip"
                  [matTooltip]="duplicate | translate"
                  class="tb-error">
          warning
        </mat-icon>
      } @else if (argumentFormGroup.get('argumentName').touched && argumentFormGroup.get('argumentName').hasError('pattern')) {
        <mat-icon matSuffix
                  matTooltipPosition="above"
                  matTooltipClass="tb-error-tooltip"
                  [matTooltip]="pattern | translate"
                  class="tb-error">
          warning
        </mat-icon>
      } @else if (argumentFormGroup.get('argumentName').touched && argumentFormGroup.get('argumentName').hasError('maxlength')) {
        <mat-icon matSuffix
                  matTooltipPosition="above"
                  matTooltipClass="tb-error-tooltip"
                  [matTooltip]="maxlength | translate"
                  class="tb-error">
          warning
        </mat-icon>
      } @else if (argumentFormGroup.get('argumentName').touched && argumentFormGroup.get('argumentName').hasError('forbiddenName')) {
        <mat-icon matSuffix
                  matTooltipPosition="above"
                  matTooltipClass="tb-error-tooltip"
                  [matTooltip]="forbidden | translate"
                  class="tb-error">
          warning
        </mat-icon>
      }
    </mat-form-field>
  </div>
</ng-template>
