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/button';
import * as i6 from '@angular/material/radio';
import * as i7 from '@angular/material/tooltip';
import * as i8 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 AswRadioButton {
    dialog;
    aswApiService;
    constants = Constants;
    icons = Icons;
    /**
     * RadioButton control
     */
    control;
    propertyPersonalization = [];
    isPreviewTemplate = false;
    CSSFramework = CSSFrameworkEnum.Material;
    radioButtonUpdateEvent = new EventEmitter();
    radioButtonDeleteEvent = 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);
        }
    }
    callApiToFetchData(control) {
        switch (control.method) {
            case 'GET': {
                this.aswApiService.get(control.apiUrl, control.headers).subscribe({
                    next: (data) => {
                        if (data?.value) {
                            this.control.options = data.value;
                        }
                        else {
                            this.control.options = data;
                        }
                    }
                });
                break;
            }
            case 'POST': {
                this.aswApiService.post(control.apiUrl, control.body, control.headers).subscribe({
                    next: (data) => {
                        if (data?.value) {
                            this.control.options = data.value;
                        }
                        else {
                            this.control.options = data;
                        }
                    }
                });
                break;
            }
            case 'PUT': {
                this.aswApiService.put(control.apiUrl, control.body, control.headers).subscribe({
                    next: (data) => {
                        if (data?.value) {
                            this.control.options = data.value;
                        }
                        else {
                            this.control.options = data;
                        }
                    }
                });
                break;
            }
            case 'PATCH': {
                this.aswApiService.patch(control.apiUrl, control.body, control.headers).subscribe({
                    next: (data) => {
                        if (data?.value) {
                            this.control.options = data.value;
                        }
                        else {
                            this.control.options = data;
                        }
                    }
                });
                break;
            }
            case 'DELETE': {
                this.aswApiService.delete(control.apiUrl, control.headers).subscribe({
                    next: (data) => {
                        if (data?.value) {
                            this.control.options = data.value;
                        }
                        else {
                            this.control.options = data;
                        }
                    }
                });
                break;
            }
        }
    }
    deleteRadioButtonDialog(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.radioButtonDeleteEvent.emit(control);
            }
        });
    }
    editRadioButtonDialog(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.radioButtonUpdateEvent.emit(result);
            }
        });
    }
    onSelectionChange(control) {
        control.options.forEach((element) => {
            element.isChecked = control.value === element.key ? true : false;
        });
        this.selectionChange.emit(control);
    }
    duplicateRadioButtonControl(control) {
        this.duplicateControl.emit(control);
    }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.2", ngImport: i0, type: AswRadioButton, deps: [{ token: i1.MatDialog }, { token: i2.AswApiService }], target: i0.ɵɵFactoryTarget.Component });
    static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "16.2.2", type: AswRadioButton, selector: "asw-radio-button", inputs: { control: "control", propertyPersonalization: "propertyPersonalization", isPreviewTemplate: "isPreviewTemplate", CSSFramework: "CSSFramework" }, outputs: { radioButtonUpdateEvent: "radioButtonUpdateEvent", radioButtonDeleteEvent: "radioButtonDeleteEvent", selectionChange: "selectionChange", duplicateControl: "duplicateControl" }, ngImport: i0, template: "<ng-container *ngIf=\"control as control\">\r\n    <label id=\"example-radio-group-label\" [innerHTML]=\"control.label | aswSafeHtml\"></label>\r\n    <mat-radio-group class=\"asw-radio-group {{control.customClass}}\"\r\n        color=\"primary\"\r\n        [id]=\"control.id\"\r\n        [matTooltip]=\"control.tooltip\"\r\n        [(ngModel)]=\"control.value\"\r\n        (change)=\"onSelectionChange(control)\"\r\n        [disabled]=\"control.isDisabled ? true : false\">\r\n        <mat-radio-button\r\n            *ngFor=\"let option of control.options\" \r\n            [value]=\"option.key\">\r\n            {{ option.value }}\r\n        </mat-radio-button>\r\n    </mat-radio-group>\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)=\"duplicateRadioButtonControl(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)=\"editRadioButtonDialog(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)=\"deleteRadioButtonDialog(control)\">\r\n                <div [innerHTML]=\"icons.delete2 | aswSafeHtml\"></div>\r\n            </button>\r\n        </div>\r\n    </div>\r\n</ng-container>\r\n", 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.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i4.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }, { kind: "component", type: i5.MatIconButton, selector: "button[mat-icon-button]", inputs: ["disabled", "disableRipple", "color"], exportAs: ["matButton"] }, { kind: "directive", type: i6.MatRadioGroup, selector: "mat-radio-group", exportAs: ["matRadioGroup"] }, { kind: "component", type: i6.MatRadioButton, selector: "mat-radio-button", inputs: ["disableRipple", "tabIndex"], exportAs: ["matRadioButton"] }, { kind: "directive", type: i7.MatTooltip, selector: "[matTooltip]", exportAs: ["matTooltip"] }, { kind: "pipe", type: i2.AswSafeHtmlPipe, name: "aswSafeHtml" }, { kind: "pipe", type: i8.AswTranslatePipe, name: "aswTranslate" }] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.2", ngImport: i0, type: AswRadioButton, decorators: [{
            type: Component,
            args: [{ selector: 'asw-radio-button', template: "<ng-container *ngIf=\"control as control\">\r\n    <label id=\"example-radio-group-label\" [innerHTML]=\"control.label | aswSafeHtml\"></label>\r\n    <mat-radio-group class=\"asw-radio-group {{control.customClass}}\"\r\n        color=\"primary\"\r\n        [id]=\"control.id\"\r\n        [matTooltip]=\"control.tooltip\"\r\n        [(ngModel)]=\"control.value\"\r\n        (change)=\"onSelectionChange(control)\"\r\n        [disabled]=\"control.isDisabled ? true : false\">\r\n        <mat-radio-button\r\n            *ngFor=\"let option of control.options\" \r\n            [value]=\"option.key\">\r\n            {{ option.value }}\r\n        </mat-radio-button>\r\n    </mat-radio-group>\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)=\"duplicateRadioButtonControl(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)=\"editRadioButtonDialog(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)=\"deleteRadioButtonDialog(control)\">\r\n                <div [innerHTML]=\"icons.delete2 | aswSafeHtml\"></div>\r\n            </button>\r\n        </div>\r\n    </div>\r\n</ng-container>\r\n" }]
        }], ctorParameters: function () { return [{ type: i1.MatDialog }, { type: i2.AswApiService }]; }, propDecorators: { control: [{
                type: Input
            }], propertyPersonalization: [{
                type: Input
            }], isPreviewTemplate: [{
                type: Input
            }], CSSFramework: [{
                type: Input
            }], radioButtonUpdateEvent: [{
                type: Output
            }], radioButtonDeleteEvent: [{
                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 AswRadioButtonModule {
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.2", ngImport: i0, type: AswRadioButtonModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
    static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "16.2.2", ngImport: i0, type: AswRadioButtonModule, declarations: [AswRadioButton], imports: [CommonModule, FormsModule, ReactiveFormsModule, MaterialModule, AswConfirmDialogModule, AswSharedDialogModule, AswPipeModule, AswTranslateModule], exports: [AswRadioButton] });
    static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "16.2.2", ngImport: i0, type: AswRadioButtonModule, imports: [CommonModule, FormsModule, ReactiveFormsModule, MaterialModule, AswConfirmDialogModule, AswSharedDialogModule, AswPipeModule, AswTranslateModule] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.2", ngImport: i0, type: AswRadioButtonModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [CommonModule, FormsModule, ReactiveFormsModule, MaterialModule, AswConfirmDialogModule, AswSharedDialogModule, AswPipeModule, AswTranslateModule],
                    declarations: [AswRadioButton],
                    exports: [AswRadioButton]
                }]
        }] });

/**
 * @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 { AswRadioButton, AswRadioButtonModule };
//# sourceMappingURL=asoftwareworld-form-builder-pro-form-control-radio-button.mjs.map
