<!--

    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 [formGroup]="lwm2mListFormGroup" class="mat-block" [appearance]="appearance">
  <mat-label translate>device-profile.lwm2m.object-list</mat-label>
  <mat-chip-grid #chipList formControlName="objectsList" [required]="required">
    @for (objectLwm2m of objectsList; track objectLwm2m) {
      <mat-chip-row
        [removable]="!disabled"
        (removed)="remove(objectLwm2m)">
        {{objectLwm2m.name}} #{{objectLwm2m.keyId}}
        @if (!disabled) {
          <mat-icon matChipRemove>close</mat-icon>
        }
      </mat-chip-row>
    }
    <input matInput type="text"
           style="max-width: 200px;"
           #objectInput
           (focusin)="onFocus()"
           formControlName="objectLwm2m"
           matAutocompleteOrigin
           #origin="matAutocompleteOrigin"
           [matAutocompleteConnectedTo]="origin"
           [matAutocomplete]="objectAutocomplete"
           [matChipInputFor]="chipList">
  </mat-chip-grid>
  <mat-autocomplete #objectAutocomplete="matAutocomplete"
                    class="tb-autocomplete"
                    [displayWith]="displayObjectLwm2mFn">
    @for (objectLwm2m of filteredObjectsList | async; track objectLwm2m) {
      <mat-option [value]="objectLwm2m">
        <span [innerHTML]="objectLwm2m.keyId + ': ' + (objectLwm2m.name  | highlight: searchText)"></span>
      </mat-option>
    }
    @if (!(filteredObjectsList | async)?.length) {
      <mat-option [value]="null">
        @if (!textIsNotEmpty(searchText)) {
          <div>
            <span translate>device-profile.lwm2m.no-objects-found</span>
          </div>
        } @else {
          <span>
            {{ 'device-profile.lwm2m.no-objects-matching' | translate:{object: (searchText | truncate: true: 6: &apos;...&apos;)} }}
          </span>
        }
      </mat-option>
    }
  </mat-autocomplete>
  @if (lwm2mListFormGroup.get('objectsList').hasError('required')) {
    <mat-error>
      {{ 'device-profile.lwm2m.object-list-empty' | translate }}
    </mat-error>
  }
</mat-form-field>

