import { DrawerService } from '@masterteam/components/dynamic-drawer';
import { DialogService } from '@masterteam/components/dialog';
import { DOCUMENT } from '@angular/common';
import * as i0 from '@angular/core';
import { inject, Injectable } from '@angular/core';

class ModalService {
    drawerService = inject(DrawerService);
    dialogService = inject(DialogService);
    doc = inject(DOCUMENT);
    openRefs = new Set();
    footerClass = 'mt-modal-footer';
    contentClass = 'mt-modal-content';
    modalConfig;
    openModal(component, modalType, config) {
        let ref;
        if (modalType === 'drawer') {
            config.position = this.handelStartEndPosition(config.position);
            ref = this.drawerService.open(component, config);
        }
        else {
            ref = this.dialogService.open(component, config);
        }
        this.trackRef(ref);
        return ref;
    }
    /**
     * Closes every modal/drawer opened through this service.
     * Useful on session expiry, forced logout, or any global reset
     * where leftover popups would confuse the user.
     */
    closeAll() {
        const refs = Array.from(this.openRefs);
        this.openRefs.clear();
        for (const ref of refs) {
            try {
                ref?.close?.();
            }
            catch {
                // best-effort: ignore failures on individual refs
            }
        }
    }
    handelStartEndPosition(position) {
        const direction = this.resolveCurrentDirection();
        if (position === 'start') {
            return direction === 'rtl' ? 'right' : 'left';
        }
        else if (position === 'end') {
            return direction === 'rtl' ? 'left' : 'right';
        }
        else {
            return position || 'right';
        }
    }
    resolveCurrentDirection() {
        const root = this.doc.documentElement;
        return (this.readDirectionAttribute(root) ??
            this.normalizeDirection(root?.dir) ??
            this.readComputedDirection(root) ??
            'ltr');
    }
    readDirectionAttribute(element) {
        return this.normalizeDirection(element?.getAttribute('dir'));
    }
    readComputedDirection(element) {
        if (!element || !this.doc.defaultView?.getComputedStyle) {
            return null;
        }
        return this.normalizeDirection(this.doc.defaultView.getComputedStyle(element).direction);
    }
    normalizeDirection(value) {
        return value?.toLowerCase() === 'rtl'
            ? 'rtl'
            : value?.toLowerCase() === 'ltr'
                ? 'ltr'
                : null;
    }
    trackRef(ref) {
        if (!ref)
            return;
        this.openRefs.add(ref);
        const cleanup = () => this.openRefs.delete(ref);
        ref.onClose?.subscribe({
            next: cleanup,
            error: cleanup,
            complete: cleanup,
        });
        ref.onDestroy?.subscribe({
            next: cleanup,
            error: cleanup,
            complete: cleanup,
        });
    }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.8", ngImport: i0, type: ModalService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
    static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.8", ngImport: i0, type: ModalService, providedIn: 'root' });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.8", ngImport: i0, type: ModalService, decorators: [{
            type: Injectable,
            args: [{ providedIn: 'root' }]
        }] });

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

export { ModalService };
//# sourceMappingURL=masterteam-components-modal.mjs.map
