<!--

    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]="simpleConfiguration" class="tb-form-panel no-border no-padding">
  <div class="tb-form-panel">
    <div class="tb-form-panel-title tb-required"
         tb-hint-tooltip-icon="{{ (isScript ? 'calculated-fields.hint.script-arguments' : 'calculated-fields.hint.simple-arguments') | translate }}">
      {{ 'calculated-fields.arguments' | translate }}
    </div>
    <tb-calculated-field-arguments-table formControlName="arguments"
                                         [entityId]="entityId"
                                         [tenantId]="tenantId"
                                         [ownerId]="ownerId"
                                         [entityName]="entityName"
                                         [isEditValue]="isEditValue"
                                         [watchKeyChange]="true"
                                         [isScript]="isScript" />
  </div>
  <div class="tb-form-panel no-gap">
    <div class="tb-form-panel-title tb-required"
         tb-hint-tooltip-icon="{{ (isScript ? 'calculated-fields.hint.script-title' : 'calculated-fields.hint.simple-expression-title') | translate }}">
      {{ (isScript ? 'calculated-fields.type.script' : 'calculated-fields.expression') | translate }}
    </div>
    <mat-form-field class="mt-3" appearance="outline" subscriptSizing="dynamic" [class.hidden]="isScript">
      <input matInput formControlName="expressionSIMPLE" maxlength="255" [placeholder]="'(temperature - 32) / 1.8'"
             required>
      <div matSuffix
           class="pr-2"
           [tb-help-popup]="'math/math-methods_fn'"
           tb-help-popup-placement="left"
           [tb-help-popup-style]="{maxWidth: '970px'}">
      </div>
      @if (simpleConfiguration.get('expressionSIMPLE').errors && simpleConfiguration.get('expressionSIMPLE').touched) {
        <mat-error>
          @if (simpleConfiguration.get('expressionSIMPLE').hasError('required')) {
            {{ 'calculated-fields.hint.expression-required' | translate }}
          } @else if (simpleConfiguration.get('expressionSIMPLE').hasError('pattern')) {
            {{ 'calculated-fields.hint.expression-invalid' | translate }}
          } @else if (simpleConfiguration.get('expressionSIMPLE').hasError('maxLength')) {
            {{ 'calculated-fields.hint.expression-max-length' | translate }}
          }
        </mat-error>
      } @else {
        <mat-hint>{{ 'calculated-fields.hint.expression' | translate }}</mat-hint>
      }
    </mat-form-field>
    <div [class.hidden]="!isScript">
      <tb-js-func required
                  formControlName="expressionSCRIPT"
                  functionName="calculate"
                  [functionArgs]="functionArgs$ | async"
                  [disableUndefinedCheck]="true"
                  [scriptLanguage]="ScriptLanguage.TBEL"
                  [highlightRules]="argumentsHighlightRules$ | async"
                  [editorCompleter]="argumentsEditorCompleter$ | async"
                  [helpPopupStyle]="{ width: '1200px' }"
                  helpId="calculated-field/expression_fn">
        <div toolbarPrefixButton
             class="tb-primary-background tbel-script-lang-chip">{{ 'api-usage.tbel' | translate }}
        </div>
        <button toolbarSuffixButton
                mat-icon-button
                matTooltip="{{ 'calculated-fields.test-script-function' | translate }}"
                matTooltipPosition="above"
                class="tb-mat-32"
                [disabled]="simpleConfiguration.get('arguments').invalid || disabled"
                (click)="onTestScript()">
          <mat-icon class="material-icons" color="primary">bug_report</mat-icon>
        </button>
      </tb-js-func>
      <div>
        <button mat-button mat-raised-button color="primary"
                type="button"
                (click)="onTestScript()"
                [disabled]="simpleConfiguration.get('arguments').invalid || disabled">
          {{ 'calculated-fields.test-script-function' | translate }}
        </button>
      </div>
    </div>
  </div>
  <tb-calculate-field-output formControlName="output" [simpleMode]="!isScript" [entityId]="entityId">
    <div class="tb-form-row simpleMode"
         [class.!hidden]="simpleConfiguration.get('output').value.type !== OutputType.Timeseries">
      <mat-slide-toggle class="mat-slide" formControlName="useLatestTs">
        <div tb-hint-tooltip-icon="{{ 'calculated-fields.hint.use-latest-timestamp' | translate }}" translate>
          calculated-fields.use-latest-timestamp
        </div>
      </mat-slide-toggle>
    </div>
  </tb-calculate-field-output>
</div>
