"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.manager = void 0;
exports.useModalManager = useModalManager;
const react_1 = require("react");
const core_1 = require("../core");
/** State management for nested modals. */
class ModalManager {
    constructor() {
        this.modals = [];
        this.add = (modal) => this.modals.push(modal);
        this.remove = (modal) => (this.modals = this.modals.filter(m => m !== modal));
        this.isTopModal = (modal) => {
            const topmostModal = this.modals[this.modals.length - 1];
            return topmostModal === modal;
        };
    }
}
exports.manager = new ModalManager();
/** Refs of new modals will be automatically added/removed from the state when rendering. */
function useModalManager(ref, isOpen, disableScrollLock) {
    const { setIsScrollLocked } = (0, core_1.useBodyScrollLock)();
    (0, react_1.useEffect)(() => {
        if (isOpen) {
            exports.manager.add(ref);
            if (!disableScrollLock)
                setIsScrollLocked(true);
        }
        return () => {
            exports.manager.remove(ref);
            if (!disableScrollLock)
                setIsScrollLocked(false);
        };
    }, [isOpen, ref]);
}
//# sourceMappingURL=modalManager.js.map