"use strict";
var import_Modal = require("../Modal.css");
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const antd = require("antd");
const React = require("react");
const kit_Button = require("./Button.cjs");
const kit_Icon = require("./Icon.cjs");
const kit_Spinner = require("./Spinner.cjs");
const kit_Theme_UI = require("./Theme/UI.cjs");
require("./Theme/themeUtils.cjs");
require("./Theme/themes.cjs");
const kit_utils_error = require("./utils/error.cjs");
const modalContent = "Modal_modalContent_f1929";
const modalBody = "Modal_modalBody_f1929";
const header = "Modal_header_f1929";
const headerTitle = "Modal_headerTitle_f1929";
const headerLink = "Modal_headerLink_f1929";
const dangerIcon = "Modal_dangerIcon_f1929";
const footer = "Modal_footer_f1929";
const footerLink = "Modal_footerLink_f1929";
const buttons = "Modal_buttons_f1929";
const wrapper = "Modal_wrapper_f1929";
const css = {
  modalContent,
  modalBody,
  header,
  headerTitle,
  headerLink,
  dangerIcon,
  footer,
  footerLink,
  buttons,
  wrapper
};
const modalWidths = {
  large: 1025,
  medium: 692,
  small: 358
};
const DEFAULT_CANCEL_LABEL = "Cancel";
const ModalContext = React.createContext(null);
const Modal = ({
  cancel,
  cancelText,
  danger,
  footer: footer2,
  footerLink: footerLink2,
  headerLink: headerLink2,
  icon,
  key,
  onClose,
  size = "large",
  submit,
  title,
  children: modalBody2
}) => {
  const modalContext = React.useContext(ModalContext);
  const {
    themeSettings: { className: themeClass }
  } = kit_Theme_UI.useTheme();
  if (modalContext === null) {
    throw new Error("Modal used outside of ModalContext");
  }
  const classes = [css.modalContent, themeClass];
  const { isOpen, setIsOpen } = modalContext;
  const [isSubmitting, setIsSubmitting] = React.useState(false);
  const close = React.useCallback(() => {
    setIsOpen(false);
    onClose == null ? void 0 : onClose();
  }, [setIsOpen, onClose]);
  const handleSubmit = React.useCallback(
    async (e) => {
      var _a;
      setIsSubmitting(true);
      try {
        await new Promise((resolve) => setTimeout(resolve));
        await (submit == null ? void 0 : submit.handler(e));
        setIsSubmitting(false);
        setIsOpen(false);
        await ((_a = submit == null ? void 0 : submit.onComplete) == null ? void 0 : _a.call(submit));
      } catch (err) {
        submit == null ? void 0 : submit.handleError(err, {
          level: kit_utils_error.ErrorLevel.Error,
          publicMessage: err instanceof Error ? err.message : "",
          publicSubject: "Could not submit form",
          silent: false,
          type: kit_utils_error.ErrorType.Server
        });
        setIsSubmitting(false);
      }
    },
    [submit, setIsOpen]
  );
  const stopEventPropagation = (e) => e.stopPropagation();
  return /* @__PURE__ */ jsxRuntime.jsx(
    "div",
    {
      className: css.wrapper,
      onClick: stopEventPropagation,
      onContextMenu: stopEventPropagation,
      children: /* @__PURE__ */ jsxRuntime.jsx(
        antd.Modal,
        {
          cancelText,
          className: classes.join(" "),
          closeIcon: /* @__PURE__ */ jsxRuntime.jsx(kit_Icon.default, { name: "close", size: "small", title: "Close modal" }),
          footer: /* @__PURE__ */ jsxRuntime.jsx("div", { className: css.footer, children: footer2 ?? /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
            /* @__PURE__ */ jsxRuntime.jsx("div", { className: css.footerLink, children: footerLink2 }),
            /* @__PURE__ */ jsxRuntime.jsxs("div", { className: css.buttons, children: [
              (cancel || cancelText) && /* @__PURE__ */ jsxRuntime.jsx(kit_Button, { onClick: close, children: cancelText || DEFAULT_CANCEL_LABEL }, "back"),
              /* @__PURE__ */ jsxRuntime.jsx(
                kit_Button,
                {
                  danger,
                  disabled: !!(submit == null ? void 0 : submit.disabled),
                  form: submit == null ? void 0 : submit.form,
                  htmlType: (submit == null ? void 0 : submit.form) ? "submit" : "button",
                  loading: isSubmitting,
                  tooltip: (submit == null ? void 0 : submit.disabled) ? "Address validation errors before proceeding" : void 0,
                  type: "primary",
                  onClick: handleSubmit,
                  children: (submit == null ? void 0 : submit.text) ?? "OK"
                },
                "submit"
              )
            ] })
          ] }) }),
          maskClosable: true,
          open: isOpen,
          title: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: css.header, children: [
            danger ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: css.dangerIcon, children: /* @__PURE__ */ jsxRuntime.jsx(kit_Icon.default, { name: "warning", size: "large", title: "Danger" }) }) : icon && /* @__PURE__ */ jsxRuntime.jsx(kit_Icon.default, { decorative: true, name: icon, size: "large" }),
            /* @__PURE__ */ jsxRuntime.jsx("div", { className: css.headerTitle, children: title }),
            /* @__PURE__ */ jsxRuntime.jsx("div", { className: css.headerLink, children: headerLink2 })
          ] }),
          width: modalWidths[size],
          onCancel: close,
          onOk: handleSubmit,
          children: /* @__PURE__ */ jsxRuntime.jsx(kit_Spinner, { spinning: isSubmitting, children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: css.modalBody, children: modalBody2 }) })
        },
        key
      )
    }
  );
};
const useModal = (Comp) => {
  const [isOpen, setIsOpen] = React.useState(false);
  const handleOpen = React.useCallback(() => setIsOpen(true), []);
  const handleClose = React.useCallback(() => {
    setIsOpen(false);
  }, []);
  const Component = React.useCallback(
    (props) => {
      return /* @__PURE__ */ jsxRuntime.jsx(ModalContext.Provider, { value: { isOpen, setIsOpen }, children: /* @__PURE__ */ jsxRuntime.jsx(Comp, { ...props }) });
    },
    [Comp, isOpen]
  );
  return { close: handleClose, Component, open: handleOpen };
};
exports.DEFAULT_CANCEL_LABEL = DEFAULT_CANCEL_LABEL;
exports.Modal = Modal;
exports.useModal = useModal;
