import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import React, { createContext, useContext, useReducer } from "react";
import { getModalId } from "./utils";
import { MODAL_REGISTRY, register } from "./modalHelper";
const initialState = {};
// const MODAL_STACK: string[] = [];
export const ALREADY_MOUNTED = {};
export const ModalContext = /*#__PURE__*/ createContext(initialState);
export const ModalIdContext = /*#__PURE__*/ createContext(null);
export const modalCallbacks = {};
export 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 = useContext(ModalContext);
    const visibleModalIds = Object.keys(modals).filter((id)=>!!modals[id]);
    visibleModalIds.forEach((id)=>{
        if (!MODAL_REGISTRY.has(id) && !ALREADY_MOUNTED[id]) {
            ;
            return;
        }
    });
    const components = visibleModalIds.filter((id)=>MODAL_REGISTRY.has(id)).map((id)=>{
        return {
            id,
            ...MODAL_REGISTRY.get(id)
        };
    });
    return /*#__PURE__*/ _jsx(_Fragment, {
        children: components.map((component)=>{
            return /*#__PURE__*/ _jsx(component.comp, {
                id: component.id,
                ...component.props
            }, component.id);
        })
    });
};
export const ModalProvider = (props)=>{
    const [state, dispatchOrigin] = useReducer(reducer, initialState);
    dispatch = dispatchOrigin;
    return /*#__PURE__*/ _jsxs(ModalContext.Provider, {
        value: state,
        children: [
            props.children,
            /*#__PURE__*/ _jsx(ModalContainer, {})
        ]
    });
};
function show(modal, args) {
    const modalId = getModalId(modal);
    if (typeof modal !== "string" && !MODAL_REGISTRY.has(modalId)) {
        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 = 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));
}
export const modalActions = {
    show,
    hide,
    remove,
    setStates
};

//# sourceMappingURL=modalContext.js.map