<!--

    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]="mathFunctionConfigForm" class="flex flex-col">
  <tb-math-function-autocomplete
    class="flex-full max-h-30% xs:max-h-full md:max-h-full"
    required
    appearance="outline"
    formControlName="operation">
  </tb-math-function-autocomplete>
  <fieldset class="fields-group flex flex-col gap-2">
    <legend class="group-title" translate>rule-node-config.argument-tile</legend>
    <tb-arguments-map-config formControlName="arguments"
                             [function]="mathFunctionConfigForm.get('operation').value">
    </tb-arguments-map-config>
  </fieldset>
  @if (mathFunctionConfigForm.get('operation').value === MathFunction.CUSTOM) {
    <fieldset
      class="fields-group flex flex-col gap-2">
      <legend class="group-title">{{'rule-node-config.custom-expression-field-input' | translate }} *</legend>
      <mat-form-field class="mat-block no-margin-top flex-1" subscriptSizing="dynamic" appearance="outline">
        <input matInput formControlName="customFunction" 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 (mathFunctionConfigForm.get('customFunction').hasError('required')) {
          <mat-error translate>
            rule-node-config.custom-expression-field-input-required
          </mat-error>
        }
        <mat-hint translate>rule-node-config.custom-expression-field-input-hint</mat-hint>
      </mat-form-field>
    </fieldset>
  }
  <fieldset class="fields-group flex flex-col gap-2">
    <legend class="group-title" translate>rule-node-config.result-title</legend>
    <div formGroupName="result">
      <mat-form-field class="mat-block flex-1" appearance="outline">
        <mat-label translate>rule-node-config.type-field-input</mat-label>
        <mat-select formControlName="type" required>
          <mat-select-trigger>
            {{ argumentTypeResultMap.get(mathFunctionConfigForm.get('result.type').value)?.name | translate }}
          </mat-select-trigger>
          @for (argument of argumentsResult; track argument) {
            <mat-option [value]="argument"
                        style="border-bottom: 1px solid #eee;">
              {{ argumentTypeResultMap.get(argument).name | translate }}
              <small style="display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">
                {{ argumentTypeResultMap.get(argument).description | translate }}
              </small>
            </mat-option>
          }
        </mat-select>
        @if (mathFunctionConfigForm.get('result.type').hasError('required')) {
          <mat-error translate>
            rule-node-config.type-field-input-required
          </mat-error>
        }
      </mat-form-field>
      <div class="xs:flex-col gt-xs:gap-4 flex flex-1 flex-row">
        @if (mathFunctionConfigForm.get('result').get('type').value === ArgumentTypeResult.ATTRIBUTE) {
          <mat-form-field class="mat-block flex-1" appearance="outline">
            <mat-label translate>rule-node-config.attribute-scope-field-input</mat-label>
            <mat-select required formControlName="attributeScope">
              @for (scope of attributeScopeResult; track scope) {
                <mat-option [value]="scope">
                  {{ attributeScopeMap.get(scope) | translate }}
                </mat-option>
              }
            </mat-select>
          </mat-form-field>
        }
        <mat-form-field floatLabel="always" class="mat-block flex-1" appearance="outline">
          <mat-label translate>rule-node-config.key-field-input</mat-label>
          <input matInput formControlName="key" required/>
          <mat-icon class="help-icon m-2 cursor-pointer" aria-hidden="false" aria-label="help-icon" matSuffix
                    color="primary"
          matTooltip="{{ 'rule-node-config.math-templatization-tooltip' | translate }}">help</mat-icon>
          @if (mathFunctionConfigForm.get('result.key').hasError('required')) {
            <mat-error translate>
              rule-node-config.key-field-input-required
            </mat-error>
          }
        </mat-form-field>
      </div>
      <div class="xs:flex-col gt-xs:gap-4 flex flex-1 flex-row">
        <mat-form-field floatLabel="always" class="mat-block flex-1" subscriptSizing="dynamic" appearance="outline">
          <mat-label translate>rule-node-config.number-floating-point-field-input</mat-label>
          <input formControlName="resultValuePrecision"
                 matInput
                 step="1" min="0"
                 type="number"/>
          <mat-hint [innerHTML]="'rule-node-config.number-floating-point-field-input-hint' | translate | safe: 'html'"></mat-hint>
        </mat-form-field>
      </div>
      @if ([ArgumentTypeResult.ATTRIBUTE, ArgumentTypeResult.TIME_SERIES].includes(mathFunctionConfigForm.get('result').get('type').value)) {
        <div class="xs:flex-col gt-xs:gap-4 flex flex-1 flex-row items-stretch justify-start" style="padding-top: 16px;">
          <mat-checkbox formControlName="addToBody">
            {{'rule-node-config.add-to-message-field-input' | translate }}
          </mat-checkbox>
          <mat-checkbox formControlName="addToMetadata">
            {{'rule-node-config.add-to-metadata-field-input' | translate}}
          </mat-checkbox>
        </div>
      }
    </div>
  </fieldset>
</section>
