/**
 * @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/common";
import * as i6 from "@angular/material/input";
import * as i7 from "@angular/forms";
import * as i8 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();
    }
    ngOnInit() {
        this.filteredOptions = this.control?.options;
    }
    filter(value) {
        if (value) {
            const filterValue = value.toLowerCase();
            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.name, 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 });
            }
        });
    }
}
AswAutocomplete.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.2.2", ngImport: i0, type: AswAutocomplete, deps: [{ token: i1.MatDialog }], target: i0.ɵɵFactoryTarget.Component });
AswAutocomplete.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "13.2.2", type: AswAutocomplete, selector: "asw-autocomplete", inputs: { control: "control", controlIndex: "controlIndex", isPreviewTemplate: "isPreviewTemplate" }, outputs: { autocompleteUpdateEvent: "autocompleteUpdateEvent", autocompleteDeleteEvent: "autocompleteDeleteEvent" }, ngImport: i0, template: "<ng-container *ngIf=\"control as control\">\r\n    <mat-form-field [appearance]=\"control.style\">\r\n        <mat-label>{{control.label}}</mat-label>\r\n        <input [name]=\"control.name\"\r\n               [placeholder]=\"control.label\"\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.value)\"\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-form-field>\r\n    <ng-template [ngIf]=\"isPreviewTemplate\">\r\n        <div class=\"row\">\r\n            <div class=\"col-md-12\">\r\n                <button mat-raised-button \r\n                        type=\"button\" \r\n                        matTooltip=\"Edit autocomplete control\" \r\n                        [matTooltipPosition]=\"'above'\" \r\n                        (click)=\"editAutocompleteDialog(control, controlIndex)\"\r\n                        class=\"btn btn-sm btn-outline-success btn-just-icon mr-2 mb-1\">\r\n                    <i class=\"material-icons\">edit</i>\r\n                </button>\r\n                <button mat-raised-button \r\n                        type=\"button\" \r\n                        matTooltip=\"Remove autocomplete control\" \r\n                        [matTooltipPosition]=\"'above'\" \r\n                        (click)=\"deleteAutocompleteDialog(control, controlIndex)\"\r\n                        class=\"btn btn-sm btn-outline-danger btn-just-icon mb-1\">\r\n                    <i class=\"material-icons\">close</i>\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"] }], directives: [{ type: i5.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { type: i2.MatLabel, selector: "mat-label" }, { type: i6.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: i7.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: i7.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { type: i7.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }, { type: i8.MatTooltip, selector: "[matTooltip]", exportAs: ["matTooltip"] }, { type: i7.RequiredValidator, selector: ":not([type=checkbox])[required][formControlName],:not([type=checkbox])[required][formControl],:not([type=checkbox])[required][ngModel]", inputs: ["required"] }, { type: i5.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.2", 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\">\r\n        <mat-label>{{control.label}}</mat-label>\r\n        <input [name]=\"control.name\"\r\n               [placeholder]=\"control.label\"\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.value)\"\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-form-field>\r\n    <ng-template [ngIf]=\"isPreviewTemplate\">\r\n        <div class=\"row\">\r\n            <div class=\"col-md-12\">\r\n                <button mat-raised-button \r\n                        type=\"button\" \r\n                        matTooltip=\"Edit autocomplete control\" \r\n                        [matTooltipPosition]=\"'above'\" \r\n                        (click)=\"editAutocompleteDialog(control, controlIndex)\"\r\n                        class=\"btn btn-sm btn-outline-success btn-just-icon mr-2 mb-1\">\r\n                    <i class=\"material-icons\">edit</i>\r\n                </button>\r\n                <button mat-raised-button \r\n                        type=\"button\" \r\n                        matTooltip=\"Remove autocomplete control\" \r\n                        [matTooltipPosition]=\"'above'\" \r\n                        (click)=\"deleteAutocompleteDialog(control, controlIndex)\"\r\n                        class=\"btn btn-sm btn-outline-danger btn-just-icon mb-1\">\r\n                    <i class=\"material-icons\">close</i>\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
            }] } });
//# sourceMappingURL=data:application/json;base64, 