"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "useModal", {
    enumerable: true,
    get: function() {
        return useModal;
    }
});
const _react = require("react");
const _modalContext = require("./modalContext");
const _utils = require("./utils");
const _modalHelper = require("./modalHelper");
function useModal(modal, args) {
    const modals = (0, _react.useContext)(_modalContext.ModalContext);
    const modalIdFromContext = (0, _react.useContext)(_modalContext.ModalIdContext);
    let modalId;
    let isComponent = modal && typeof modal !== "string";
    if (!modal) {
        modalId = modalIdFromContext;
    } else {
        modalId = (0, _utils.getModalId)(modal);
    }
    if (!modalId) {
        throw new Error("modalId is required");
    }
    const id = modalId;
    (0, _react.useEffect)(()=>{
        if (isComponent) {
            (0, _modalHelper.register)(id, modal, args);
        }
    }, [
        isComponent,
        modalId,
        modal,
        args
    ]);
    const modalInfo = modals[id];
    const show = (0, _react.useCallback)((args)=>_modalContext.modalActions.show(id, args), [
        id
    ]);
    const hide = (0, _react.useCallback)(()=>_modalContext.modalActions.hide(id), [
        id
    ]);
    const remove = (0, _react.useCallback)(()=>_modalContext.modalActions.remove(id), [
        id
    ]);
    const onOpenChange = (0, _react.useCallback)((isOpen)=>{
        isOpen ? ()=>{} : hide();
    }, [
        id
    ]);
    const setStates = (0, _react.useCallback)((states)=>_modalContext.modalActions.setStates(id, states), [
        id
    ]);
    const resolve = (0, _react.useCallback)((args)=>{
        _modalContext.modalCallbacks[id]?.resolve(args);
        delete _modalContext.modalCallbacks[id];
    }, [
        id
    ]);
    const reject = (0, _react.useCallback)((args)=>{
        _modalContext.modalCallbacks[id]?.reject(args);
        delete _modalContext.modalCallbacks[id];
    }, [
        id
    ]);
    const resolveHide = (0, _react.useCallback)((args)=>{
        _modalContext.hideModalCallbacks[id]?.resolve(args);
        delete _modalContext.hideModalCallbacks[id];
    }, []);
    return {
        id,
        args: modalInfo?.args,
        visible: !!modalInfo?.visible,
        keepMounted: !!modalInfo?.keepMounted,
        show,
        hide,
        onOpenChange,
        setStates,
        remove,
        resolve,
        reject,
        resolveHide
    };
}

//# sourceMappingURL=useModal.js.map