import { jsxs, Fragment, jsx } from "react/jsx-runtime";
import React__default, { useState } from "react";
import { useModal, Modal } from "./Modal.js";
const DEFAULT_CONFIRM_TITLE = "Confirm Action";
const DEFAULT_CONFIRM_LABEL = "Confirm";
const DEFAULT_CONTENT = "Are you sure?";
const ConfirmModal = ({
  cancelText,
  content,
  danger = false,
  size = "small",
  title,
  okText,
  onClose,
  onConfirm,
  onError
}) => {
  return /* @__PURE__ */ jsx(
    Modal,
    {
      cancel: true,
      cancelText,
      danger,
      icon: "warning",
      size,
      submit: {
        handleError: onError,
        handler: onConfirm,
        text: okText ?? DEFAULT_CONFIRM_LABEL
      },
      title: title ?? DEFAULT_CONFIRM_TITLE,
      onClose,
      children: /* @__PURE__ */ jsx("div", { children: content })
    }
  );
};
const voidFn = () => {
};
const voidPromiseFn = async () => {
};
const ConfirmationContext = React__default.createContext(null);
const ConfirmationProvider = ({ children }) => {
  const [modalProps, setModalProps] = useState();
  const Modal2 = useModal(ConfirmModal);
  const contextValue = ({
    cancelText,
    content = DEFAULT_CONTENT,
    danger = false,
    okText,
    size = "small",
    title,
    onClose = voidFn,
    onConfirm = voidPromiseFn,
    onError = voidFn
  }) => {
    setModalProps({
      cancelText,
      content,
      danger,
      okText,
      onClose,
      onConfirm,
      onError,
      size,
      title
    });
    Modal2.open();
  };
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    React__default.useMemo(
      () => /* @__PURE__ */ jsx(ConfirmationContext.Provider, { value: contextValue, children }),
      /* eslint-disable-next-line react-hooks/exhaustive-deps */
      [children]
    ),
    /* @__PURE__ */ jsx(
      Modal2.Component,
      {
        ...modalProps,
        onConfirm: (modalProps == null ? void 0 : modalProps.onConfirm) ?? voidPromiseFn,
        onError: (modalProps == null ? void 0 : modalProps.onError) ?? voidFn
      }
    )
  ] });
};
const useConfirm = () => {
  const context = React__default.useContext(ConfirmationContext);
  if (context === null) {
    throw new Error("Attempted to use confirmation modal outside of ConfirmationContext");
  }
  return context;
};
export {
  ConfirmationProvider,
  DEFAULT_CONFIRM_LABEL,
  DEFAULT_CONFIRM_TITLE,
  DEFAULT_CONTENT,
  useConfirm as default,
  voidFn,
  voidPromiseFn
};
