<!--

    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]="dataLayerFormGroup" class="tb-form-table-row tb-map-data-layer-row">
  <div class="tb-source-field">
    <mat-form-field class="tb-ds-type-field tb-inline-field" appearance="outline" subscriptSizing="dynamic">
      <mat-select formControlName="dsType">
        @for (type of datasourceTypes; track type) {
          <mat-option [value]="type">
            {{ datasourceTypesTranslations.get(type) | translate }}
          </mat-option>
        }
      </mat-select>
    </mat-form-field>
    @if (dataLayerFormGroup.get('dsType').value === DatasourceType.function) {
      <mat-form-field
        class="tb-label-field tb-inline-field" appearance="outline" subscriptSizing="dynamic">
        <input matInput formControlName="dsLabel" placeholder="{{ 'widget-config.set' | translate }}">
      </mat-form-field>
    }
    @if (dataLayerFormGroup.get('dsType').value === DatasourceType.device) {
      <tb-entity-autocomplete
        class="tb-device-field"
        required
        inlineField
        placeholder="{{ 'device.select-device' | translate }}"
        [entityType]="EntityType.DEVICE"
        formControlName="dsDeviceId">
      </tb-entity-autocomplete>
    }
    @if (dataLayerFormGroup.get('dsType').value === DatasourceType.entity) {
      <tb-entity-alias-select
        class="tb-entity-alias-field"
        inlineField
        tbRequired
        [aliasController]="context.aliasController"
        formControlName="dsEntityAliasId"
        [callbacks]="context.callbacks">
      </tb-entity-alias-select>
    }
  </div>
  @if (['trips', 'markers'].includes(dataLayerType)) {
    <tb-data-key-input
      class="tb-x-pos-field"
      required
      requiredText="{{ mapType === MapType.geoMap ? 'widgets.maps.data-layer.marker.latitude-key-required' : 'widgets.maps.data-layer.marker.x-pos-key-required' }}"
      [datasourceType]="dataLayerFormGroup.get('dsType').value"
      [entityAliasId]="dataLayerFormGroup.get('dsEntityAliasId').value"
      [deviceId]="dataLayerFormGroup.get('dsDeviceId').value"
      [aliasController]="context.aliasController"
      [widgetType]="dataLayerType === 'trips' ? widgetType.timeseries : widgetType.latest"
      [dataKeyType]="context.functionsOnly ? DataKeyType.function : null"
      [dataKeyTypes]="dataLayerType === 'trips' ? [DataKeyType.timeseries] : [DataKeyType.attribute, DataKeyType.timeseries]"
      [callbacks]="context.callbacks"
      [generateKey]="context.generateDataKey"
      (keyEdit)="editKey('xKey')"
      formControlName="xKey">
    </tb-data-key-input>
  }
  @if (['trips', 'markers'].includes(dataLayerType)) {
    <tb-data-key-input
      class="tb-y-pos-field"
      required
      requiredText="{{ mapType === MapType.geoMap ? 'widgets.maps.data-layer.marker.longitude-key-required' : 'widgets.maps.data-layer.marker.y-pos-key-required' }}"
      [datasourceType]="dataLayerFormGroup.get('dsType').value"
      [entityAliasId]="dataLayerFormGroup.get('dsEntityAliasId').value"
      [deviceId]="dataLayerFormGroup.get('dsDeviceId').value"
      [aliasController]="context.aliasController"
      [widgetType]="dataLayerType === 'trips' ? widgetType.timeseries : widgetType.latest"
      [dataKeyType]="context.functionsOnly ? DataKeyType.function : null"
      [dataKeyTypes]="dataLayerType === 'trips' ? [DataKeyType.timeseries] : [DataKeyType.attribute, DataKeyType.timeseries]"
      [callbacks]="context.callbacks"
      [generateKey]="context.generateDataKey"
      (keyEdit)="editKey('yKey')"
      formControlName="yKey">
    </tb-data-key-input>
  }
  @if (dataLayerType === 'polygons') {
    <tb-data-key-input
      class="tb-key-field"
      required
      requiredText="widgets.maps.data-layer.polygon.polygon-key-required"
      [datasourceType]="dataLayerFormGroup.get('dsType').value"
      [entityAliasId]="dataLayerFormGroup.get('dsEntityAliasId').value"
      [deviceId]="dataLayerFormGroup.get('dsDeviceId').value"
      [aliasController]="context.aliasController"
      [widgetType]="widgetType.latest"
      [dataKeyType]="context.functionsOnly ? DataKeyType.function : null"
      [dataKeyTypes]="[DataKeyType.attribute, DataKeyType.timeseries]"
      [callbacks]="context.callbacks"
      [generateKey]="context.generateDataKey"
      (keyEdit)="editKey('polygonKey')"
      formControlName="polygonKey">
    </tb-data-key-input>
  }
  @if (dataLayerType === 'circles') {
    <tb-data-key-input
      class="tb-key-field"
      required
      requiredText="widgets.maps.data-layer.circle.circle-key-required"
      [datasourceType]="dataLayerFormGroup.get('dsType').value"
      [entityAliasId]="dataLayerFormGroup.get('dsEntityAliasId').value"
      [deviceId]="dataLayerFormGroup.get('dsDeviceId').value"
      [aliasController]="context.aliasController"
      [widgetType]="widgetType.latest"
      [dataKeyType]="context.functionsOnly ? DataKeyType.function : null"
      [dataKeyTypes]="[DataKeyType.attribute, DataKeyType.timeseries]"
      [callbacks]="context.callbacks"
      [generateKey]="context.generateDataKey"
      (keyEdit)="editKey('circleKey')"
      formControlName="circleKey">
    </tb-data-key-input>
  }
  @if (dataLayerType === 'polylines') {
    <tb-data-key-input
      class="tb-key-field"
      required
      requiredText="widgets.maps.data-layer.polyline.polyline-key-required"
      [datasourceType]="dataLayerFormGroup.get('dsType').value"
      [entityAliasId]="dataLayerFormGroup.get('dsEntityAliasId').value"
      [deviceId]="dataLayerFormGroup.get('dsDeviceId').value"
      [aliasController]="context.aliasController"
      [widgetType]="widgetType.latest"
      [dataKeyType]="context.functionsOnly ? DataKeyType.function : null"
      [dataKeyTypes]="[DataKeyType.attribute, DataKeyType.timeseries]"
      [callbacks]="context.callbacks"
      [generateKey]="context.generateDataKey"
      (keyEdit)="editKey('polylineKey')"
      formControlName="polylineKey">
    </tb-data-key-input>
  }

  <div class="tb-form-table-row-cell-buttons">
    <button type="button"
            mat-icon-button
            #matButton
            (click)="editDataLayer($event, matButton)"
            matTooltip="{{ editDataLayerText | translate }}"
            matTooltipPosition="above">
      <mat-icon>settings</mat-icon>
    </button>
    <div class="tb-remove-button">
      <button type="button"
              mat-icon-button
              (click)="dataLayerRemoved.emit()"
              matTooltip="{{ removeDataLayerText | translate }}"
              matTooltipPosition="above">
        <mat-icon>delete</mat-icon>
      </button>
    </div>
  </div>
</div>
