<!--

    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 class="tb-form-row no-border no-padding column-xs" style="gap: 10px;" [formGroup]="valueSourceFormGroup">
  <mat-form-field class="gt-xs:max-w-33% gt-xs:flex-full xs:flex-1" appearance="outline" subscriptSizing="dynamic">
    <mat-select formControlName="valueSource">
      <mat-option [value]="'predefinedValue'">
        {{ 'widgets.value-source.predefined-value' | translate }}
      </mat-option>
      <mat-option [value]="'entityAttribute'">
        {{ 'widgets.value-source.entity-attribute' | translate }}
      </mat-option>
    </mat-select>
  </mat-form-field>
  <mat-form-field [class.!hidden]="valueSourceFormGroup.get('valueSource').value !== 'predefinedValue'"
                  appearance="outline"
                  class="number flex"
                  subscriptSizing="dynamic">
    <input matInput formControlName="value" type="number" placeholder="{{ 'widgets.value-source.value' | translate }}">
  </mat-form-field>
  <mat-form-field [class.!hidden]="valueSourceFormGroup.get('valueSource').value !== 'entityAttribute'"
                  appearance="outline"
                  class="flex"
                  subscriptSizing="dynamic">
    <input matInput type="text" placeholder="{{ entityAliasPlaceholder | translate }}"
           #entityAliasInput
           formControlName="entityAlias"
           [matAutocomplete]="entityAliasAutocomplete">
    @if (valueSourceFormGroup.get('entityAlias').value && !valueSourceFormGroup.get('entityAlias').disabled) {
      <button
        type="button"
        matSuffix mat-icon-button aria-label="Clear"
        (click)="clearEntityAlias()">
        <mat-icon class="material-icons">close</mat-icon>
      </button>
    }
    <mat-autocomplete class="tb-autocomplete" #entityAliasAutocomplete="matAutocomplete">
      @for (entityAlias of filteredEntityAliases | async; track entityAlias) {
        <mat-option [value]="entityAlias">
          <span [innerHTML]="entityAlias | highlight:aliasSearchText"></span>
        </mat-option>
      }
    </mat-autocomplete>
  </mat-form-field>
  <mat-form-field class="flex" appearance="outline" subscriptSizing="dynamic" [class.!hidden]="valueSourceFormGroup.get('valueSource').value !== 'entityAttribute'">
    <input matInput type="text" placeholder="{{ entityAttributePlaceholder | translate }}"
           #keyInput
           formControlName="attribute"
           [matAutocomplete]="keyAutocomplete">
    @if (valueSourceFormGroup.get('attribute').value && !valueSourceFormGroup.get('attribute').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>
</div>
