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 { ReplaySubject } from 'rxjs';
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/core';
import * as i6 from '@angular/material/button';
import * as i7 from '@angular/material/input';
import * as i8 from '@angular/material/form-field';
import * as i9 from '@angular/material/select';
import * as i10 from '@angular/material/tooltip';
import * as i11 from '@asoftwareworld/form-builder-pro/form-control/select-search';
import { AswSelectSearchModule } from '@asoftwareworld/form-builder-pro/form-control/select-search';
import * as i12 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 AswSelect {
    dialog;
    aswApiService;
    constants = Constants;
    icons = Icons;
    filteredOptions = new ReplaySubject(1);
    filterValue = '';
    /**
     * Select control
     */
    control;
    CSSFramework = CSSFrameworkEnum.Material;
    isPreviewTemplate = false;
    propertyPersonalization = [];
    selectUpdateEvent = new EventEmitter();
    selectDeleteEvent = 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.next(this.control.options.slice());
        }
    }
    filter(control) {
        if (!control.options) {
            return;
        }
        let search = this.filterValue;
        if (!search) {
            this.filteredOptions.next(control.options.slice());
            return;
        }
        else {
            search = search.toLowerCase();
        }
        // filter the options
        this.filteredOptions.next(control.options.filter((option) => option.value.toLowerCase().indexOf(search) > -1));
    }
    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.next(this.control.options.slice());
    }
    deleteSelectDialog(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.selectDeleteEvent.emit(control);
            }
        });
    }
    editSelectDialog(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.selectUpdateEvent.emit(result);
            }
        });
    }
    onSelectionChange(control) {
        control.options.forEach((element) => {
            element.isChecked = control.value === element.key ? true : false;
        });
        this.selectionChange.emit(control);
    }
    duplicateSelectControl(control) {
        this.duplicateControl.emit(control);
    }
    filterMyOptions() { }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.2", ngImport: i0, type: AswSelect, deps: [{ token: i1.MatDialog }, { token: i2.AswApiService }], target: i0.ɵɵFactoryTarget.Component });
    static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "16.2.2", type: AswSelect, selector: "asw-select", inputs: { control: "control", CSSFramework: "CSSFramework", isPreviewTemplate: "isPreviewTemplate", propertyPersonalization: "propertyPersonalization" }, outputs: { selectUpdateEvent: "selectUpdateEvent", selectDeleteEvent: "selectDeleteEvent", 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            <mat-select [placeholder]=\"control.label\" \r\n                [id]=\"control.id\"\r\n                [matTooltip]=\"control.tooltip\"\r\n                [(ngModel)]=\"control.value\"\r\n                #input=\"ngModel\"\r\n                (selectionChange)=\"onSelectionChange(control)\"\r\n                [required]=\"control.isRequired\"\r\n                [disabled]=\"control.isDisabled ? true : false\">\r\n                <mat-option>\r\n                    <asw-select-search ngModel [(ngModel)]=\"filterValue\" (ngModelChange)=\"filter(control)\"></asw-select-search>\r\n                </mat-option>\r\n                <mat-option *ngFor=\"let option of filteredOptions | async\" [value]=\"option.key\">\r\n                    {{ option.value }}\r\n                </mat-option>\r\n            </mat-select>\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            <select matNativeControl \r\n                [id]=\"control.id\"\r\n                class=\"asw-select {{control.customClass}}\"\r\n                [class.asw-is-invalid]=\"input.invalid && (input.dirty || input.touched)\"\r\n                [matTooltip]=\"control.tooltip\"\r\n                [(ngModel)]=\"control.value\"\r\n                #input=\"ngModel\"\r\n                (change)=\"onSelectionChange(control)\"\r\n                [required]=\"control.isRequired\"\r\n                [disabled]=\"control.isDisabled ? true : false\">\r\n                <!-- <option>\r\n                    <asw-select-search ngModel [(ngModel)]=\"filterValue\" (ngModelChange)=\"filter(control)\"></asw-select-search>\r\n                </option> -->\r\n                <option *ngFor=\"let option of filteredOptions | async\" [value]=\"option.key\">\r\n                    {{ option.value }}\r\n                </option>\r\n            </select>\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)=\"duplicateSelectControl(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)=\"editSelectDialog(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)=\"deleteSelectDialog(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.NgSelectOption, selector: "option", inputs: ["ngValue", "value"] }, { kind: "directive", type: i4.ɵNgSelectMultipleOption, selector: "option", inputs: ["ngValue", "value"] }, { kind: "directive", type: i4.SelectControlValueAccessor, selector: "select:not([multiple])[formControlName],select:not([multiple])[formControl],select:not([multiple])[ngModel]", inputs: ["compareWith"] }, { 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.MatOption, selector: "mat-option", exportAs: ["matOption"] }, { kind: "component", type: i6.MatIconButton, selector: "button[mat-icon-button]", inputs: ["disabled", "disableRipple", "color"], exportAs: ["matButton"] }, { kind: "directive", type: i7.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: i8.MatFormField, selector: "mat-form-field", inputs: ["hideRequiredMarker", "color", "floatLabel", "appearance", "subscriptSizing", "hintLabel"], exportAs: ["matFormField"] }, { kind: "directive", type: i8.MatLabel, selector: "mat-label" }, { kind: "directive", type: i8.MatError, selector: "mat-error, [matError]", inputs: ["id"] }, { kind: "component", type: i9.MatSelect, selector: "mat-select", inputs: ["disabled", "disableRipple", "tabIndex", "panelWidth", "hideSingleSelectionIndicator"], exportAs: ["matSelect"] }, { kind: "directive", type: i10.MatTooltip, selector: "[matTooltip]", exportAs: ["matTooltip"] }, { kind: "component", type: i11.AswSelectSearch, selector: "asw-select-search", inputs: ["placeholderLabel", "type", "closeIcon", "closeSvgIcon", "noEntriesFoundLabel", "clearSearchInput", "searching", "disableInitialFocus", "enableClearOnEscapePressed", "preventHomeEndKeyPropagation", "disableScrollToActiveOnOptionsChanged", "ariaLabel", "showToggleAllCheckbox", "toggleAllCheckboxChecked", "toggleAllCheckboxIndeterminate", "toggleAllCheckboxTooltipMessage", "toggleAllCheckboxTooltipPosition", "hideClearSearchButton", "alwaysRestoreSelectedOptionsMulti"], outputs: ["toggleAll"] }, { kind: "pipe", type: i3.AsyncPipe, name: "async" }, { kind: "pipe", type: i12.AswTranslatePipe, name: "aswTranslate" }, { kind: "pipe", type: i2.AswSafeHtmlPipe, name: "aswSafeHtml" }] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.2", ngImport: i0, type: AswSelect, decorators: [{
            type: Component,
            args: [{ selector: 'asw-select', 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            <mat-select [placeholder]=\"control.label\" \r\n                [id]=\"control.id\"\r\n                [matTooltip]=\"control.tooltip\"\r\n                [(ngModel)]=\"control.value\"\r\n                #input=\"ngModel\"\r\n                (selectionChange)=\"onSelectionChange(control)\"\r\n                [required]=\"control.isRequired\"\r\n                [disabled]=\"control.isDisabled ? true : false\">\r\n                <mat-option>\r\n                    <asw-select-search ngModel [(ngModel)]=\"filterValue\" (ngModelChange)=\"filter(control)\"></asw-select-search>\r\n                </mat-option>\r\n                <mat-option *ngFor=\"let option of filteredOptions | async\" [value]=\"option.key\">\r\n                    {{ option.value }}\r\n                </mat-option>\r\n            </mat-select>\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            <select matNativeControl \r\n                [id]=\"control.id\"\r\n                class=\"asw-select {{control.customClass}}\"\r\n                [class.asw-is-invalid]=\"input.invalid && (input.dirty || input.touched)\"\r\n                [matTooltip]=\"control.tooltip\"\r\n                [(ngModel)]=\"control.value\"\r\n                #input=\"ngModel\"\r\n                (change)=\"onSelectionChange(control)\"\r\n                [required]=\"control.isRequired\"\r\n                [disabled]=\"control.isDisabled ? true : false\">\r\n                <!-- <option>\r\n                    <asw-select-search ngModel [(ngModel)]=\"filterValue\" (ngModelChange)=\"filter(control)\"></asw-select-search>\r\n                </option> -->\r\n                <option *ngFor=\"let option of filteredOptions | async\" [value]=\"option.key\">\r\n                    {{ option.value }}\r\n                </option>\r\n            </select>\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)=\"duplicateSelectControl(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)=\"editSelectDialog(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)=\"deleteSelectDialog(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
            }], selectUpdateEvent: [{
                type: Output
            }], selectDeleteEvent: [{
                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 AswSelectModule {
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.2", ngImport: i0, type: AswSelectModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
    static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "16.2.2", ngImport: i0, type: AswSelectModule, declarations: [AswSelect], imports: [CommonModule, FormsModule, ReactiveFormsModule, MaterialModule, AswConfirmDialogModule, AswSharedDialogModule, AswTranslateModule, AswPipeModule, AswSelectSearchModule], exports: [AswSelect] });
    static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "16.2.2", ngImport: i0, type: AswSelectModule, imports: [CommonModule, FormsModule, ReactiveFormsModule, MaterialModule, AswConfirmDialogModule, AswSharedDialogModule, AswTranslateModule, AswPipeModule, AswSelectSearchModule] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.2", ngImport: i0, type: AswSelectModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [CommonModule, FormsModule, ReactiveFormsModule, MaterialModule, AswConfirmDialogModule, AswSharedDialogModule, AswTranslateModule, AswPipeModule, AswSelectSearchModule],
                    declarations: [AswSelect],
                    exports: [AswSelect]
                }]
        }] });

/**
 * @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 { AswSelect, AswSelectModule };
//# sourceMappingURL=asoftwareworld-form-builder-pro-form-control-select.mjs.map
