<!--

    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 appearance="outline" [formGroup]="selectUserFormGroup" class="mat-block search-users">
  <input matInput type="text" placeholder="{{ 'user.search' | translate }}"
         #userInput
         formControlName="user"
         (focusin)="onFocus()"
         [matAutocomplete]="userAutocomplete">
  <mat-icon matSuffix>search</mat-icon>
  <mat-autocomplete class="tb-assignee-autocomplete tb-autocomplete"
                    #userAutocomplete="matAutocomplete"
                    [displayWith]="displayUserFn"
                    (optionSelected)="selected($event)">
    @if (displayAssigneeNotSet) {
      <mat-option [value]="assigneeOptions.noAssignee">
        <mat-icon class="unassigned-icon">account_circle</mat-icon>
        <span>{{ assigneeNotSetText | translate }}</span>
      </mat-option>
    }
    @if (displayAssignedToCurrentUser) {
      <mat-option [value]="assigneeOptions.currentUser">
        <mat-icon class="unassigned-icon">account_circle</mat-icon>
        <span>{{ assignedToCurrentUserText | translate }}</span>
      </mat-option>
    }
    @for (user of filteredUsers | async; track user) {
      <mat-option [value]="user"
                  [class.!hidden]="assigneeId === user.id.id">
        <span class="user-avatar" [innerHTML]="getUserInitials(user)"
              [style.background-color]="getAvatarBgColor(user)">
        </span>
        <div class="user-display-name flex flex-col gap-0.5">
          @if (user.firstName || user.lastName) {
            <span
            [innerHTML]="getFullName(user) | highlight:searchText"></span>
          }
          <span [innerHTML]="user.email | highlight:searchText" tbTruncateWithTooltip></span>
        </div>
      </mat-option>
    }
    @if (!(filteredUsers | async)?.length) {
      <mat-option [value]="null" class="tb-not-found">
        <div class="tb-not-found-content" (click)="$event.stopPropagation()">
          <span style="white-space: normal">
            {{ translate.get('user.no-users-matching', {entity: searchText}) | async }}
          </span>
        </div>
      </mat-option>
    }
  </mat-autocomplete>
</mat-form-field>

