<!--

    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]="selectTenantProfileFormGroup" class="mat-block" [appearance]="appearance">
  <mat-label translate>tenant-profile.tenant-profile</mat-label>
  <input matInput type="text"
         #tenantProfileInput
         formControlName="tenantProfile"
         (focusin)="onFocus()"
         [required]="required"
         (keydown)="tenantProfileEnter($event)"
         (keypress)="tenantProfileEnter($event)"
         [matAutocomplete]="tenantProfileAutocomplete"
         [class.!hidden]="showDetailsPageLink && disabled && selectTenantProfileFormGroup.get('tenantProfile').value">
  @if (showDetailsPageLink && selectTenantProfileFormGroup.get('tenantProfile').value && disabled) {
    <a aria-label="Open tenant profile" [routerLink]=tenantProfileURL>
      {{ displayTenantProfileFn(selectTenantProfileFormGroup.get('tenantProfile').value) }}
    </a>
  }
  @if (selectTenantProfileFormGroup.get('tenantProfile').value && !disabled) {
    <button
      type="button"
      matSuffix mat-icon-button aria-label="Clear"
      (click)="clear()">
      <mat-icon class="material-icons">close</mat-icon>
    </button>
  }
  @if (selectTenantProfileFormGroup.get('tenantProfile').value?.id && !disabled) {
    <button
      type="button"
      matSuffix mat-icon-button aria-label="Edit"
      matTooltip="{{ 'tenant-profile.edit' | translate }}"
      matTooltipPosition="above"
      (click)="editTenantProfile($event)">
      <mat-icon class="material-icons">edit</mat-icon>
    </button>
  }
  @if (!selectTenantProfileFormGroup.get('tenantProfile').value && !disabled) {
    <button mat-button color="primary" matSuffix
            (click)="createTenantProfile($event, '')"
            >
      <span style="white-space: nowrap">{{ 'notification.create-new' | translate }}</span>
    </button>
  }
  <mat-autocomplete
    class="tb-autocomplete"
    #tenantProfileAutocomplete="matAutocomplete"
    [displayWith]="displayTenantProfileFn">
    @for (tenantProfile of filteredTenantProfiles | async; track tenantProfile) {
      <mat-option [value]="tenantProfile">
        <span [innerHTML]="tenantProfile.name | highlight:searchText"></span>
      </mat-option>
    }
    @if (!(filteredTenantProfiles | async)?.length) {
      <mat-option [value]="null" class="tb-not-found">
        <div class="tb-not-found-content" (click)="$event.stopPropagation()">
          @if (!textIsNotEmpty(searchText)) {
            <div>
              <span translate>tenant-profile.no-tenant-profiles-found</span>
            </div>
          } @else {
            <span>
              {{ translate.get('tenant-profile.no-tenant-profiles-matching',
              {entity: truncate.transform(searchText, true, 6, &apos;...&apos;)}) | async }}
            </span>
          }
          <span>
            <a translate (click)="createTenantProfile($event, searchText)">tenant-profile.create-new-tenant-profile</a>
          </span>
        </div>
      </mat-option>
    }
  </mat-autocomplete>
  @if (selectTenantProfileFormGroup.get('tenantProfile').hasError('required')) {
    <mat-error>
      {{ 'tenant-profile.tenant-profile-required' | translate }}
    </mat-error>
  }
</mat-form-field>
