import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__chakra_ui_react__ from "@chakra-ui/react";
import * as __WEBPACK_EXTERNAL_MODULE__Modal_mjs__ from "./Modal.mjs";
// default prop values
const DEFAULT_VALUES = {
    LABEL_HEADING: 'Delete',
    MODAL_SIZE: 'lg'
};
const useConfirmModal = ({ heading = DEFAULT_VALUES.LABEL_HEADING, size = DEFAULT_VALUES.MODAL_SIZE, render, initialFocusRef })=>{
    // state for modal visibility
    const { isOpen, onOpen, onClose } = (0, __WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.useDisclosure)();
    // promises used to await the user before dismissing the modal
    const promiseRef = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)({
        resolve: (value)=>{},
        reject: (error)=>{},
        params: {}
    });
    const renderModal = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>{
        // passed to caller to cancel/close the modal
        const cancel = (error)=>{
            promiseRef.current.reject(error);
            onClose();
        };
        // passed to caller to signal a confirmation and close modal
        const confirm = (result)=>{
            promiseRef.current.resolve(result);
            onClose();
        };
        return /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__Modal_mjs__["default"], {
            heading: heading,
            isOpen: isOpen,
            onClose: cancel,
            size: size,
            initialFocusRef: initialFocusRef
        }, /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Stack, {
            spacing: 4
        }, render({
            // current visibility state of modal
            isOpen,
            // called by user when ready to dismiss the modal after confirmation
            // (allows for async operation to occur before dismissing the modal)
            // optionally accepts a result to relay back to caller
            confirm,
            // called to close modal; optionally accepts an error to relay
            // back to caller
            cancel,
            params: promiseRef.current.params
        })));
    }, [
        isOpen,
        heading,
        render,
        initialFocusRef
    ]);
    const showModal = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((params)=>new Promise((resolve, reject)=>{
            promiseRef.current = {
                resolve,
                reject,
                params
            };
            onOpen();
        }), []);
    return {
        // method to call that renders modal in the DOM
        renderModal,
        // called to open the modal; visibility of modal is
        // is managed by us, not by the user. Easy peezy!
        showModal
    };
};
/* ESM default export */ const useConfirmModal_rslib_entry_ = useConfirmModal;
export { useConfirmModal_rslib_entry_ as default };
