var __create = Object.create;
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  default: () => stdin_default,
  sharedPopupProps: () => sharedPopupProps
});
module.exports = __toCommonJS(stdin_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_react_transition_group = require("react-transition-group");
var import_icon = __toESM(require("../icon"));
var import_overlay = __toESM(require("../overlay"));
var import_use_event_listener = __toESM(require("../hooks/use-event-listener"));
var import_use_lock_scroll = require("../hooks/use-lock-scroll");
var import_utils = require("../utils");
var import_interceptor = require("../utils/interceptor");
var import_renderToContainer = require("../utils/dom/renderToContainer");
var import_ConfigProviderContext = __toESM(require("../config-provider/ConfigProviderContext"));
var import_PopupContext = __toESM(require("./PopupContext"));
const sharedPopupProps = ["round", "zIndex", "closeable", "overlay", "overlayClass", "overlayStyle", "destroyOnClose", "mountOnEnter", "lockScroll", "duration", "transition", "closeOnClickOverlay", "closeOnPopstate", "onClickOverlay", "safeAreaInsetBottom", "onOpen", "onClose", "onOpened", "onClosed", "beforeClose"];
let globalZIndex = 2e3;
const Popup = /* @__PURE__ */ (0, import_react.forwardRef)((props, ref) => {
  var _a;
  const {
    prefixCls,
    createNamespace
  } = (0, import_react.useContext)(import_ConfigProviderContext.default);
  const [bem] = createNamespace("popup", prefixCls);
  const {
    round,
    visible,
    closeable,
    title,
    description,
    children,
    duration,
    closeIcon,
    position
  } = props;
  const opened = (0, import_react.useRef)(false);
  const zIndex = (0, import_react.useRef)((_a = props.zIndex) != null ? _a : globalZIndex);
  const popupRef = (0, import_react.useRef)();
  const [animatedVisible, setAnimatedVisible] = (0, import_react.useState)(visible);
  const style = (0, import_react.useMemo)(() => {
    const initStyle = __spreadValues({
      zIndex: zIndex.current
    }, props.style);
    if ((0, import_utils.isDef)(props.duration)) {
      const key = props.position === "center" ? "animationDuration" : "transitionDuration";
      initStyle[key] = `${props.duration}ms`;
    }
    return initStyle;
  }, [zIndex.current, props.style, props.duration]);
  const open = () => {
    var _a2;
    zIndex.current = props.zIndex !== void 0 ? +props.zIndex : globalZIndex++;
    opened.current = true;
    (_a2 = props.onOpen) == null ? void 0 : _a2.call(props);
  };
  const close = () => {
    if (opened.current) {
      (0, import_interceptor.callInterceptor)({
        interceptor: props.beforeClose,
        args: ["close"],
        done: () => {
          var _a2;
          opened.current = false;
          (_a2 = props.onClose) == null ? void 0 : _a2.call(props);
        }
      });
    }
  };
  const onClickOverlay = (event) => {
    var _a2;
    (_a2 = props.onClickOverlay) == null ? void 0 : _a2.call(props, event);
    if (props.closeOnClickOverlay) {
      close();
    }
  };
  const renderOverlay = () => {
    if (props.overlay) {
      return /* @__PURE__ */ import_react.default.createElement(import_overlay.default, {
        visible,
        lockScroll: props.lockScroll,
        className: props.overlayClass,
        customStyle: props.overlayStyle,
        zIndex: zIndex.current,
        duration,
        onClick: onClickOverlay
      });
    }
    return null;
  };
  const onClickCloseIcon = (e) => {
    if (props.onClickCloseIcon) {
      props.onClickCloseIcon(e);
    }
    close();
  };
  const renderCloseIcon = () => {
    if (closeable) {
      const {
        closeIconPosition
      } = props;
      if (typeof closeIcon === "string") {
        return /* @__PURE__ */ import_react.default.createElement(import_icon.default, {
          name: closeIcon,
          className: (0, import_classnames.default)(bem("close-icon", closeIconPosition)),
          classPrefix: props.iconPrefix,
          onClick: onClickCloseIcon
        });
      }
      return /* @__PURE__ */ import_react.default.createElement("div", {
        className: (0, import_classnames.default)(bem("close-icon", closeIconPosition)),
        onClick: onClickCloseIcon
      }, closeIcon);
    }
    return null;
  };
  const renderTitle = () => {
    if (title) {
      return /* @__PURE__ */ import_react.default.createElement("div", {
        className: (0, import_classnames.default)(bem("title"))
      }, title);
    }
    return null;
  };
  const renderDescrition = () => {
    if (description) {
      return /* @__PURE__ */ import_react.default.createElement("div", {
        className: (0, import_classnames.default)(bem("description"))
      }, description);
    }
    return null;
  };
  const renderPopup = () => {
    const {
      safeAreaInsetBottom
    } = props;
    return /* @__PURE__ */ import_react.default.createElement("div", {
      ref: popupRef,
      style: __spreadProps(__spreadValues({}, style), {
        display: !animatedVisible ? "none" : void 0
      }),
      className: (0, import_classnames.default)(bem({
        round,
        [position]: position
      }), {
        "rc-safe-area-bottom": safeAreaInsetBottom
      }, props.className),
      onClick: props.onClick,
      onMouseDown: (e) => {
        if (props.preventDefaultMouseDown) {
          e.preventDefault();
        }
      }
    }, renderTitle(), renderDescrition(), children, renderCloseIcon());
  };
  const renderTransition = () => {
    const {
      transition,
      destroyOnClose,
      mountOnEnter
    } = props;
    const name = position === "center" ? `${prefixCls}-fade` : `${prefixCls}-popup-slide-${position}`;
    return /* @__PURE__ */ import_react.default.createElement(import_react_transition_group.CSSTransition, {
      in: visible,
      nodeRef: popupRef,
      timeout: {
        exit: duration
      },
      classNames: transition || name,
      mountOnEnter: !mountOnEnter,
      unmountOnExit: destroyOnClose,
      onEnter: open,
      onEntered: props.onOpened,
      onExited: () => {
        var _a2;
        setAnimatedVisible(false);
        (_a2 = props.onClosed) == null ? void 0 : _a2.call(props);
      }
    }, renderPopup());
  };
  (0, import_use_event_listener.default)("popstate", () => {
    if (props.closeOnPopstate) {
      close();
    }
  });
  (0, import_use_lock_scroll.useLockScroll)(popupRef, props.visible);
  (0, import_react.useEffect)(() => {
    if (visible) {
      opened.current = true;
      setAnimatedVisible(true);
    }
  }, [visible]);
  (0, import_react.useImperativeHandle)(ref, () => ({
    popupRef
  }));
  return (0, import_renderToContainer.renderToContainer)(props.teleport, /* @__PURE__ */ import_react.default.createElement(import_PopupContext.default.Provider, {
    value: {
      visible
    }
  }, renderOverlay(), renderTransition()));
});
Popup.defaultProps = {
  duration: 300,
  overlay: true,
  lockScroll: true,
  position: "center",
  closeIcon: "cross",
  closeIconPosition: "top-right",
  closeOnClickOverlay: true,
  preventDefaultMouseDown: false,
  teleport: () => document.body
};
Popup.displayName = "Popup";
var stdin_default = Popup;
