<div class="row multiple-auto-complete" ngForm>
	<mat-chip-list #chipList ngDefaultControl style="width: 100% !important;">
		<ng-container *ngIf="value as values">
			<mat-chip *ngFor="let value of values" [disabled]="disabled" [selectable]="selectable" [removable]="removable" [ngClass]="computeClass(value)">
				<ng-container *ngIf="_selectedItems.get(stringify(value)) as selectedItem">
					<ng-template #cellTemplate *ngIf="_selectedValueTemplate(selectedItem)" [ngTemplateOutlet]="_selectedValueTemplate(selectedItem)" [ngTemplateOutletContext]="{ item: selectedItem }"></ng-template>
					<div *ngIf="!_selectedValueTemplate(selectedItem)">{{_displayFn(selectedItem)}}</div>
					<mat-icon matChipRemove *ngIf="!disabled && removable" (click)="_removeSelectedItem(_selectedItems.get(stringify(value)), $event)">cancel</mat-icon>
				</ng-container>
			</mat-chip>
		</ng-container>
<!--		<input matInput #autocompleteInput class="col" [class.hide-placeholder]="hidePlaceholder" [name]="id" autocomplete="off" #autocompleteTrigger="matAutocompleteTrigger" [placeholder]="placeholder" [matAutocomplete]="autocomplete" [value]="inputValue" (keyup)="onKeyUp($event)" (keydown)="onKeyDown($event)" [disabled]="disabled" (focus)="_onInputFocus()" (blur)="onBlur($event)" [matChipInputFor]="chipList" [matChipInputSeparatorKeyCodes]="separatorKeysCodes" [matChipInputAddOnBlur]="autoSelectFirstOptionOnBlur" (matChipInputTokenEnd)="_addItem($event)">-->
		<input matInput #autocompleteInput class="col"
			   style="width: calc(100% - 8px) !important;"
			   [class.hide-placeholder]="hidePlaceholder" [name]="id"
			   autocomplete="off" #autocompleteTrigger="matAutocompleteTrigger"
			   [placeholder]="placeholder" [matAutocomplete]="autocomplete" [value]="inputValue"
			   (keyup)="onKeyUp($event)" (keydown)="onKeyDown($event)" [disabled]="disabled" (focus)="_onInputFocus()"
			   (blur)="onBlur($event)" [matChipInputFor]="chipList" [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
			   [matChipInputAddOnBlur]="autoSelectFirstOptionOnBlur"
			   (matChipInputTokenEnd)="_addItem($event)"
			   [matAutocompleteConnectedTo]="origin">
		<!-- The attribute autocomplete="nope", set by downshift, is ignored in Chrome 67 and Opera 54 (latest at the time of writing)
      <input matInput #autocompleteInput class="col" [name]="id" autocomplete="nope" #autocompleteTrigger="matAutocompleteTrigger" [placeholder]="placeholder" [matAutocomplete]="autocomplete" [value]="inputValue" (keyup)="onKeyUp($event)" (keydown)="onKeyDown($event)" [disabled]="disabled" (focus)="_onInputFocus()" (blur)="onBlur($event)" [matChipInputFor]="chipList" [matChipInputSeparatorKeyCodes]="separatorKeysCodes" [matChipInputAddOnBlur]="autoSelectFirstOptionOnBlur" (matChipInputTokenEnd)="_addItem($event)"> -->
		<mat-icon *ngIf="!disabled" class="align-arrow-right" matSuffix>arrow_drop_down</mat-icon>
		<div style="height: 0 !important; width: 100% !important; visibility: hidden !important;" matAutocompleteOrigin #origin="matAutocompleteOrigin"></div>
	</mat-chip-list>

	<mat-autocomplete #autocomplete="matAutocomplete" [displayWith]="_displayFn.bind(this)" (optionSelected)="_optionSelected($event)">
		<span *ngIf="_groupedItems">
			<mat-optgroup *ngFor="let group of _groupedItems | async" [label]="group.title">
				<mat-option *ngFor="let item of group.items" [value]="item" [class.two-line-mat-option]="_subtitleFn(item) && !_optionTemplate(item)">
					<!-- <img style="vertical-align:middle;" aria-hidden src="{{state.flag}}" height="25" /> -->
					<ng-template #cellTemplate *ngIf="_optionTemplate(item)" [ngTemplateOutlet]="_optionTemplate(item)" [ngTemplateOutletContext]="{
							item: item
						}"></ng-template>
					<div *ngIf="!_optionTemplate(item)" class="d-flex">
						<div class="title-subtitle-fn">
							<div class="title-fn">
								<div class="title-fn-inner">
									<span *ngIf="!_optionTemplate(item)">{{_titleFn(item)}}</span>
								</div>
							</div>
							<div *ngIf="_subtitleFn(item)" class="subtitle-fn">
								<div class="subtitle-fn-inner">
									<small [innerHTML]="_subtitleFn(item)"></small>
								</div>
							</div>
						</div>
						<span *ngIf="popupItemActionIcon" class="option-icon" (click)="_optionActionClick(item, $event)"><mat-icon>{{popupItemActionIcon}}</mat-icon></span>
					</div>
				</mat-option>
			</mat-optgroup>
		</span>
		<span *ngIf="!_groupedItems">
			<ng-container *ngIf="_items | async as autocompleteItems; else loading">
				<ng-container *ngIf="autocompleteItems.length">
					<mat-option *ngFor="let item of autocompleteItems" [value]="item" [class.two-line-mat-option]="_subtitleFn(item) && !_optionTemplate(item)">
						<!-- <img style="vertical-align:middle;" aria-hidden src="{{state.flag}}" height="25" /> -->
						<ng-template #cellTemplate *ngIf="_optionTemplate(item)" [ngTemplateOutlet]="_optionTemplate(item)" [ngTemplateOutletContext]="{
										item: item
									}"></ng-template>
						<div *ngIf="!_optionTemplate(item)" class="d-flex">
							<div class="title-subtitle-fn">
								<div class="title-fn">
									<div class="title-fn-inner">
										<span *ngIf="!_optionTemplate(item)">{{_titleFn(item)}}</span>
									</div>
								</div>
								<div *ngIf="_subtitleFn(item)" class="subtitle-fn">
									<div class="subtitle-fn-inner">
										<small [innerHTML]="_subtitleFn(item)"></small>
									</div>
								</div>
							</div>
							<span *ngIf="popupItemActionIcon" class="option-icon" (click)="_optionActionClick(item, $event)"><mat-icon>{{popupItemActionIcon}}</mat-icon></span>
						</div>
					</mat-option>
				</ng-container>
				<ng-container *ngIf="!autocompleteItems.length && showNoResultsLabel && (queryValue == inputValue)">
					<mat-option disabled="true">No results found!</mat-option>
				</ng-container>
			</ng-container>
			<ng-template #loading>
				<mat-option disabled="true">loading...</mat-option>
			</ng-template>
		</span>
	</mat-autocomplete>
</div>
