"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, {
    ALREADY_MOUNTED: function() {
        return ALREADY_MOUNTED;
    },
    ModalContext: function() {
        return ModalContext;
    },
    ModalIdContext: function() {
        return ModalIdContext;
    },
    ModalProvider: function() {
        return ModalProvider;
    },
    hideModalCallbacks: function() {
        return hideModalCallbacks;
    },
    modalActions: function() {
        return modalActions;
    },
    modalCallbacks: function() {
        return modalCallbacks;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _utils = require("./utils");
const _modalHelper = require("./modalHelper");
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const initialState = {};
const ALREADY_MOUNTED = {};
const ModalContext = /*#__PURE__*/ (0, _react.createContext)(initialState);
const ModalIdContext = /*#__PURE__*/ (0, _react.createContext)(null);
const modalCallbacks = {};
const hideModalCallbacks = {};
let dispatch = ()=>{
    throw new Error("No dispatch method detected, did you embed your app with Modal.Provider?");
};
//------- reducer actions --------
/**
 * show modal
 * @param id string modalId
 * @param args Recored<string, unknown> modalArgs
 * @returns ModalAction
 */ function showModal(id, args) {
    return {
        type: "SHOW_MODAL",
        payload: {
            id,
            args
        }
    };
}
/**
 * close modal
 * @param id string modalId
 * @returns ModalAction
 */ function hideModal(id) {
    return {
        type: "HIDE_MODAL",
        payload: {
            id
        }
    };
}
function removeModal(id) {
    return {
        type: "DESTROY_MODAL",
        payload: {
            id
        }
    };
}
function setModalStates(id, states) {
    return {
        type: "SET_MODAL_STATES",
        payload: {
            id,
            states
        }
    };
}
//------- reducer actions --------
const reducer = (state, action)=>{
    const { id, args } = action.payload;
    switch(action.type){
        case "SHOW_MODAL":
            {
                return {
                    ...state,
                    [id]: {
                        ...state[id],
                        id,
                        args,
                        visible: !!ALREADY_MOUNTED[id],
                        delayVisible: !ALREADY_MOUNTED[id]
                    }
                };
            }
        case "HIDE_MODAL":
            {
                return {
                    ...state,
                    [id]: {
                        ...state[id],
                        visible: false
                    }
                };
            }
        case "DESTROY_MODAL":
            {
                const newState = {
                    ...state
                };
                delete newState[id];
                return newState;
            }
        case "SET_MODAL_STATES":
            {
                return {
                    ...state,
                    [id]: {
                        ...state[id],
                        ...action.payload.states
                    }
                };
            }
        default:
            return state;
    }
};
const ModalContainer = ()=>{
    const modals = (0, _react.useContext)(ModalContext);
    const visibleModalIds = Object.keys(modals).filter((id)=>!!modals[id]);
    visibleModalIds.forEach((id)=>{
        if (!_modalHelper.MODAL_REGISTRY.has(id) && !ALREADY_MOUNTED[id]) {
            ;
            return;
        }
    });
    const components = visibleModalIds.filter((id)=>_modalHelper.MODAL_REGISTRY.has(id)).map((id)=>{
        return {
            id,
            ..._modalHelper.MODAL_REGISTRY.get(id)
        };
    });
    return /*#__PURE__*/ (0, _jsxruntime.jsx)(_jsxruntime.Fragment, {
        children: components.map((component)=>{
            return /*#__PURE__*/ (0, _jsxruntime.jsx)(component.comp, {
                id: component.id,
                ...component.props
            }, component.id);
        })
    });
};
const ModalProvider = (props)=>{
    const [state, dispatchOrigin] = (0, _react.useReducer)(reducer, initialState);
    dispatch = dispatchOrigin;
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)(ModalContext.Provider, {
        value: state,
        children: [
            props.children,
            /*#__PURE__*/ (0, _jsxruntime.jsx)(ModalContainer, {})
        ]
    });
};
function show(modal, args) {
    const modalId = (0, _utils.getModalId)(modal);
    if (typeof modal !== "string" && !_modalHelper.MODAL_REGISTRY.has(modalId)) {
        (0, _modalHelper.register)(modalId, modal);
    }
    dispatch(showModal(modalId, args));
    if (!modalCallbacks[modalId]) {
        let theResolve;
        // `!` tell ts that theResolve will be written before it is used
        let theReject;
        const promise = new Promise((resolve, reject)=>{
            theResolve = resolve;
            theReject = reject;
        });
        modalCallbacks[modalId] = {
            resolve: theResolve,
            reject: theReject,
            promise
        };
    }
    return modalCallbacks[modalId].promise;
}
function hide(modal) {
    const modalId = (0, _utils.getModalId)(modal);
    dispatch(hideModal(modalId));
    delete modalCallbacks[modalId];
    if (!hideModalCallbacks[modalId]) {
        let theResolve;
        let theReject;
        const promise = new Promise((resolve, reject)=>{
            theResolve = resolve;
            theReject = reject;
        });
        hideModalCallbacks[modalId] = {
            resolve: theResolve,
            reject: theReject,
            promise
        };
    }
    return hideModalCallbacks[modalId].promise;
}
function remove(id) {
    dispatch(removeModal(id));
    delete modalCallbacks[id];
    delete hideModalCallbacks[id];
}
function setStates(id, states) {
    dispatch(setModalStates(id, states));
}
const modalActions = {
    show,
    hide,
    remove,
    setStates
};

//# sourceMappingURL=modalContext.js.map