<!--

    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.

-->
@if (dataKeyConfigMode === dataKeyConfigModes.general) {
  <div
    [formGroup]="dataKeyFormGroup">
    <div class="mat-padding flex flex-col gap-4">
      <div class="tb-form-panel">
        <div class="tb-form-panel-title" translate>datakey.general</div>
        @if (modelValue.type !== dataKeyTypes.function && !hideDataKeyName) {
          <mat-form-field subscriptSizing="dynamic" appearance="outline">
            <mat-label>{{ 'entity.key' | translate }}</mat-label>
            <input matInput type="text" placeholder="{{ 'entity.key-name' | translate }}"
                   #keyInput
                   formControlName="name"
                   required
                   [matAutocomplete]="keyAutocomplete">
            @if (dataKeyFormGroup.get('name').value && !dataKeyFormGroup.get('name').disabled) {
              <button
                type="button"
                matSuffix mat-icon-button aria-label="Clear"
                (click)="clearKey()">
                <mat-icon class="material-icons">close</mat-icon>
              </button>
            }
            <mat-autocomplete
              class="tb-autocomplete"
              #keyAutocomplete="matAutocomplete">
              @for (key of filteredKeys | async; track key) {
                <mat-option [value]="key">
                  <span [innerHTML]="key | highlight:keySearchText"></span>
                </mat-option>
              }
            </mat-autocomplete>
          </mat-form-field>
        }
        @if (!hideDataKeyLabel) {
          <mat-form-field subscriptSizing="dynamic" appearance="outline">
            <mat-label translate>datakey.label</mat-label>
            <input matInput formControlName="label" required>
          </mat-form-field>
        }
        @if (modelValue.type !== dataKeyTypes.alarm) {
          @if (!hideDataKeyUnits) {
            <div class="tb-form-row space-between">
              <div translate>widget-config.units-short</div>
              <tb-unit-input
                [supportsUnitConversion]="supportsUnitConversion"
                formControlName="units">
              </tb-unit-input>
            </div>
          }
          @if (!hideDataKeyDecimals) {
            <div class="tb-form-row space-between">
              <div translate>widget-config.decimals-short</div>
              <mat-form-field appearance="outline" class="number" subscriptSizing="dynamic">
                <input matInput formControlName="decimals" type="number" min="0" max="15" step="1" placeholder="{{ 'widget-config.set' | translate }}">
              </mat-form-field>
            </div>
          }
        }
        @if (!hideDataKeyColor) {
          <div class="tb-form-row space-between">
            <div>{{ 'datakey.color' | translate }}</div>
            <tb-color-input asBoxInput
                            formControlName="color">
            </tb-color-input>
          </div>
        }
        @if (modelValue.type === dataKeyTypes.function) {
          <section class="flex flex-col">
            <span translate>datakey.data-generation-func</span>
            <br/>
            <tb-js-func #funcBodyEdit
                        [functionArgs]="['time', 'prevValue']"
                        [globalVariables]="functionScopeVariables"
                        [validationArgs]="[[1, 1],[1, '1']]"
                        withModules
                        resultType="any"
                        helpId="widget/config/datakey_generation_fn"
                        formControlName="funcBody">
            </tb-js-func>
          </section>
        }
        @if (!hideDataKeyAggregation && widgetType === widgetTypes.latest && modelValue.type === dataKeyTypes.timeseries) {
          <div class="tb-form-row column">
            <div class="tb-form-row space-between no-border no-padding">
              <div>{{ 'datakey.aggregation' | translate }}</div>
              <mat-form-field class="medium-width" subscriptSizing="dynamic" appearance="outline">
                <mat-select formControlName="aggregationType">
                  @for (aggregation of aggregations; track aggregation) {
                    <mat-option [value]="aggregation">
                      {{ aggregationTypesTranslations.get(aggregationTypes[aggregation]) | translate }}
                    </mat-option>
                  }
                </mat-select>
              </mat-form-field>
            </div>
            <div class="tb-form-hint tb-primary-fill">
              {{ dataKeyFormGroup.get('aggregationType').value ? (dataKeyAggregationTypeHintTranslations.get(aggregationTypes[dataKeyFormGroup.get('aggregationType').value]) | translate) : '' }}
              @if (dataKeyFormGroup.get('aggregationType').value !== aggregationTypes.NONE) {
                <section>
                  {{ 'datakey.aggregation-type-hint-common' | translate }}
                </section>
              }
            </div>
          </div>
        }
      </div>
      @if (dataKeyFormGroup.get('aggregationType').value && dataKeyFormGroup.get('aggregationType').value !== aggregationTypes.NONE) {
        <div
          class="tb-form-panel">
          <div class="tb-form-panel-title" translate>datakey.delta-calculation</div>
          <div class="tb-form-panel stroked tb-slide-toggle">
            <mat-expansion-panel class="tb-settings" [expanded]="dataKeyFormGroup.get('comparisonEnabled').value" [disabled]="!dataKeyFormGroup.get('comparisonEnabled').value">
              <mat-expansion-panel-header class="flex flex-row flex-wrap">
                <mat-panel-title class="flex flex-col items-start justify-center gap-2">
                  <mat-slide-toggle class="mat-slide" formControlName="comparisonEnabled" (click)="$event.stopPropagation()">
                    {{ 'datakey.enable-delta-calculation' | translate }}
                  </mat-slide-toggle>
                  <mat-hint class="tb-form-hint tb-primary-fill">{{ 'datakey.enable-delta-calculation-hint' | translate }}</mat-hint>
                </mat-panel-title>
              </mat-expansion-panel-header>
              <ng-template matExpansionPanelContent>
                @if (dataKeyFormGroup.get('comparisonEnabled').value) {
                  <div class="tb-form-row space-between">
                    <div>{{ 'widgets.chart.time-for-comparison' | translate }}</div>
                    <mat-form-field class="medium-width" subscriptSizing="dynamic" appearance="outline">
                      <mat-select formControlName="timeForComparison">
                        <mat-option [value]="'previousInterval'">
                          {{ 'widgets.chart.time-for-comparison-previous-interval' | translate }}
                        </mat-option>
                        <mat-option [value]="'days'">
                          {{ 'widgets.chart.time-for-comparison-days' | translate }}
                        </mat-option>
                        <mat-option [value]="'weeks'">
                          {{ 'widgets.chart.time-for-comparison-weeks' | translate }}
                        </mat-option>
                        <mat-option [value]="'months'">
                          {{ 'widgets.chart.time-for-comparison-months' | translate }}
                        </mat-option>
                        <mat-option [value]="'years'">
                          {{ 'widgets.chart.time-for-comparison-years' | translate }}
                        </mat-option>
                        <mat-option [value]="'customInterval'">
                          {{ 'widgets.chart.time-for-comparison-custom-interval' | translate }}
                        </mat-option>
                      </mat-select>
                    </mat-form-field>
                  </div>
                  @if (dataKeyFormGroup.get('timeForComparison').value === 'customInterval') {
                    <div class="tb-form-row space-between">
                      <div>{{ 'widgets.chart.custom-interval-value' | translate }}</div>
                      <mat-form-field class="medium-width number" subscriptSizing="dynamic" appearance="outline">
                        <input required matInput type="number" min="0" formControlName="comparisonCustomIntervalValue">
                      </mat-form-field>
                    </div>
                  }
                  <div class="tb-form-row space-between">
                    <div>{{ 'datakey.delta-calculation-result' | translate }}</div>
                    <mat-form-field class="medium-width" subscriptSizing="dynamic" appearance="outline">
                      <mat-select formControlName="comparisonResultType" style="min-width: 150px;">
                        @for (comparisonResultType of comparisonResults; track comparisonResultType) {
                          <mat-option [value]="comparisonResultType">
                            {{ comparisonResultTypeTranslations.get(comparisonResultTypes[comparisonResultType]) | translate }}
                          </mat-option>
                        }
                      </mat-select>
                    </mat-form-field>
                  </div>
                }
              </ng-template>
            </mat-expansion-panel>
          </div>
        </div>
      }
      @if ((modelValue.type === dataKeyTypes.timeseries || modelValue.type === dataKeyTypes.attribute || modelValue.type === dataKeyTypes.count) && showPostProcessing) {
        <div class="tb-form-panel tb-slide-toggle"
             >
          <mat-expansion-panel class="tb-settings" [expanded]="dataKeyFormGroup.get('usePostProcessing').value" [disabled]="!dataKeyFormGroup.get('usePostProcessing').value">
            <mat-expansion-panel-header class="flex flex-row flex-wrap">
              <mat-panel-title>
                <mat-slide-toggle class="mat-slide flex items-stretch justify-center" formControlName="usePostProcessing" (click)="$event.stopPropagation()">
                  {{ 'datakey.use-data-post-processing-func' | translate }}
                </mat-slide-toggle>
              </mat-panel-title>
            </mat-expansion-panel-header>
            <ng-template matExpansionPanelContent>
              @if (dataKeyFormGroup.get('usePostProcessing').value) {
                <tb-js-func #postFuncBodyEdit
                            [functionArgs]="['time', 'value', 'prevValue', 'timePrev', 'prevOrigValue']"
                            [globalVariables]="functionScopeVariables"
                            [validationArgs]="[[1, 1, 1, 1, 1],[1, '1', '1', 1, '1']]"
                            withModules
                            resultType="any"
                            helpId="widget/config/datakey_postprocess_fn"
                            formControlName="postFuncBody">
                </tb-js-func>
              }
              @if (dataKeyFormGroup.get('usePostProcessing').value) {
                <label class="tb-title" style="margin-left: 15px;">
                  time - {{ 'datakey.time-description' | translate }}<br/>
                  value - {{ 'datakey.value-description' | translate }}<br/>
                  prevValue - {{ 'datakey.prev-value-description' | translate }}<br/>
                  timePrev - {{ 'datakey.time-prev-description' | translate }}<br/>
                  prevOrigValue - {{ 'datakey.prev-orig-value-description' | translate }}
                </label>
              }
            </ng-template>
          </mat-expansion-panel>
        </div>
      }
    </div>
  </div>
} @else {
  <div class="mat-padding flex flex-col" [formGroup]="dataKeySettingsFormGroup">
    <tb-widget-settings
      [dashboard]="dashboard"
      [aliasController]="aliasController"
      [callbacks]="callbacks"
      [widget]="widget"
      [widgetConfig]="widgetConfig"
      formControlName="settings">
    </tb-widget-settings>
  </div>
}
