import * as i0 from '@angular/core';
import { EventEmitter, Component, Input, Output, NgModule } from '@angular/core';
import * as i1 from '@angular/material/dialog';
import { CSSFrameworkEnum } from '@asoftwareworld/form-builder-pro/api';
import * as i2 from '@asoftwareworld/form-builder-pro/common';
import { Constants, Icons, MaterialModule, AswPipeModule } from '@asoftwareworld/form-builder-pro/common';
import { AswConfirmDialog, AswConfirmDialogModule } from '@asoftwareworld/form-builder-pro/form-control/confirm-dialog';
import { AswSingleSelectDialog, AswSharedDialogModule } from '@asoftwareworld/form-builder-pro/form-control/shared';
import * as i3 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as i4 from '@angular/forms';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import * as i5 from '@angular/material/autocomplete';
import * as i6 from '@angular/material/core';
import * as i7 from '@angular/material/button';
import * as i8 from '@angular/material/input';
import * as i9 from '@angular/material/form-field';
import * as i10 from '@angular/material/tooltip';
import * as i11 from '@asoftwareworld/form-builder-pro/core';
import { AswTranslateModule } from '@asoftwareworld/form-builder-pro/core';

/**
 * @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
 */
class AswAutocomplete {
    dialog;
    aswApiService;
    constants = Constants;
    icons = Icons;
    filteredOptions;
    /**
     * Autocomplete control
     */
    control;
    CSSFramework = CSSFrameworkEnum.Material;
    isPreviewTemplate = false;
    propertyPersonalization = [];
    autocompleteUpdateEvent = new EventEmitter();
    autocompleteDeleteEvent = new EventEmitter();
    selectionChange = new EventEmitter();
    duplicateControl = new EventEmitter();
    constructor(dialog, aswApiService) {
        this.dialog = dialog;
        this.aswApiService = aswApiService;
    }
    ngOnInit() {
        if (this.control?.apiUrl) {
            this.callApiToFetchData(this.control);
        }
        else {
            this.filteredOptions = this.control.options;
        }
    }
    callApiToFetchData(control) {
        switch (control.method) {
            case 'GET': {
                this.aswApiService.get(control.apiUrl, control.headers).subscribe({
                    next: (data) => {
                        if (data?.value) {
                            this.setControlOptions(data.value);
                        }
                        else {
                            this.setControlOptions(data);
                        }
                    }
                });
                break;
            }
            case 'POST': {
                this.aswApiService.post(control.apiUrl, control.body, control.headers).subscribe({
                    next: (data) => {
                        if (data?.value) {
                            this.setControlOptions(data.value);
                        }
                        else {
                            this.setControlOptions(data);
                        }
                    }
                });
                break;
            }
            case 'PUT': {
                this.aswApiService.put(control.apiUrl, control.body, control.headers).subscribe({
                    next: (data) => {
                        if (data?.value) {
                            this.setControlOptions(data.value);
                        }
                        else {
                            this.setControlOptions(data);
                        }
                    }
                });
                break;
            }
            case 'PATCH': {
                this.aswApiService.patch(control.apiUrl, control.body, control.headers).subscribe({
                    next: (data) => {
                        if (data?.value) {
                            this.setControlOptions(data.value);
                        }
                        else {
                            this.setControlOptions(data);
                        }
                    }
                });
                break;
            }
            case 'DELETE': {
                this.aswApiService.delete(control.apiUrl, control.headers).subscribe({
                    next: (data) => {
                        if (data?.value) {
                            this.setControlOptions(data.value);
                        }
                        else {
                            this.setControlOptions(data);
                        }
                    }
                });
                break;
            }
        }
    }
    setControlOptions(options) {
        this.control.options = options;
        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
     */
    deleteAutocompleteDialog(control) {
        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(control);
            }
        });
    }
    editAutocompleteDialog(control) {
        control.options.forEach((element) => {
            element.isChecked = control.value === element.key ? true : false;
        });
        const dialogRef = this.dialog.open(AswSingleSelectDialog, {
            width: '80%',
            disableClose: true,
            data: { control, CSSFramework: this.CSSFramework, propertyPersonalization: this.propertyPersonalization }
        });
        dialogRef.afterClosed().subscribe((result) => {
            if (result !== undefined) {
                this.autocompleteUpdateEvent.emit(result);
            }
        });
    }
    duplicateAutocompleteControl(control) {
        this.duplicateControl.emit(control);
    }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.2", ngImport: i0, type: AswAutocomplete, deps: [{ token: i1.MatDialog }, { token: i2.AswApiService }], target: i0.ɵɵFactoryTarget.Component });
    static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "16.2.2", type: AswAutocomplete, selector: "asw-autocomplete", inputs: { control: "control", CSSFramework: "CSSFramework", isPreviewTemplate: "isPreviewTemplate", propertyPersonalization: "propertyPersonalization" }, outputs: { autocompleteUpdateEvent: "autocompleteUpdateEvent", autocompleteDeleteEvent: "autocompleteDeleteEvent", selectionChange: "selectionChange", duplicateControl: "duplicateControl" }, ngImport: i0, template: "<ng-container *ngIf=\"control as control\">\r\n    <ng-container *ngIf=\"CSSFramework === 'material'; else bootstrap\">\r\n        <mat-form-field [appearance]=\"control.style\" class=\"asw-width-100 {{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                [disabled]=\"control.isDisabled ? true : false\">\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 *ngIf=\"input.invalid && (input.dirty || input.touched)\">\r\n                <ng-container *ngIf=\"input?.errors?.required\">\r\n                    {{control.label}} {{'FormControl.IsRequired' | aswTranslate}}\r\n                </ng-container>\r\n            </mat-error>\r\n        </mat-form-field>\r\n    </ng-container>\r\n    <ng-template #bootstrap>\r\n        <div class=\"asw-mb-3\">\r\n            <label for=\"{{control.id}}\" \r\n            class=\"asw-input-label {{(input.invalid && (input.dirty || input.touched)) ? 'asw-red-color' : ''}}\"\r\n            [class.asw-invalid-label]=\"control.isRequired\">{{control.label}}</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                class=\"asw-input-control {{control.customClass}}\"\r\n                [class.asw-is-invalid]=\"input.invalid && (input.dirty || input.touched)\"\r\n                [required]=\"control.isRequired\"\r\n                [disabled]=\"control.isDisabled ? true : false\">\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            <div *ngIf=\"input.invalid && (input.dirty || input.touched)\" class=\"invalid-feedback\">\r\n                <ng-container *ngIf=\"input?.errors?.required\">\r\n                    {{control.label}} {{'FormControl.IsRequired' | aswTranslate}}\r\n                </ng-container>\r\n            </div>\r\n        </div>\r\n    </ng-template>\r\n    <div class=\"asw-row\" *ngIf=\"isPreviewTemplate\">\r\n        <div class=\"asw-col-md-12\" align=\"right\">\r\n            <button mat-icon-button\r\n                    type=\"button\" \r\n                    matTooltip=\"{{'FormControl.Duplicate' | aswTranslate}}\" \r\n                    [matTooltipPosition]=\"'below'\"\r\n                    (click)=\"duplicateAutocompleteControl(control)\">\r\n                <div [innerHTML]=\"icons.contentCopy | aswSafeHtml\"></div>\r\n            </button>\r\n            <button mat-icon-button \r\n                    type=\"button\" \r\n                    matTooltip=\"{{'FormControl.Edit' | aswTranslate}}\" \r\n                    [matTooltipPosition]=\"'below'\" \r\n                    (click)=\"editAutocompleteDialog(control)\">\r\n                <div [innerHTML]=\"icons.edit | aswSafeHtml\"></div>\r\n            </button>\r\n            <button mat-icon-button \r\n                    type=\"button\" \r\n                    matTooltip=\"{{'FormControl.Delete' | aswTranslate}}\" \r\n                    [matTooltipPosition]=\"'below'\" \r\n                    (click)=\"deleteAutocompleteDialog(control)\">\r\n                <div [innerHTML]=\"icons.delete2 | aswSafeHtml\"></div>\r\n            </button>\r\n        </div>\r\n    </div>\r\n</ng-container>", dependencies: [{ kind: "directive", type: i3.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { kind: "directive", type: i3.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i4.DefaultValueAccessor, selector: "input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]" }, { kind: "directive", type: i4.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i4.RequiredValidator, selector: ":not([type=checkbox])[required][formControlName],:not([type=checkbox])[required][formControl],:not([type=checkbox])[required][ngModel]", inputs: ["required"] }, { kind: "directive", type: i4.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }, { kind: "component", type: i5.MatAutocomplete, selector: "mat-autocomplete", inputs: ["disableRipple", "hideSingleSelectionIndicator"], exportAs: ["matAutocomplete"] }, { kind: "component", type: i6.MatOption, selector: "mat-option", exportAs: ["matOption"] }, { kind: "directive", type: i5.MatAutocompleteTrigger, selector: "input[matAutocomplete], textarea[matAutocomplete]", exportAs: ["matAutocompleteTrigger"] }, { kind: "component", type: i7.MatIconButton, selector: "button[mat-icon-button]", inputs: ["disabled", "disableRipple", "color"], exportAs: ["matButton"] }, { kind: "directive", 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"] }, { kind: "component", type: i9.MatFormField, selector: "mat-form-field", inputs: ["hideRequiredMarker", "color", "floatLabel", "appearance", "subscriptSizing", "hintLabel"], exportAs: ["matFormField"] }, { kind: "directive", type: i9.MatLabel, selector: "mat-label" }, { kind: "directive", type: i9.MatError, selector: "mat-error, [matError]", inputs: ["id"] }, { kind: "directive", type: i10.MatTooltip, selector: "[matTooltip]", exportAs: ["matTooltip"] }, { kind: "pipe", type: i11.AswTranslatePipe, name: "aswTranslate" }, { kind: "pipe", type: i2.AswSafeHtmlPipe, name: "aswSafeHtml" }] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.2", ngImport: i0, type: AswAutocomplete, decorators: [{
            type: Component,
            args: [{ selector: 'asw-autocomplete', template: "<ng-container *ngIf=\"control as control\">\r\n    <ng-container *ngIf=\"CSSFramework === 'material'; else bootstrap\">\r\n        <mat-form-field [appearance]=\"control.style\" class=\"asw-width-100 {{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                [disabled]=\"control.isDisabled ? true : false\">\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 *ngIf=\"input.invalid && (input.dirty || input.touched)\">\r\n                <ng-container *ngIf=\"input?.errors?.required\">\r\n                    {{control.label}} {{'FormControl.IsRequired' | aswTranslate}}\r\n                </ng-container>\r\n            </mat-error>\r\n        </mat-form-field>\r\n    </ng-container>\r\n    <ng-template #bootstrap>\r\n        <div class=\"asw-mb-3\">\r\n            <label for=\"{{control.id}}\" \r\n            class=\"asw-input-label {{(input.invalid && (input.dirty || input.touched)) ? 'asw-red-color' : ''}}\"\r\n            [class.asw-invalid-label]=\"control.isRequired\">{{control.label}}</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                class=\"asw-input-control {{control.customClass}}\"\r\n                [class.asw-is-invalid]=\"input.invalid && (input.dirty || input.touched)\"\r\n                [required]=\"control.isRequired\"\r\n                [disabled]=\"control.isDisabled ? true : false\">\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            <div *ngIf=\"input.invalid && (input.dirty || input.touched)\" class=\"invalid-feedback\">\r\n                <ng-container *ngIf=\"input?.errors?.required\">\r\n                    {{control.label}} {{'FormControl.IsRequired' | aswTranslate}}\r\n                </ng-container>\r\n            </div>\r\n        </div>\r\n    </ng-template>\r\n    <div class=\"asw-row\" *ngIf=\"isPreviewTemplate\">\r\n        <div class=\"asw-col-md-12\" align=\"right\">\r\n            <button mat-icon-button\r\n                    type=\"button\" \r\n                    matTooltip=\"{{'FormControl.Duplicate' | aswTranslate}}\" \r\n                    [matTooltipPosition]=\"'below'\"\r\n                    (click)=\"duplicateAutocompleteControl(control)\">\r\n                <div [innerHTML]=\"icons.contentCopy | aswSafeHtml\"></div>\r\n            </button>\r\n            <button mat-icon-button \r\n                    type=\"button\" \r\n                    matTooltip=\"{{'FormControl.Edit' | aswTranslate}}\" \r\n                    [matTooltipPosition]=\"'below'\" \r\n                    (click)=\"editAutocompleteDialog(control)\">\r\n                <div [innerHTML]=\"icons.edit | aswSafeHtml\"></div>\r\n            </button>\r\n            <button mat-icon-button \r\n                    type=\"button\" \r\n                    matTooltip=\"{{'FormControl.Delete' | aswTranslate}}\" \r\n                    [matTooltipPosition]=\"'below'\" \r\n                    (click)=\"deleteAutocompleteDialog(control)\">\r\n                <div [innerHTML]=\"icons.delete2 | aswSafeHtml\"></div>\r\n            </button>\r\n        </div>\r\n    </div>\r\n</ng-container>" }]
        }], ctorParameters: function () { return [{ type: i1.MatDialog }, { type: i2.AswApiService }]; }, propDecorators: { control: [{
                type: Input
            }], CSSFramework: [{
                type: Input
            }], isPreviewTemplate: [{
                type: Input
            }], propertyPersonalization: [{
                type: Input
            }], autocompleteUpdateEvent: [{
                type: Output
            }], autocompleteDeleteEvent: [{
                type: Output
            }], selectionChange: [{
                type: Output
            }], duplicateControl: [{
                type: Output
            }] } });

/**
 * @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
 */
class AswAutocompleteModule {
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.2", ngImport: i0, type: AswAutocompleteModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
    static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "16.2.2", ngImport: i0, type: AswAutocompleteModule, declarations: [AswAutocomplete], imports: [CommonModule, FormsModule, ReactiveFormsModule, AswConfirmDialogModule, AswSharedDialogModule, MaterialModule, AswTranslateModule, AswPipeModule], exports: [AswAutocomplete] });
    static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "16.2.2", ngImport: i0, type: AswAutocompleteModule, imports: [CommonModule, FormsModule, ReactiveFormsModule, AswConfirmDialogModule, AswSharedDialogModule, MaterialModule, AswTranslateModule, AswPipeModule] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.2", ngImport: i0, type: AswAutocompleteModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [CommonModule, FormsModule, ReactiveFormsModule, AswConfirmDialogModule, AswSharedDialogModule, MaterialModule, AswTranslateModule, AswPipeModule],
                    declarations: [AswAutocomplete],
                    exports: [AswAutocomplete]
                }]
        }] });

/**
 * @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
 */

/**
 * Generated bundle index. Do not edit.
 */

export { AswAutocomplete, AswAutocompleteModule };
//# sourceMappingURL=asoftwareworld-form-builder-pro-form-control-autocomplete.mjs.map
