<!--

    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]="datasourcesFormGroup" class="tb-form-panel tb-datasources" [class.no-padding-bottom]="!showAddDatasource">
  <div class="flex flex-col">
    <div class="flex flex-row items-center justify-between">
      <div class="tb-form-panel-title">{{ (singleDatasource ? (isAlarmSource ? 'widget-config.alarm-source' : 'widget-config.datasource') : 'widget-config.datasources') | translate }}</div>
      @if (timeseriesKeyError) {
        <div class="flex flex-1 flex-row items-center justify-center">
          <mat-error >{{ 'widget-config.timeseries-key-error' | translate }}</mat-error>
        </div>
      }
      @if (basicMode && !hideDatasourcesMode) {
        <tb-toggle-select [ngModel]="datasourcesMode" (ngModelChange)="datasourcesModeChange($event)"
                          [ngModelOptions]="{ standalone: true }">
          <tb-toggle-option [value]="datasourceType.device">{{ 'device.device' | translate }}</tb-toggle-option>
          <tb-toggle-option [value]="datasourceType.entity">{{ 'entity.entity-alias' | translate }}</tb-toggle-option>
        </tb-toggle-select>
      }
    </div>
    @if (maxDatasources > 1) {
      <div
      class="tb-form-panel-hint">{{ 'widget-config.maximum-datasources' | translate:{count: maxDatasources} }}</div>
    }
  </div>
  @if (datasourcesFormArray.length === 0) {
    <div>
      <span translate
      class="tb-prompt flex items-center justify-center">datasource.add-datasource-prompt</span>
    </div>
  } @else {
    <div style="overflow: auto;">
      <mat-list class="tb-drop-list"
                cdkDropList
                cdkDropListOrientation="vertical"
                (cdkDropListDropped)="onDatasourceDrop($event)"
                [cdkDropListDisabled]="dragDisabled"
                formArrayName="datasources">
        @for (datasourceControl of datasourcesControls; track trackByDatasource($index, datasourceControl)) {
          <mat-list-item
            class="tb-datasource-list-item tb-draggable" cdkDrag
            [cdkDragDisabled]="dragDisabled">
            <div class="tb-datasource-row">
              @if (!singleDatasource) {
                <div class="tb-datasource-index-container">
                  <div class="tb-datasource-index">{{$index + 1}}</div>
                </div>
              }
              <div class="tb-datasource-params">
                <div class="tb-datasource-item">
                  <tb-datasource [formControl]="datasourceControl">
                  </tb-datasource>
                  @if (!singleDatasource && !disabled) {
                    <div class="tb-datasource-buttons">
                      <button class="tb-box-button" type="button"
                              mat-icon-button
                              (click)="removeDatasource($index)"
                              matTooltip="{{ 'widget-config.remove-datasource' | translate }}"
                              matTooltipPosition="above">
                        <mat-icon>delete</mat-icon>
                      </button>
                      @if (!dragDisabled) {
                        <button
                          class="tb-box-button"
                          mat-icon-button
                          type="button"
                          cdkDragHandle
                          matTooltip="{{ 'action.drag' | translate }}"
                          matTooltipPosition="above">
                          <mat-icon>drag_indicator</mat-icon>
                        </button>
                      }
                    </div>
                  }
                </div>
                <tb-error class="tb-datasource-error" [error]="datasourceError[$index] ? datasourceError[$index] : ''"></tb-error>
              </div>
            </div>
          </mat-list-item>
        }
      </mat-list>
    </div>
  }
  @if (showAddDatasource) {
    <div>
      <button type="button"
              mat-stroked-button color="primary"
              (click)="addDatasource()"
              matTooltip="{{ 'widget-config.add-datasource' | translate }}"
              matTooltipPosition="above">
        <span translate>widget-config.add-datasource</span>
      </button>
    </div>
  }
</div>
