/**
 * @license
 * Copyright ASW (A Software World) All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file
 */
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { AswConfirmDialog } from '@asoftwareworld/form-builder/form-control/confirm-dialog';
import { Constants } from '@asoftwareworld/form-builder/form-control/core';
import { AswAutocompleteDialog } from './autocomplete-dialog';
import * as i0 from "@angular/core";
import * as i1 from "@angular/material/dialog";
import * as i2 from "@angular/material/form-field";
import * as i3 from "@angular/material/autocomplete";
import * as i4 from "@angular/material/core";
import * as i5 from "@angular/material/button";
import * as i6 from "@angular/material/icon";
import * as i7 from "@angular/common";
import * as i8 from "@angular/material/input";
import * as i9 from "@angular/forms";
import * as i10 from "@angular/material/tooltip";
export class AswAutocomplete {
    constructor(dialog) {
        this.dialog = dialog;
        this.constants = Constants;
        /**
         * Autocomplete control
         */
        this.control = null;
        this.isPreviewTemplate = false;
        this.autocompleteUpdateEvent = new EventEmitter();
        this.autocompleteDeleteEvent = new EventEmitter();
        this.selectionChange = new EventEmitter();
        this.duplicateControl = new EventEmitter();
    }
    ngOnInit() {
        this.filteredOptions = this.control?.options;
    }
    filter(control) {
        if (control.value) {
            const filterValue = control.value.toLowerCase();
            control.options.forEach(element => {
                element.isChecked = control.value === element.key ? true : false;
            });
            this.selectionChange.emit(control);
            return this.control?.options.filter(option => option.value.toLowerCase().startsWith(filterValue));
        }
        return this.control?.options;
    }
    /**
     * Delete autocomplete control based on control index
     * @param control autocomplete control items
     * @param controlIndex autocomplete control index
     */
    deleteAutocompleteDialog(control, controlIndex) {
        const dialogRef = this.dialog.open(AswConfirmDialog, {
            width: '350px',
            data: { name: control.controlType, message: this.constants.messages.waringMessage }
        });
        dialogRef.afterClosed().subscribe(result => {
            if (result !== undefined) {
                this.autocompleteDeleteEvent.emit(controlIndex);
            }
        });
    }
    editAutocompleteDialog(control, controlIndex) {
        control.options.forEach(element => {
            element.isChecked = control.value === element.key ? true : false;
        });
        const dialogRef = this.dialog.open(AswAutocompleteDialog, {
            disableClose: true,
            width: '744px',
            data: control
        });
        dialogRef.afterClosed().subscribe(result => {
            if (result !== undefined) {
                this.autocompleteUpdateEvent.emit({ control: result, index: controlIndex });
            }
        });
    }
    duplicateAutocompleteControl(control) {
        this.duplicateControl.emit(control);
    }
}
AswAutocomplete.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.2.4", ngImport: i0, type: AswAutocomplete, deps: [{ token: i1.MatDialog }], target: i0.ɵɵFactoryTarget.Component });
AswAutocomplete.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "13.2.4", type: AswAutocomplete, selector: "asw-autocomplete", inputs: { control: "control", controlIndex: "controlIndex", isPreviewTemplate: "isPreviewTemplate" }, outputs: { autocompleteUpdateEvent: "autocompleteUpdateEvent", autocompleteDeleteEvent: "autocompleteDeleteEvent", selectionChange: "selectionChange", duplicateControl: "duplicateControl" }, ngImport: i0, template: "<ng-container *ngIf=\"control as control\">\r\n    <mat-form-field [appearance]=\"control.style\" class=\"asw-mat-form-field {{control.customClass}}\">\r\n        <mat-label>{{control.label}}</mat-label>\r\n        <input [placeholder]=\"control.label\"\r\n               [id]=\"control.id\"\r\n               [matAutocomplete]=\"auto\"\r\n               matInput\r\n               [(ngModel)]=\"control.value\"                    \r\n                #input=\"ngModel\"\r\n               [matTooltip]=\"control.tooltip\"\r\n               (ngModelChange)=\"filteredOptions = filter(control)\"\r\n               [required]=\"control.isRequired\">\r\n        <mat-autocomplete autoActiveFirstOption #auto=\"matAutocomplete\">\r\n            <mat-option *ngFor=\"let option of filteredOptions\" [value]=\"option.key\">\r\n                {{option.value}}\r\n            </mat-option>\r\n        </mat-autocomplete>\r\n        <mat-error class=\"asw-mat-error\" *ngIf=\"input.invalid && (input.dirty || input.touched)\">\r\n            <mat-error class=\"asw-mat-error\" *ngIf=\"input?.errors?.required\">\r\n                {{control.label}} is required\r\n            </mat-error>\r\n        </mat-error>\r\n    </mat-form-field>\r\n    <ng-template [ngIf]=\"isPreviewTemplate\">\r\n        <div class=\"row\">\r\n            <div class=\"col-md-12\">\r\n                <button mat-icon-button\r\n                        type=\"button\" \r\n                        matTooltip=\"Duplicate\" \r\n                        [matTooltipPosition]=\"'below'\"\r\n                        (click)=\"duplicateAutocompleteControl(control)\">\r\n                    <mat-icon>content_copy</mat-icon>\r\n                </button>\r\n                <button mat-icon-button \r\n                        type=\"button\" \r\n                        matTooltip=\"Edit\" \r\n                        [matTooltipPosition]=\"'below'\" \r\n                        (click)=\"editAutocompleteDialog(control, controlIndex)\">\r\n                    <mat-icon>edit</mat-icon>\r\n                </button>\r\n                <button mat-icon-button \r\n                        type=\"button\" \r\n                        matTooltip=\"Delete\" \r\n                        [matTooltipPosition]=\"'below'\" \r\n                        (click)=\"deleteAutocompleteDialog(control, controlIndex)\">\r\n                    <mat-icon>delete</mat-icon>\r\n                </button>\r\n            </div>                                            \r\n        </div>\r\n    </ng-template>    \r\n</ng-container>", components: [{ type: i2.MatFormField, selector: "mat-form-field", inputs: ["color", "appearance", "hideRequiredMarker", "hintLabel", "floatLabel"], exportAs: ["matFormField"] }, { type: i3.MatAutocomplete, selector: "mat-autocomplete", inputs: ["disableRipple"], exportAs: ["matAutocomplete"] }, { type: i4.MatOption, selector: "mat-option", exportAs: ["matOption"] }, { type: i5.MatButton, selector: "button[mat-button], button[mat-raised-button], button[mat-icon-button],             button[mat-fab], button[mat-mini-fab], button[mat-stroked-button],             button[mat-flat-button]", inputs: ["disabled", "disableRipple", "color"], exportAs: ["matButton"] }, { type: i6.MatIcon, selector: "mat-icon", inputs: ["color", "inline", "svgIcon", "fontSet", "fontIcon"], exportAs: ["matIcon"] }], directives: [{ type: i7.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { type: i2.MatLabel, selector: "mat-label" }, { type: i8.MatInput, selector: "input[matInput], textarea[matInput], select[matNativeControl],      input[matNativeControl], textarea[matNativeControl]", inputs: ["disabled", "id", "placeholder", "name", "required", "type", "errorStateMatcher", "aria-describedby", "value", "readonly"], exportAs: ["matInput"] }, { type: i3.MatAutocompleteTrigger, selector: "input[matAutocomplete], textarea[matAutocomplete]", exportAs: ["matAutocompleteTrigger"] }, { type: i9.DefaultValueAccessor, selector: "input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]" }, { type: i9.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { type: i9.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }, { type: i10.MatTooltip, selector: "[matTooltip]", exportAs: ["matTooltip"] }, { type: i9.RequiredValidator, selector: ":not([type=checkbox])[required][formControlName],:not([type=checkbox])[required][formControl],:not([type=checkbox])[required][ngModel]", inputs: ["required"] }, { type: i7.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { type: i2.MatError, selector: "mat-error", inputs: ["id"] }] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.4", ngImport: i0, type: AswAutocomplete, decorators: [{
            type: Component,
            args: [{ selector: 'asw-autocomplete', template: "<ng-container *ngIf=\"control as control\">\r\n    <mat-form-field [appearance]=\"control.style\" class=\"asw-mat-form-field {{control.customClass}}\">\r\n        <mat-label>{{control.label}}</mat-label>\r\n        <input [placeholder]=\"control.label\"\r\n               [id]=\"control.id\"\r\n               [matAutocomplete]=\"auto\"\r\n               matInput\r\n               [(ngModel)]=\"control.value\"                    \r\n                #input=\"ngModel\"\r\n               [matTooltip]=\"control.tooltip\"\r\n               (ngModelChange)=\"filteredOptions = filter(control)\"\r\n               [required]=\"control.isRequired\">\r\n        <mat-autocomplete autoActiveFirstOption #auto=\"matAutocomplete\">\r\n            <mat-option *ngFor=\"let option of filteredOptions\" [value]=\"option.key\">\r\n                {{option.value}}\r\n            </mat-option>\r\n        </mat-autocomplete>\r\n        <mat-error class=\"asw-mat-error\" *ngIf=\"input.invalid && (input.dirty || input.touched)\">\r\n            <mat-error class=\"asw-mat-error\" *ngIf=\"input?.errors?.required\">\r\n                {{control.label}} is required\r\n            </mat-error>\r\n        </mat-error>\r\n    </mat-form-field>\r\n    <ng-template [ngIf]=\"isPreviewTemplate\">\r\n        <div class=\"row\">\r\n            <div class=\"col-md-12\">\r\n                <button mat-icon-button\r\n                        type=\"button\" \r\n                        matTooltip=\"Duplicate\" \r\n                        [matTooltipPosition]=\"'below'\"\r\n                        (click)=\"duplicateAutocompleteControl(control)\">\r\n                    <mat-icon>content_copy</mat-icon>\r\n                </button>\r\n                <button mat-icon-button \r\n                        type=\"button\" \r\n                        matTooltip=\"Edit\" \r\n                        [matTooltipPosition]=\"'below'\" \r\n                        (click)=\"editAutocompleteDialog(control, controlIndex)\">\r\n                    <mat-icon>edit</mat-icon>\r\n                </button>\r\n                <button mat-icon-button \r\n                        type=\"button\" \r\n                        matTooltip=\"Delete\" \r\n                        [matTooltipPosition]=\"'below'\" \r\n                        (click)=\"deleteAutocompleteDialog(control, controlIndex)\">\r\n                    <mat-icon>delete</mat-icon>\r\n                </button>\r\n            </div>                                            \r\n        </div>\r\n    </ng-template>    \r\n</ng-container>" }]
        }], ctorParameters: function () { return [{ type: i1.MatDialog }]; }, propDecorators: { control: [{
                type: Input
            }], controlIndex: [{
                type: Input
            }], isPreviewTemplate: [{
                type: Input
            }], autocompleteUpdateEvent: [{
                type: Output
            }], autocompleteDeleteEvent: [{
                type: Output
            }], selectionChange: [{
                type: Output
            }], duplicateControl: [{
                type: Output
            }] } });
//# sourceMappingURL=data:application/json;base64, 