import { AXComponent, NXComponent } from '@acorex/cdk/common';
import * as i0 from '@angular/core';
import { ChangeDetectionStrategy, ViewEncapsulation, Component, inject, signal, ComponentRef, Injectable, viewChild, TemplateRef, NgModule, input, Directive } from '@angular/core';
import { AXOverlayService } from '@acorex/cdk/overlay';
import { cloneDeep, set } from 'lodash-es';
import { Subject } from 'rxjs';

class AXModalFooterComponent {
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.3", ngImport: i0, type: AXModalFooterComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "20.3.3", type: AXModalFooterComponent, isStandalone: true, selector: "ax-modal-footer", providers: [{ provide: AXComponent, useExisting: AXModalFooterComponent }], ngImport: i0, template: "<ng-content select=\"ax-modal-footer\">\n  <ng-content select=\"ax-prefix\"></ng-content>\n  <ng-content select=\"ax-suffix\"></ng-content>\n</ng-content>\n", styles: ["ax-modal-footer{display:flex;justify-content:space-between;align-items:center;gap:.5rem}\n"], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.3", ngImport: i0, type: AXModalFooterComponent, decorators: [{
            type: Component,
            args: [{ selector: 'ax-modal-footer', imports: [], encapsulation: ViewEncapsulation.None, changeDetection: ChangeDetectionStrategy.OnPush, providers: [{ provide: AXComponent, useExisting: AXModalFooterComponent }], template: "<ng-content select=\"ax-modal-footer\">\n  <ng-content select=\"ax-prefix\"></ng-content>\n  <ng-content select=\"ax-suffix\"></ng-content>\n</ng-content>\n", styles: ["ax-modal-footer{display:flex;justify-content:space-between;align-items:center;gap:.5rem}\n"] }]
        }] });

class AXModalStateService {
    constructor() {
        this.modalList = new Map();
        this.overlayService = inject(AXOverlayService);
        this.modalOption = signal(null, ...(ngDevMode ? [{ debugName: "modalOption" }] : []));
    }
    /**
     * Opens a modal with the specified content and options.
     * @param content - Component, template, or HTML element to display
     * @param options - Configuration options for the modal
     * @returns Promise<AXModalRef> - Reference to the opened modal
     */
    async open(content, options) {
        this.modalOption.set(options);
        const randomId = Math.floor(Math.random() * 100000000000);
        const modalContentComp = (await import('./acorex-components-modal-modal-content.component-DPMt_zaI.mjs')).AXModalContentComponent;
        const onClose = new Subject();
        const returnRef = {
            close: (data) => {
                this.close(randomId, data);
            },
            setInputs: (values) => {
                this.setInputs(randomId, values);
            },
            minimize: () => {
                this.minimize(randomId);
            },
            maximize: () => {
                this.maximize(randomId);
            },
            restore: () => {
                this.restore(randomId);
            },
            bringToFront: () => {
                this.bringToFront(randomId);
            },
            onClose,
        };
        const finalInputs = { ...(cloneDeep(options?.inputs) ?? {}), ...{ __modal__: returnRef } };
        const finalOptions = set(cloneDeep(options) ?? {}, 'inputs', finalInputs);
        const ref = await this.overlayService.create(modalContentComp, {
            position: options?.position,
            backdrop: options?.backdrop,
            inputs: {
                __content__: content,
                inputs: {
                    ...finalOptions?.inputs,
                },
                id: randomId,
            },
        });
        this.modalList.set(randomId, { overlay: ref, modal: returnRef });
        return returnRef;
    }
    /**
     * Closes a modal by its ID.
     * @param id - The modal ID to close
     * @param data - Optional data to pass to the close event
     */
    async close(id, data) {
        const ref = this.modalList.get(id);
        if (!ref)
            return;
        ref.modal.onClose.next(data);
        ref.overlay.dispose();
        this.modalList.delete(id);
    }
    /**
     * Sets input values for a modal by its ID.
     * @param id - The modal ID
     * @param values - Object containing input values to set
     */
    async setInputs(id, values) {
        const ref = this.modalList.get(id).overlay.instance;
        if (!ref)
            return;
        if (ref instanceof ComponentRef) {
            const comRef = ref;
            Object.entries(values).forEach((v) => {
                comRef.setInput(v[0], v[1]);
            });
        }
        else {
            ref.context.inputs.set(values);
        }
    }
    /**
     * Minimizes a modal by its ID.
     * @param id - The modal ID to minimize
     */
    async minimize(id) {
        const ref = this.modalList.get(id).overlay.instance;
        if (!ref)
            return;
        if (ref instanceof ComponentRef) {
            ref.instance.minimize();
        }
        else {
            ref.context.minimize();
        }
    }
    /**
     * Maximizes a modal by its ID.
     * @param id - The modal ID to maximize
     */
    async maximize(id) {
        const ref = this.modalList.get(id).overlay.instance;
        if (!ref)
            return;
        if (ref instanceof ComponentRef) {
            ref.instance.maximize();
        }
        else {
            ref.context.maximize();
        }
    }
    /**
     * Restores a modal by its ID.
     * @param id - The modal ID to restore
     */
    async restore(id) {
        const ref = this.modalList.get(id).overlay.instance;
        if (!ref)
            return;
        if (ref instanceof ComponentRef) {
            ref.instance.restore();
        }
        else {
            ref.context.restore();
        }
    }
    /**
     * Gets the index of a modal in the minimized stack.
     * @param targetKey - The modal ID to find
     * @returns number - The index position in the stack
     */
    getMapIndexOf(targetKey) {
        let index = 0;
        for (const key of this.modalList.keys()) {
            if (key === targetKey) {
                return index;
            }
            index++;
        }
        return -1;
    }
    /**
     * Brings a modal to the front of all other overlays.
     * @param id - The modal ID to bring to front
     */
    bringToFront(id) {
        const ref = this.modalList.get(id);
        if (!ref)
            return;
        ref.overlay.bringToFront();
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.3", ngImport: i0, type: AXModalStateService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.3", ngImport: i0, type: AXModalStateService, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.3", ngImport: i0, type: AXModalStateService, decorators: [{
            type: Injectable,
            args: [{
                    providedIn: 'root',
                }]
        }] });

class AXModalComponent extends NXComponent {
    constructor() {
        super(...arguments);
        this.service = inject(AXModalStateService);
        this.template = viewChild(TemplateRef, ...(ngDevMode ? [{ debugName: "template" }] : []));
        this.ref = signal(null, ...(ngDevMode ? [{ debugName: "ref" }] : []));
    }
    /**
     * Opens the modal with the specified options.
     * @param options - Configuration options for the modal
     * @returns Promise<AXModalRef> - Reference to the opened modal
     */
    async open(options) {
        const ref = await this.service.open(this.template(), options);
        this.ref.set(ref);
        return ref;
    }
    /**
     * Closes the currently open modal.
     */
    async close() {
        if (this.ref())
            return;
        this.ref().close();
    }
    /**
     * Sets input values for the modal content component.
     * @param values - Object containing input values to set
     */
    async setInputs(values) {
        if (!this.ref())
            return;
        const transform = { inputs: values };
        this.ref().setInputs(transform);
    }
    /**
     * Minimizes the modal to the bottom bar.
     */
    async minimize() {
        if (!this.ref())
            return;
        this.ref().minimize();
    }
    /**
     * Maximizes the modal to fullscreen.
     */
    async maximize() {
        if (!this.ref())
            return;
        this.ref().maximize();
    }
    /**
     * Restores the modal from minimized or maximized state.
     */
    async restore() {
        if (!this.ref())
            return;
        this.ref().restore();
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.3", ngImport: i0, type: AXModalComponent, deps: null, target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.2.0", version: "20.3.3", type: AXModalComponent, isStandalone: true, selector: "ax-modal", providers: [{ provide: AXComponent, useExisting: AXModalComponent }], viewQueries: [{ propertyName: "template", first: true, predicate: TemplateRef, descendants: true, isSignal: true }], usesInheritance: true, ngImport: i0, template: `
    <ng-template #contentTemplate>
      <ng-content></ng-content>
    </ng-template>
  `, isInline: true, changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.3", ngImport: i0, type: AXModalComponent, decorators: [{
            type: Component,
            args: [{
                    selector: 'ax-modal',
                    template: `
    <ng-template #contentTemplate>
      <ng-content></ng-content>
    </ng-template>
  `,
                    encapsulation: ViewEncapsulation.None,
                    changeDetection: ChangeDetectionStrategy.OnPush,
                    providers: [{ provide: AXComponent, useExisting: AXModalComponent }],
                }]
        }] });

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

class AXModalService {
    constructor() {
        this.stateService = inject(AXModalStateService);
        this.ref = signal(null, ...(ngDevMode ? [{ debugName: "ref" }] : []));
    }
    /**
     * Opens a modal with the specified content and options.
     * @param content - Component, template, or HTML element to display
     * @param options - Configuration options for the modal
     * @returns Promise<AXModalRef> - Reference to the opened modal
     */
    async open(content, options) {
        const ref = await this.stateService.open(content, options);
        this.ref.set(ref);
        return ref;
    }
    /**
     * Closes the currently open modal.
     */
    async close() {
        if (!this.ref())
            return;
        this.ref().close();
    }
    /**
     * Sets input values for the modal content component.
     * @param values - Object containing input values to set
     */
    async setInputs(values) {
        if (!this.ref())
            return;
        const transform = { inputs: values };
        this.ref().setInputs(transform);
    }
    /**
     * Minimizes the modal to the bottom bar.
     */
    async minimize() {
        if (!this.ref())
            return;
        this.ref().minimize();
    }
    /**
     * Maximizes the modal to fullscreen.
     */
    async maximize() {
        if (!this.ref())
            return;
        this.ref().maximize();
    }
    /**
     * Restores the modal from minimized or maximized state.
     */
    async restore() {
        if (!this.ref())
            return;
        this.ref().restore();
    }
    /**
     * Brings the modal to the front of all other overlays.
     */
    bringToFront() {
        if (!this.ref())
            return;
        this.ref().bringToFront();
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.3", ngImport: i0, type: AXModalService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.3", ngImport: i0, type: AXModalService, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.3", ngImport: i0, type: AXModalService, decorators: [{
            type: Injectable,
            args: [{
                    providedIn: 'root',
                }]
        }] });

class AXModalComponentBase {
    constructor() {
        this.__modal__ = input(...(ngDevMode ? [undefined, { debugName: "__modal__" }] : []));
    }
    close(data = null) {
        this.__modal__().close(data);
    }
    minimize() {
        this.__modal__().minimize();
    }
    restore() {
        this.__modal__().restore();
    }
    maximize() {
        this.__modal__().maximize();
    }
    bringToFront() {
        this.__modal__().bringToFront();
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.3", ngImport: i0, type: AXModalComponentBase, deps: [], target: i0.ɵɵFactoryTarget.Directive }); }
    static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "20.3.3", type: AXModalComponentBase, isStandalone: true, inputs: { __modal__: { classPropertyName: "__modal__", publicName: "__modal__", isSignal: true, isRequired: false, transformFunction: null } }, ngImport: i0 }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.3", ngImport: i0, type: AXModalComponentBase, decorators: [{
            type: Directive
        }] });

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

export { AXModalStateService as A, AXModalFooterComponent as a, AXModalComponent as b, AXModalComponentBase as c, AXModalModule as d, AXModalService as e };
//# sourceMappingURL=acorex-components-modal-acorex-components-modal-DwBY73dZ.mjs.map
