import { useEffect } from 'react';
import { useBodyScrollLock } from '../core';
/** State management for nested modals. */
class ModalManager {
    modals = [];
    add = (modal) => this.modals.push(modal);
    remove = (modal) => (this.modals = this.modals.filter(m => m !== modal));
    isTopModal = (modal) => {
        const topmostModal = this.modals[this.modals.length - 1];
        return topmostModal === modal;
    };
}
export const manager = new ModalManager();
/** Refs of new modals will be automatically added/removed from the state when rendering. */
export function useModalManager(ref, isOpen, disableScrollLock) {
    const { setIsScrollLocked } = useBodyScrollLock();
    useEffect(() => {
        if (isOpen) {
            manager.add(ref);
            if (!disableScrollLock)
                setIsScrollLocked(true);
        }
        return () => {
            manager.remove(ref);
            if (!disableScrollLock)
                setIsScrollLocked(false);
        };
    }, [isOpen, ref]);
}
//# sourceMappingURL=modalManager.js.map