"use strict";
const ReactDOM = require("react-dom");
const React = require("react");
const Combination = require("../../../../../../react-focus-lock@2.9.1_react@18.2.0/node_modules/react-focus-lock/dist/es2015/Combination.js");
const index$2 = require("../../icon/react-icon/IconClose/index.js");
const classNames = require("../_util/classNames.js");
const dom = require("../_util/dom.js");
const keycode = require("../_util/keycode.js");
const index$4 = require("../Button/index.js");
const index$3 = require("../Portal/index.js");
const confirm = require("./confirm.js");
const index$1 = require("../ConfigProvider/index.js");
const iconHover = require("../_class/icon-hover.js");
const config = require("./config.js");
const is = require("../_util/is.js");
const omit = require("../_util/omit.js");
const useOverflowHidden = require("../_util/hooks/useOverflowHidden.js");
const index = require("./useModal/index.js");
const useMergeValue = require("../_util/hooks/useMergeValue.js");
const useMergeProps = require("../_util/hooks/useMergeProps.js");
const emotionReactJsxRuntime_browser_esm = require("../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js");
const CSSTransition = require("../../../../../../react-transition-group@4.4.5_biqbaboplfbrettd7655fr4n2y/node_modules/react-transition-group/esm/CSSTransition.js");
const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const React__default = /* @__PURE__ */ _interopDefaultLegacy(React);
var __assign = globalThis && globalThis.__assign || function() {
  __assign = Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s)
        if (Object.prototype.hasOwnProperty.call(s, p))
          t[p] = s[p];
    }
    return t;
  };
  return __assign.apply(this, arguments);
};
var __rest = globalThis && globalThis.__rest || function(s, e) {
  var t = {};
  for (var p in s)
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
      t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function")
    for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
      if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
        t[p[i]] = s[p[i]];
    }
  return t;
};
var __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = {
      error
    };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
var cursorPosition = null;
var globalDialogIndex = 0;
if (!dom.isServerRendering) {
  document.documentElement.addEventListener("click", function(e) {
    cursorPosition = {
      left: e.clientX,
      top: e.clientY
    };
    setTimeout(function() {
      cursorPosition = null;
    }, 100);
  }, true);
}
var defaultProps = {
  mask: true,
  maskClosable: true,
  mountOnEnter: true,
  escToExit: true,
  getPopupContainer: function() {
    return document.body;
  },
  alignCenter: true
};
function Modal(baseProps, ref) {
  var _a, _b;
  var _c;
  var context = React.useContext(index$1.ConfigContext);
  var props = useMergeProps(baseProps, defaultProps, (_c = context.componentConfig) === null || _c === void 0 ? void 0 : _c.Modal);
  var className = props.className, style = props.style, visible = props.visible, simple = props.simple, title = props.title, children = props.children, cancelText = props.cancelText, okText = props.okText, okButtonProps = props.okButtonProps, cancelButtonProps = props.cancelButtonProps, _d = props.getPopupContainer, getPopupContainer = _d === void 0 ? function() {
    return document.body;
  } : _d, footer = props.footer, afterClose = props.afterClose, confirmLoading = props.confirmLoading, mountOnEnter = props.mountOnEnter, unmountOnExit = props.unmountOnExit, afterOpen = props.afterOpen, hideCancel = props.hideCancel, autoFocus = props.autoFocus, focusLock = props.focusLock, maskClosable = props.maskClosable, mask = props.mask, alignCenter = props.alignCenter, getChildrenPopupContainer = props.getChildrenPopupContainer, wrapClassName = props.wrapClassName, escToExit = props.escToExit, modalRender = props.modalRender, maskStyle = props.maskStyle, wrapStyle = props.wrapStyle, closeIcon = props.closeIcon, rest = __rest(props, ["className", "style", "visible", "simple", "title", "children", "cancelText", "okText", "okButtonProps", "cancelButtonProps", "getPopupContainer", "footer", "afterClose", "confirmLoading", "mountOnEnter", "unmountOnExit", "afterOpen", "hideCancel", "autoFocus", "focusLock", "maskClosable", "mask", "alignCenter", "getChildrenPopupContainer", "wrapClassName", "escToExit", "modalRender", "maskStyle", "wrapStyle", "closeIcon"]);
  var modalWrapperRef = React.useRef(null);
  var contentWrapper = React.useRef(null);
  var _e = __read(React.useState(), 2), wrapperVisible = _e[0], setWrapperVisible = _e[1];
  var _f = __read(React.useState(), 2), popupZIndex = _f[0], setPopupZIndex = _f[1];
  var cursorPositionRef = React.useRef(null);
  var haveOriginTransformOrigin = React.useRef(false);
  var maskClickRef = React.useRef(false);
  var dialogIndex = React.useRef();
  if (!dialogIndex.current) {
    dialogIndex.current = globalDialogIndex++;
  }
  var _g = __read(useMergeValue(false, {
    defaultValue: false,
    value: confirmLoading
  }), 2), loading = _g[0], setLoading = _g[1];
  var prefixCls = context.getPrefixCls("modal", props.prefixCls);
  var locale = context.locale, rtl = context.rtl;
  var defaultClosable = !simple;
  var closable = "closable" in props ? props.closable : defaultClosable;
  var getContainer = React.useCallback(function() {
    return ReactDOM.findDOMNode(getPopupContainer());
  }, [getPopupContainer]);
  useOverflowHidden(getContainer, {
    hidden: visible && mask
  });
  var onCancel = function() {
    props.onCancel && props.onCancel();
  };
  var onEscExit = function(event) {
    if (escToExit && visible && event.key === keycode.Esc.key) {
      event.stopPropagation();
      onCancel();
    }
  };
  var inExit = React.useRef(false);
  var onClickMask = function(e) {
    if (!maskClickRef.current)
      return;
    maskClickRef.current = false;
    if (!inExit.current && maskClosable && mask && e.target === e.currentTarget) {
      setTimeout(function() {
        onCancel();
      }, 100);
    }
  };
  var onConfirmModal = function(e) {
    var onConfirm = props.onConfirm, onOk = props.onOk;
    var _onConfirm = onOk || onConfirm;
    var ret;
    if (_onConfirm) {
      ret = _onConfirm(e);
    }
    if (ret && ret.then) {
      setLoading(true);
      ret.then(function() {
        setLoading(false);
      }, function(e2) {
        setLoading(false);
        console.error(e2);
      });
    }
  };
  React.useEffect(function() {
    var timer = null;
    if (escToExit) {
      timer = setTimeout(function() {
        var _a2;
        (_a2 = modalWrapperRef.current) === null || _a2 === void 0 ? void 0 : _a2.focus();
      });
    }
    return function() {
      timer && clearTimeout(timer);
    };
  }, [visible, escToExit]);
  React.useEffect(function() {
    var _a2;
    if (visible && popupZIndex === void 0) {
      if (modalWrapperRef.current) {
        var zIndex = +((_a2 = window.getComputedStyle(modalWrapperRef.current, null)) === null || _a2 === void 0 ? void 0 : _a2.zIndex);
        if (!isNaN(zIndex)) {
          setPopupZIndex(zIndex + 1);
        }
      }
    }
  }, [visible, popupZIndex]);
  var renderFooter = function() {
    if (footer === null)
      return;
    var cancelButtonNode = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$4, {
      ...__assign({
        onClick: onCancel
      }, cancelButtonProps),
      children: cancelText || locale.Modal.cancelText
    });
    var okButtonNode = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$4, {
      ...__assign({
        loading,
        onClick: onConfirmModal,
        type: "primary"
      }, okButtonProps),
      children: okText || locale.Modal.okText
    });
    var footerContent = is.isFunction(footer) ? footer(cancelButtonNode, okButtonNode) : footer || /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs(emotionReactJsxRuntime_browser_esm.Fragment, {
      children: [!hideCancel && cancelButtonNode, okButtonNode]
    });
    return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
      className: prefixCls + "-footer",
      children: footerContent
    });
  };
  var globalFocusLockConfig = context.focusLock.modal;
  var globalFocusLock = !!globalFocusLockConfig;
  var globalAutoFocus = is.isObject(globalFocusLockConfig) && globalFocusLockConfig.autoFocus;
  var innerFocusLock = focusLock !== void 0 ? focusLock : globalFocusLock;
  var innerAutoFocus = autoFocus !== void 0 ? autoFocus : globalAutoFocus;
  var element = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs(index$1.default, {
    ...__assign({}, context, {
      prefixCls: props.prefixCls || context.prefixCls,
      locale,
      zIndex: popupZIndex || 1050,
      getPopupContainer: function(node) {
        return typeof getChildrenPopupContainer === "function" ? getChildrenPopupContainer(node) : contentWrapper.current;
      }
    }),
    children: [title && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
      className: prefixCls + "-header",
      children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
        className: prefixCls + "-title",
        id: "arco-dialog-" + dialogIndex.current,
        children: title
      })
    }), /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
      ref: contentWrapper,
      className: prefixCls + "-content",
      children
    }), renderFooter(), closable && (closeIcon !== void 0 ? /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("span", {
      onClick: onCancel,
      className: prefixCls + "-close-icon",
      children: closeIcon
    }) : /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(iconHover, {
      tabIndex: -1,
      onClick: onCancel,
      className: prefixCls + "-close-icon",
      role: "button",
      "aria-label": "Close",
      children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$2, {})
    }))]
  });
  var ariaProps = title ? {
    "aria-labelledby": "arco-dialog-" + dialogIndex.current
  } : {};
  var modalDom = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
    ...__assign({
      role: "dialog"
    }, ariaProps, {
      className: classNames(prefixCls, (_a = {}, _a[prefixCls + "-simple"] = simple, _a[prefixCls + "-rtl"] = rtl, _a), className),
      style
    }),
    children: innerFocusLock ? /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(Combination, {
      crossFrame: false,
      disabled: !visible,
      autoFocus: innerAutoFocus,
      lockProps: {
        tabIndex: -1,
        onKeyDown: onEscExit
      },
      children: element
    }) : element
  });
  var setTransformOrigin = function(e) {
    if (haveOriginTransformOrigin.current)
      return;
    var transformOrigin = "";
    if (cursorPositionRef.current) {
      var eRect = e.getBoundingClientRect();
      var _a2 = cursorPositionRef.current, left = _a2.left, top_1 = _a2.top;
      transformOrigin = left - eRect.left + "px " + (top_1 - eRect.top) + "px";
    }
    e.style.transformOrigin = transformOrigin;
  };
  return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$3, {
    visible,
    forceRender: !mountOnEnter,
    getContainer: getPopupContainer,
    children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("div", {
      ref,
      children: [mask ? /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(CSSTransition, {
        in: visible,
        timeout: 400,
        appear: true,
        mountOnEnter,
        classNames: "fadeModal",
        unmountOnExit,
        onEnter: function(e) {
          e.style.display = "block";
        },
        onExited: function(e) {
          e.style.display = "none";
        },
        children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
          "aria-hidden": true,
          className: prefixCls + "-mask",
          style: maskStyle
        })
      }) : null, /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
        ...__assign({}, omit(rest, ["content", "icon", "showIcon", "isNotice", "noticeType", "onCancel", "onOk", "onConfirm", "closable", "prefixCls"]), {
          tabIndex: !innerFocusLock || !innerAutoFocus ? -1 : null,
          ref: modalWrapperRef,
          className: classNames(prefixCls + "-wrapper", (_b = {}, _b[prefixCls + "-wrapper-no-mask"] = !mask, _b[prefixCls + "-wrapper-align-center"] = alignCenter, _b[prefixCls + "-wrapper-rtl"] = rtl, _b), wrapClassName),
          style: __assign(__assign({}, wrapStyle || {}), {
            display: visible || wrapperVisible ? "block" : "none",
            overflow: !visible && wrapperVisible ? "hidden" : ""
          }),
          onKeyDown: !innerFocusLock || !innerAutoFocus ? onEscExit : null,
          onMouseDown: function(e) {
            maskClickRef.current = e.target === e.currentTarget;
          },
          onClick: onClickMask
        }),
        children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(CSSTransition, {
          in: visible,
          timeout: 400,
          appear: true,
          classNames: "zoomModal",
          unmountOnExit,
          mountOnEnter,
          onEnter: function(e) {
            setWrapperVisible(true);
            cursorPositionRef.current = cursorPosition;
            haveOriginTransformOrigin.current = !!e.style.transformOrigin;
            setTransformOrigin(e);
          },
          onEntered: function(e) {
            setTransformOrigin(e);
            cursorPositionRef.current = null;
            afterOpen && afterOpen();
          },
          onExit: function() {
            inExit.current = true;
          },
          onExited: function(e) {
            setWrapperVisible(false);
            setTransformOrigin(e);
            afterClose && afterClose();
            inExit.current = false;
          },
          children: React__default.default.cloneElement(is.isFunction(modalRender) ? modalRender(modalDom) : modalDom, {
            onMouseDown: function() {
              maskClickRef.current = false;
            },
            onMouseUp: function() {
              maskClickRef.current = false;
            }
          })
        })
      })]
    })
  });
}
var ExportedModalComponent = React.forwardRef(Modal);
ExportedModalComponent.displayName = "Modal";
ExportedModalComponent.config = config.setModalConfig;
ExportedModalComponent.confirm = function(props) {
  return confirm.default(props);
};
ExportedModalComponent.useModal = index;
["info", "success", "warning", "error"].forEach(function(type) {
  ExportedModalComponent[type] = function(props) {
    return confirm.default(__assign(__assign({}, props), {
      isNotice: true,
      noticeType: type
    }));
  };
});
ExportedModalComponent.destroyAll = function() {
  while (config.destroyList.length) {
    var close_1 = config.destroyList.pop();
    if (close_1) {
      close_1();
    }
  }
};
const Modal$1 = ExportedModalComponent;
module.exports = Modal$1;
//# sourceMappingURL=modal.js.map
