<!--

    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]="keyRowFormGroup" class="tb-form-table-row tb-data-key-row" [class]="{'tb-single-row': singleRow}">
  @if (hasAdditionalLatestDataKeys) {
    <mat-form-field class="tb-inline-field tb-source-field" appearance="outline" subscriptSizing="dynamic">
      <mat-select formControlName="latest">
        <mat-option [value]="false">{{ 'datakey.timeseries' | translate }}</mat-option>
        <mat-option [value]="true">{{ 'datakey.latest' | translate }}</mat-option>
      </mat-select>
    </mat-form-field>
  }
  <tb-data-key-input
    [required]="required"
    [widgetType]="widgetType"
    [datasourceType]="datasourceType"
    [entityAliasId]="entityAliasId"
    [deviceId]="deviceId"
    [dataKeyType]="dataKeyType"
    [isLatestDataKeys]="isLatestDataKeys"
    [callbacks]="callbacks"
    [generateKey]="generateDataKey"
    (keyEdit)="editKey()"
    [formControl]="keyFormControl">
  </tb-data-key-input>
  @if (!hideDataKeyLabel) {
    <mat-form-field class="tb-inline-field tb-label-field" appearance="outline" subscriptSizing="dynamic">
      <input matInput formControlName="label" placeholder="{{ 'widget-config.set' | translate }}">
    </mat-form-field>
  }
  @if (timeSeriesChart && showTimeSeriesType) {
    <div class="tb-time-series-type-field">
      @if (displayUnitsOrDigits) {
        <mat-form-field class="tb-inline-field fixed-height" appearance="outline" subscriptSizing="dynamic">
          <mat-select formControlName="timeSeriesType" panelClass="tb-time-series-type-field">
            <mat-select-trigger>
              @if (keyRowFormGroup.get('timeSeriesType').value) {
                <tb-icon>
                  {{ timeSeriesChartSeriesTypeIcons.get(keyRowFormGroup.get('timeSeriesType').value) }}
                </tb-icon>
              }
            </mat-select-trigger>
            @for (type of timeSeriesChartSeriesTypes; track type) {
              <mat-option [value]="type" class="flex">
                <tb-icon>{{ timeSeriesChartSeriesTypeIcons.get(type) }}</tb-icon>
                <span>{{ timeSeriesChartSeriesTypeTranslations.get(type) | translate }}</span>
              </mat-option>
            }
          </mat-select>
        </mat-form-field>
      }
    </div>
  }
  @if (timeSeriesChart) {
    <div class="tb-y-axis-field">
      @if (displayUnitsOrDigits) {
        <mat-form-field class="tb-inline-field fixed-height" appearance="outline" subscriptSizing="dynamic">
          <mat-select formControlName="yAxis">
            @for (yAxis of yAxisIds; track yAxis) {
              <mat-option [value]="yAxis">
                {{ yAxis }}
              </mat-option>
            }
          </mat-select>
        </mat-form-field>
      }
    </div>
  }
  @if (!hideDataKeyColor) {
    <div class="tb-color-field">
      <tb-color-input asBoxInput
                      formControlName="color">
      </tb-color-input>
    </div>
  }
  @if (!hideUnits && !hideDataKeyUnits && (!singleRow || displayUnitsOrDigits)) {
    <div class="tb-units-field">
      @if (displayUnitsOrDigits) {
        <tb-unit-input
          [supportsUnitConversion]="supportsUnitConversion"
          formControlName="units">
        </tb-unit-input>
      }
    </div>
  }
  @if (!hideDecimals && !hideDataKeyDecimals && (!singleRow || displayUnitsOrDigits)) {
    <div class="tb-decimals-field">
      @if (displayUnitsOrDigits) {
        <mat-form-field appearance="outline" class="tb-inline-field number" subscriptSizing="dynamic">
          <input matInput formControlName="decimals" type="number" min="0" max="15" step="1" placeholder="{{ 'widget-config.set' | translate }}">
          @if (singleRow) {
            <div matSuffix class="lt-md:!hidden" translate>widget-config.decimals-suffix</div>
          }
        </mat-form-field>
      }
    </div>
  }
  @if (!singleRow) {
    <div class="tb-form-table-row-cell-buttons">
      <div class="tb-settings-button lt-lg:!hidden">
        @if (modelValue) {
          <button
            type="button"
            mat-icon-button
            (click)="editKey(true)"
            [matTooltip]="keySettingsTitle"
            matTooltipPosition="above">
            <mat-icon>settings</mat-icon>
          </button>
        }
      </div>
      <button type="button"
              mat-icon-button
              (click)="keyRemoved.emit()"
              [matTooltip]="removeKeyTitle"
              matTooltipPosition="above">
        <mat-icon>delete</mat-icon>
      </button>
    </div>
  }
</div>
