'use strict';

var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var react = require('@chakra-ui/react');
require('chakra-react-select');
require('react-icons/fa');
require('../../styles/theme.cjs');
require('react-icons/bs');
require('react-router-dom');
require('react-icons/md');
var Modal = require('./Modal.cjs');
require('@chakra-ui/icons');
require('../Button/Button.cjs');
require('../Form/FormProvider.cjs');
require('react-hook-form');
require('@hookform/devtools');
require('@hookform/resolvers/yup');
require('yup');
require('../Inputs/Slider/chakra/ChakraTooltip.cjs');
require('react-icons/io');
require('../CodeBlock/CodeBlock.cjs');
require('../DataTable/DataTable.cjs');
require('../../styles/colors.cjs');
require('date-fns/locale/en-US');
require('date-fns-tz/zonedTimeToUtc');
require('react-datepicker');
require('date-fns');
require('date-fns-tz');
require('../Inputs/DateTimeInput/Calendar.cjs');
require('../Popover/PopoverContent.cjs');
require('../Popover/Popover.cjs');
require('../Badge/Badge.cjs');
require('react-icons/io5');
require('@textea/json-viewer');
require('react-markdown');
require('remark-gfm');
require('remark-emoji');

const DEFAULT_VALUES = {
  LABEL_HEADING: "Delete",
  MODAL_SIZE: "lg"
};
const useConfirmModal = ({
  heading = DEFAULT_VALUES.LABEL_HEADING,
  size = DEFAULT_VALUES.MODAL_SIZE,
  render,
  initialFocusRef
}) => {
  const { isOpen, onOpen, onClose } = react.useDisclosure();
  const promiseRef = React.useRef({
    resolve: (value) => {
    },
    reject: (error) => {
    },
    params: {}
  });
  const renderModal = React.useCallback(() => {
    const cancel = (error) => {
      promiseRef.current.reject(error);
      onClose();
    };
    const confirm = (result) => {
      promiseRef.current.resolve(result);
      onClose();
    };
    return /* @__PURE__ */ jsxRuntime.jsx(
      Modal,
      {
        heading,
        isOpen,
        onClose: cancel,
        size,
        initialFocusRef,
        children: /* @__PURE__ */ jsxRuntime.jsx(react.Stack, { spacing: 4, children: 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 = React.useCallback((params) => {
    return 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
  };
};

module.exports = useConfirmModal;
//# sourceMappingURL=useConfirmModal.cjs.map
