"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    MODAL_REGISTRY: function() {
        return MODAL_REGISTRY;
    },
    create: function() {
        return create;
    },
    register: function() {
        return register;
    },
    unregister: function() {
        return unregister;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _react = require("react");
const _useModal = require("./useModal");
const _modalContext = require("./modalContext");
const MODAL_REGISTRY = new Map();
const create = (Comp)=>{
    // return higher order component
    return ({ id, defaultVisible, keepMounted, ...props })=>{
        const { args, show } = (0, _useModal.useModal)(id);
        const modals = (0, _react.useContext)(_modalContext.ModalContext);
        const shouldMount = !!modals[id];
        (0, _react.useEffect)(()=>{
            if (defaultVisible) {
                show();
            }
            _modalContext.ALREADY_MOUNTED[id] = true;
            return ()=>{
                // if(!keepMounted){
                delete _modalContext.ALREADY_MOUNTED[id];
            // }
            };
        }, [
            id,
            show,
            defaultVisible
        ]);
        (0, _react.useEffect)(()=>{
            if (keepMounted) _modalContext.modalActions.setStates(id, {
                keepMounted: true
            });
        }, [
            id,
            keepMounted
        ]);
        const delayVisible = modals[id]?.delayVisible;
        (0, _react.useEffect)(()=>{
            if (delayVisible) {
                show(args);
            }
        }, [
            delayVisible,
            args,
            show
        ]);
        if (!shouldMount) return null;
        return /*#__PURE__*/ (0, _jsxruntime.jsx)(_modalContext.ModalIdContext.Provider, {
            value: id,
            children: /*#__PURE__*/ (0, _jsxruntime.jsx)(Comp, {
                ...props,
                ...args
            })
        });
    };
};
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
        });
    }
};
const unregister = (id)=>{
    MODAL_REGISTRY.delete(id);
};

//# sourceMappingURL=modalHelper.js.map