import { MXBaseComponent, AXHotkeyDirective, AXAutoFocusDirective, AXComponent } from '@acorex/cdk/common';
import { AXButtonComponent } from '@acorex/components/button';
import { AXDecoratorGenericComponent } from '@acorex/components/decorators';
import { AXLoadingComponent } from '@acorex/components/loading';
import { AXProgressBarComponent } from '@acorex/components/progress-bar';
import { AXTranslatorPipe } from '@acorex/core/translation';
import { AsyncPipe } from '@angular/common';
import * as i0 from '@angular/core';
import { input, signal, ViewEncapsulation, ChangeDetectionStrategy, Component, NgModule, inject, ComponentRef, Injectable } from '@angular/core';
import { AXOverlayService } from '@acorex/cdk/overlay';

/**
 * Represents a loading dialog component with custom styles and encapsulation.
 * @category components
 */
class AXLoadingDialogComponent extends MXBaseComponent {
    constructor() {
        super(...arguments);
        /** Loading dialog configuration data */
        this.data = input.required(...(ngDevMode ? [{ debugName: "data" }] : []));
        /** @internal Callback function to close the dialog */
        this.onClose = input(...(ngDevMode ? [undefined, { debugName: "onClose" }] : []));
        /** @ignore */
        this.progressText = signal('', ...(ngDevMode ? [{ debugName: "progressText" }] : []));
        /** @ignore */
        this.progressStatus = signal('', ...(ngDevMode ? [{ debugName: "progressStatus" }] : []));
        /** @ignore */
        this.progressColor = signal('primary', ...(ngDevMode ? [{ debugName: "progressColor" }] : []));
        /** @ignore */
        this.dialogTitle = signal('', ...(ngDevMode ? [{ debugName: "dialogTitle" }] : []));
        /** @ignore */
        this.progressValue = signal(0, ...(ngDevMode ? [{ debugName: "progressValue" }] : []));
    }
    ngOnInit() {
        super.ngOnInit();
        // Initialize signals from input data
        const config = this.data();
        this.progressText.set(config.text ?? '');
        this.progressStatus.set(config.status ?? '');
        this.progressColor.set(config.progressColor ?? 'primary');
        this.dialogTitle.set(config.title ?? '');
        this.progressValue.set(config.progressValue ?? 0);
    }
    /**
     * Closes the loading dialog.
     *
     * @returns void - No return value. The dialog is closed and control returns to the caller.
     */
    close() {
        const closeCallback = this.onClose();
        if (closeCallback) {
            closeCallback();
        }
    }
    /**
     * Sets the displayed progress text inside the dialog.
     *
     * @param text The text to display under the progress bar.
     * @returns void
     */
    setProgressText(text) {
        this.progressText.set(text);
    }
    /**
     * Sets the status text, typically representing the current step out of total steps (e.g., "5/10").
     *
     * @param status The status text to display.
     * @returns void
     */
    setProgressStatus(status) {
        this.progressStatus.set(status);
    }
    /**
     * Sets the color used by the progress indicator.
     *
     * @param color The color token to apply to the progress UI.
     * @returns void
     */
    setProgressColor(color) {
        this.progressColor.set(color);
    }
    /**
     * Sets the dialog title text.
     *
     * @param text The title text to display at the top of the dialog.
     * @returns void
     */
    setDialogTitle(text) {
        this.dialogTitle.set(text);
    }
    /**
     * Sets the numeric progress value.
     *
     * @param value A number in the range 0–100 representing the current progress.
     * @returns void
     */
    setProgressValue(value) {
        this.progressValue.set(value);
    }
    /** @ignore */
    _handleButtonClick(button) {
        if (button.onClick) {
            button.onClick({ source: button });
        }
    }
    /** @ignore */
    _hasAutoFocus(button) {
        const buttons = this.data().buttons;
        if (!buttons || buttons.length === 0)
            return false;
        const autoFocusItem = buttons.find((item) => item.autofocus === true);
        if (autoFocusItem && autoFocusItem.text === button.text) {
            return true;
        }
        if (!autoFocusItem) {
            if (buttons.length > 0) {
                buttons[0].autofocus = true;
                return buttons[0].text === button.text;
            }
        }
        return false;
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.3", ngImport: i0, type: AXLoadingDialogComponent, deps: null, target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "20.3.3", type: AXLoadingDialogComponent, isStandalone: true, selector: "ax-loading-dialog", inputs: { data: { classPropertyName: "data", publicName: "data", isSignal: true, isRequired: true, transformFunction: null }, onClose: { classPropertyName: "onClose", publicName: "onClose", isSignal: true, isRequired: false, transformFunction: null } }, providers: [{ provide: AXComponent, useExisting: AXLoadingDialogComponent }], usesInheritance: true, ngImport: i0, template: "<ax-title>{{ dialogTitle() }}</ax-title>\n\n<ax-progress-bar [mode]=\"data().mode\" [progress]=\"progressValue()\" [color]=\"progressColor()\"></ax-progress-bar>\n<div class=\"progress-decorators\">\n  <div>{{ progressText() }}</div>\n  <div>{{ progressStatus() }}</div>\n</div>\n<ax-footer>\n  <div class=\"button-wrapper\">\n    @for (button of data().buttons; track $index) {\n      <ax-button\n        [text]=\"button.text | translate | async\"\n        [tabindex]=\"$index\"\n        [axHotkey]=\"button.hotkey\"\n        [axAutoFocus]=\"_hasAutoFocus(button)\"\n        (onClick)=\"_handleButtonClick(button)\"\n        [look]=\"button.look\"\n        [disabled]=\"button.disabled\"\n        [color]=\"button.color\"\n        class=\"button-item\"\n      >\n        @if (button.loading) {\n          <ax-loading></ax-loading>\n        }\n      </ax-button>\n    }\n  </div>\n</ax-footer>\n", styles: ["ax-loading-dialog{display:flex;flex-direction:column;gap:.5rem;width:50vw;background-color:rgba(var(--ax-comp-loading-dialog-bg-color, var(--ax-sys-color-lightest-surface)));padding:1rem;border-radius:var(--ax-comp-loading-dialog-border-radius, var(--ax-sys-border-radius))}ax-loading-dialog ax-title{font-weight:500;font-size:1rem;margin-bottom:.5rem}@media (min-width: 320px) and (max-width: 640px){ax-loading-dialog{width:93vw!important}}@media (min-width: 768px){ax-loading-dialog{width:55vw!important}}@media (min-width: 1280px){ax-loading-dialog{width:30vw!important}}@media (min-width: 1536px){ax-loading-dialog{width:26vw!important}}ax-loading-dialog .progress-decorators{display:flex;justify-content:space-between;font-size:.875rem}ax-loading-dialog ax-footer{justify-content:flex-end;padding-top:.75rem}ax-loading-dialog .button-wrapper{display:flex;flex-wrap:wrap;gap:.5rem;align-items:center;justify-content:center}ax-loading-dialog .button-item{display:flex;flex:1}\n"], dependencies: [{ kind: "component", type: AXDecoratorGenericComponent, selector: "ax-footer, ax-header, ax-content, ax-divider, ax-form-hint, ax-prefix, ax-suffix, ax-text, ax-title, ax-subtitle, ax-placeholder, ax-overlay" }, { kind: "component", type: AXProgressBarComponent, selector: "ax-progress-bar", inputs: ["color", "mode", "progress", "height"], outputs: ["ValueChange", "sizeChange"] }, { kind: "component", type: AXButtonComponent, selector: "ax-button", inputs: ["disabled", "size", "tabIndex", "color", "look", "text", "toggleable", "selected", "iconOnly", "type", "loadingText"], outputs: ["onBlur", "onFocus", "onClick", "selectedChange", "toggleableChange", "lookChange", "colorChange", "disabledChange", "loadingTextChange"] }, { kind: "directive", type: AXHotkeyDirective, selector: "[axHotkey]", inputs: ["axHotkey"] }, { kind: "directive", type: AXAutoFocusDirective, selector: "[axAutoFocus]", inputs: ["axAutoFocus", "axAutoFocusTime"] }, { kind: "component", type: AXLoadingComponent, selector: "ax-loading", inputs: ["visible", "type", "context"], outputs: ["visibleChange"] }, { kind: "pipe", type: AXTranslatorPipe, name: "translate" }, { kind: "pipe", type: AsyncPipe, name: "async" }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.3", ngImport: i0, type: AXLoadingDialogComponent, decorators: [{
            type: Component,
            args: [{ selector: 'ax-loading-dialog', changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None, imports: [
                        AXDecoratorGenericComponent,
                        AXProgressBarComponent,
                        AXButtonComponent,
                        AXHotkeyDirective,
                        AXAutoFocusDirective,
                        AXLoadingComponent,
                        AXTranslatorPipe,
                        AsyncPipe,
                    ], providers: [{ provide: AXComponent, useExisting: AXLoadingDialogComponent }], template: "<ax-title>{{ dialogTitle() }}</ax-title>\n\n<ax-progress-bar [mode]=\"data().mode\" [progress]=\"progressValue()\" [color]=\"progressColor()\"></ax-progress-bar>\n<div class=\"progress-decorators\">\n  <div>{{ progressText() }}</div>\n  <div>{{ progressStatus() }}</div>\n</div>\n<ax-footer>\n  <div class=\"button-wrapper\">\n    @for (button of data().buttons; track $index) {\n      <ax-button\n        [text]=\"button.text | translate | async\"\n        [tabindex]=\"$index\"\n        [axHotkey]=\"button.hotkey\"\n        [axAutoFocus]=\"_hasAutoFocus(button)\"\n        (onClick)=\"_handleButtonClick(button)\"\n        [look]=\"button.look\"\n        [disabled]=\"button.disabled\"\n        [color]=\"button.color\"\n        class=\"button-item\"\n      >\n        @if (button.loading) {\n          <ax-loading></ax-loading>\n        }\n      </ax-button>\n    }\n  </div>\n</ax-footer>\n", styles: ["ax-loading-dialog{display:flex;flex-direction:column;gap:.5rem;width:50vw;background-color:rgba(var(--ax-comp-loading-dialog-bg-color, var(--ax-sys-color-lightest-surface)));padding:1rem;border-radius:var(--ax-comp-loading-dialog-border-radius, var(--ax-sys-border-radius))}ax-loading-dialog ax-title{font-weight:500;font-size:1rem;margin-bottom:.5rem}@media (min-width: 320px) and (max-width: 640px){ax-loading-dialog{width:93vw!important}}@media (min-width: 768px){ax-loading-dialog{width:55vw!important}}@media (min-width: 1280px){ax-loading-dialog{width:30vw!important}}@media (min-width: 1536px){ax-loading-dialog{width:26vw!important}}ax-loading-dialog .progress-decorators{display:flex;justify-content:space-between;font-size:.875rem}ax-loading-dialog ax-footer{justify-content:flex-end;padding-top:.75rem}ax-loading-dialog .button-wrapper{display:flex;flex-wrap:wrap;gap:.5rem;align-items:center;justify-content:center}ax-loading-dialog .button-item{display:flex;flex:1}\n"] }]
        }] });

class AXLoadingDialogModule {
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.3", ngImport: i0, type: AXLoadingDialogModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
    static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "20.3.3", ngImport: i0, type: AXLoadingDialogModule, imports: [AXLoadingDialogComponent], exports: [AXLoadingDialogComponent] }); }
    static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "20.3.3", ngImport: i0, type: AXLoadingDialogModule, imports: [AXLoadingDialogComponent] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.3", ngImport: i0, type: AXLoadingDialogModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [AXLoadingDialogComponent],
                    exports: [AXLoadingDialogComponent],
                }]
        }] });

class AXLoadingDialogService {
    constructor() {
        this.overlayService = inject(AXOverlayService);
    }
    /**
     * Opens a loading dialog with the specified configuration.
     *
     * @param options Configuration for the loading dialog, including title, progress mode, and optional initial values.
     * @returns AXLoadingDialogRef - A reference with helper methods to control the dialog lifecycle and UI (close, setProgressValue, setProgressText, setDialogTitle, setProgressColor, setProgressStatus).
     */
    show(options) {
        let internalRef;
        let componentInstance = null;
        const closeDialog = () => {
            if (internalRef) {
                internalRef.overlayRef.dispose();
            }
        };
        this.overlayService
            .create(AXLoadingDialogComponent, {
            zIndexBase: 2000,
            inputs: {
                data: options,
                onClose: closeDialog,
            },
            backdrop: {
                enabled: true,
                background: true,
                closeOnClick: false, // disableClose: true equivalent
            },
            panelClass: ['ax-loading-dialog-panel', 'ax-animate-fadeIn'],
        })
            .then((overlayRef) => {
            internalRef = {
                overlayRef,
                close: closeDialog,
            };
            // Get the component instance for method calls
            if (overlayRef.instance instanceof ComponentRef) {
                componentInstance = overlayRef.instance.instance;
            }
        });
        // Return the dialog ref with methods that will work once the component is created
        return {
            close: () => closeDialog(),
            setProgressValue: (value) => componentInstance?.setProgressValue(value),
            setDialogTitle: (text) => componentInstance?.setDialogTitle(text),
            setProgressText: (text) => componentInstance?.setProgressText(text),
            setProgressStatus: (status) => componentInstance?.setProgressStatus(status),
            setProgressColor: (color) => componentInstance?.setProgressColor(color),
        };
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.3", ngImport: i0, type: AXLoadingDialogService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.3", ngImport: i0, type: AXLoadingDialogService, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.3", ngImport: i0, type: AXLoadingDialogService, decorators: [{
            type: Injectable,
            args: [{
                    providedIn: 'root',
                }]
        }] });

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

export { AXLoadingDialogComponent, AXLoadingDialogModule, AXLoadingDialogService };
//# sourceMappingURL=acorex-components-loading-dialog.mjs.map
