<!--

    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.

-->
<mat-form-field [class]="fieldClass"
                [formGroup]="stringItemsForm" class="tb-string-items-list mat-block flex-1"
                [floatLabel]="floatLabel"
                [appearance]="appearance"
                [subscriptSizing]="subscriptSizing">
  @if (label) {
    <mat-label>{{ label }}</mat-label>
  }
  <mat-chip-grid #itemsChipList formControlName="items" [required]="required">
    @for (item of itemList; track item) {
      <mat-chip-row
        class="tb-chip-row-ellipsis"
        [removable]="!disabled"
        [contentEditable]="editable && !disabled"
        (removed)="removeItems(item)">
        {{ item.name }}
        @if (!disabled) {
          <mat-icon matChipRemove>close</mat-icon>
        }
      </mat-chip-row>
    }
    <input matInput type="text"
           placeholder="{{ placeholder }}"
           style="max-width: 300px;min-width: 250px"
           #stringItemInput
           (focusin)="onFocus()"
           formControlName="item"
           #origin="matAutocompleteOrigin"
           [matChipInputFor]="itemsChipList"
           [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
           [matChipInputAddOnBlur]="true"
           matAutocompleteOrigin
           (matChipInputTokenEnd)="addOnEnd($event)"
           [matAutocompleteConnectedTo]="origin"
           [matAutocomplete]="stringItemAutocomplete"
           [matAutocompleteDisabled]="!predefinedValues?.length && !fetchOptionsFn">
  </mat-chip-grid>
  <mat-autocomplete #stringItemAutocomplete="matAutocomplete"
                    [displayWith]="displayValueFn"
                    (optionSelected)="onOptionSelected($event)"
                    class="tb-autocomplete">
    @for (value of filteredValues | async; track value.value) {
      <mat-option [value]="value" (mousedown)="$event.preventDefault()"><span [innerHTML]="value.name | highlight:searchText:true"></span></mat-option>
      } @empty {
      @if (!allowUserValue) {
        <mat-option [value]="null" (mousedown)="$event.preventDefault()">{{ 'common.not-found' | translate }}</mat-option>
      }
    }
  </mat-autocomplete>
  <mat-hint [hidden]="!hint">
    {{ hint }}
  </mat-hint>
  <mat-hint align="end" [hidden]="!matHintEnd.children.length" #matHintEnd>
    <ng-content select="[matHintEnd]"></ng-content>
  </mat-hint>
  <div matSuffix>
    <ng-content select="[matSuffix]"></ng-content>
  </div>
  @if (stringItemsForm.get('items').hasError('required')) {
    <mat-error>
      {{ requiredText }}
    </mat-error>
  }
</mat-form-field>
