import "../Modal.css";
import { jsx, jsxs, Fragment } from "react/jsx-runtime";
import { Modal as Modal$1 } from "antd";
import React__default, { createContext, useContext, useState, useCallback } from "react";
import Button from "./Button.js";
import Icon from "./Icon.js";
import Spinner from "./Spinner.js";
import { useTheme } from "./Theme/UI.js";
import "./Theme/themeUtils.js";
import "./Theme/themes.js";
import { ErrorLevel, ErrorType } from "./utils/error.js";
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 = createContext(null);
const Modal = ({
  cancel,
  cancelText,
  danger,
  footer: footer2,
  footerLink: footerLink2,
  headerLink: headerLink2,
  icon,
  key,
  onClose,
  size = "large",
  submit,
  title,
  children: modalBody2
}) => {
  const modalContext = useContext(ModalContext);
  const {
    themeSettings: { className: themeClass }
  } = useTheme();
  if (modalContext === null) {
    throw new Error("Modal used outside of ModalContext");
  }
  const classes = [css.modalContent, themeClass];
  const { isOpen, setIsOpen } = modalContext;
  const [isSubmitting, setIsSubmitting] = useState(false);
  const close = useCallback(() => {
    setIsOpen(false);
    onClose == null ? void 0 : onClose();
  }, [setIsOpen, onClose]);
  const handleSubmit = 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: ErrorLevel.Error,
          publicMessage: err instanceof Error ? err.message : "",
          publicSubject: "Could not submit form",
          silent: false,
          type: ErrorType.Server
        });
        setIsSubmitting(false);
      }
    },
    [submit, setIsOpen]
  );
  const stopEventPropagation = (e) => e.stopPropagation();
  return /* @__PURE__ */ jsx(
    "div",
    {
      className: css.wrapper,
      onClick: stopEventPropagation,
      onContextMenu: stopEventPropagation,
      children: /* @__PURE__ */ jsx(
        Modal$1,
        {
          cancelText,
          className: classes.join(" "),
          closeIcon: /* @__PURE__ */ jsx(Icon, { name: "close", size: "small", title: "Close modal" }),
          footer: /* @__PURE__ */ jsx("div", { className: css.footer, children: footer2 ?? /* @__PURE__ */ jsxs(Fragment, { children: [
            /* @__PURE__ */ jsx("div", { className: css.footerLink, children: footerLink2 }),
            /* @__PURE__ */ jsxs("div", { className: css.buttons, children: [
              (cancel || cancelText) && /* @__PURE__ */ jsx(Button, { onClick: close, children: cancelText || DEFAULT_CANCEL_LABEL }, "back"),
              /* @__PURE__ */ jsx(
                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__ */ jsxs("div", { className: css.header, children: [
            danger ? /* @__PURE__ */ jsx("div", { className: css.dangerIcon, children: /* @__PURE__ */ jsx(Icon, { name: "warning", size: "large", title: "Danger" }) }) : icon && /* @__PURE__ */ jsx(Icon, { decorative: true, name: icon, size: "large" }),
            /* @__PURE__ */ jsx("div", { className: css.headerTitle, children: title }),
            /* @__PURE__ */ jsx("div", { className: css.headerLink, children: headerLink2 })
          ] }),
          width: modalWidths[size],
          onCancel: close,
          onOk: handleSubmit,
          children: /* @__PURE__ */ jsx(Spinner, { spinning: isSubmitting, children: /* @__PURE__ */ jsx("div", { className: css.modalBody, children: modalBody2 }) })
        },
        key
      )
    }
  );
};
const useModal = (Comp) => {
  const [isOpen, setIsOpen] = useState(false);
  const handleOpen = React__default.useCallback(() => setIsOpen(true), []);
  const handleClose = React__default.useCallback(() => {
    setIsOpen(false);
  }, []);
  const Component = React__default.useCallback(
    (props) => {
      return /* @__PURE__ */ jsx(ModalContext.Provider, { value: { isOpen, setIsOpen }, children: /* @__PURE__ */ jsx(Comp, { ...props }) });
    },
    [Comp, isOpen]
  );
  return { close: handleClose, Component, open: handleOpen };
};
export {
  DEFAULT_CANCEL_LABEL,
  Modal,
  useModal
};
