import { jsx as _jsx } from "react/jsx-runtime";
import { useContext, useEffect } from "react";
import { useModal } from "./useModal";
import { ALREADY_MOUNTED, modalActions, ModalContext, ModalIdContext } from "./modalContext";
export const MODAL_REGISTRY = new Map();
export const create = (Comp)=>{
    // return higher order component
    return ({ id, defaultVisible, keepMounted, ...props })=>{
        const { args, show } = useModal(id);
        const modals = useContext(ModalContext);
        const shouldMount = !!modals[id];
        useEffect(()=>{
            if (defaultVisible) {
                show();
            }
            ALREADY_MOUNTED[id] = true;
            return ()=>{
                // if(!keepMounted){
                delete ALREADY_MOUNTED[id];
            // }
            };
        }, [
            id,
            show,
            defaultVisible
        ]);
        useEffect(()=>{
            if (keepMounted) modalActions.setStates(id, {
                keepMounted: true
            });
        }, [
            id,
            keepMounted
        ]);
        const delayVisible = modals[id]?.delayVisible;
        useEffect(()=>{
            if (delayVisible) {
                show(args);
            }
        }, [
            delayVisible,
            args,
            show
        ]);
        if (!shouldMount) return null;
        return /*#__PURE__*/ _jsx(ModalIdContext.Provider, {
            value: id,
            children: /*#__PURE__*/ _jsx(Comp, {
                ...props,
                ...args
            })
        });
    };
};
export const register = (id, comp, props)=>{
    if (!MODAL_REGISTRY.has(id)) {
        MODAL_REGISTRY.set(id, {
            comp,
            props
        });
    } else {
        MODAL_REGISTRY.set(id, {
            comp: MODAL_REGISTRY.get(id).comp,
            props
        });
    }
};
export const unregister = (id)=>{
    MODAL_REGISTRY.delete(id);
};

//# sourceMappingURL=modalHelper.js.map