var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
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));
import React, { useMemo, useState, useEffect, useRef, forwardRef, useImperativeHandle, useContext } from "react";
import classnames from "classnames";
import { CSSTransition } from "react-transition-group";
import Icon from "../icon";
import Overlay from "../overlay";
import useEventListener from "../hooks/use-event-listener";
import { useLockScroll } from "../hooks/use-lock-scroll";
import { isDef } from "../utils";
import { callInterceptor } from "../utils/interceptor";
import { renderToContainer } from "../utils/dom/renderToContainer";
import ConfigProviderContext from "../config-provider/ConfigProviderContext";
import PopupContext from "./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__ */ forwardRef((props, ref) => {
  var _a;
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace("popup", prefixCls);
  const {
    round,
    visible,
    closeable,
    title,
    description,
    children,
    duration,
    closeIcon,
    position
  } = props;
  const opened = useRef(false);
  const zIndex = useRef((_a = props.zIndex) != null ? _a : globalZIndex);
  const popupRef = useRef();
  const [animatedVisible, setAnimatedVisible] = useState(visible);
  const style = useMemo(() => {
    const initStyle = __spreadValues({
      zIndex: zIndex.current
    }, props.style);
    if (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) {
      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__ */ React.createElement(Overlay, {
        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__ */ React.createElement(Icon, {
          name: closeIcon,
          className: classnames(bem("close-icon", closeIconPosition)),
          classPrefix: props.iconPrefix,
          onClick: onClickCloseIcon
        });
      }
      return /* @__PURE__ */ React.createElement("div", {
        className: classnames(bem("close-icon", closeIconPosition)),
        onClick: onClickCloseIcon
      }, closeIcon);
    }
    return null;
  };
  const renderTitle = () => {
    if (title) {
      return /* @__PURE__ */ React.createElement("div", {
        className: classnames(bem("title"))
      }, title);
    }
    return null;
  };
  const renderDescrition = () => {
    if (description) {
      return /* @__PURE__ */ React.createElement("div", {
        className: classnames(bem("description"))
      }, description);
    }
    return null;
  };
  const renderPopup = () => {
    const {
      safeAreaInsetBottom
    } = props;
    return /* @__PURE__ */ React.createElement("div", {
      ref: popupRef,
      style: __spreadProps(__spreadValues({}, style), {
        display: !animatedVisible ? "none" : void 0
      }),
      className: classnames(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__ */ React.createElement(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());
  };
  useEventListener("popstate", () => {
    if (props.closeOnPopstate) {
      close();
    }
  });
  useLockScroll(popupRef, props.visible);
  useEffect(() => {
    if (visible) {
      opened.current = true;
      setAnimatedVisible(true);
    }
  }, [visible]);
  useImperativeHandle(ref, () => ({
    popupRef
  }));
  return renderToContainer(props.teleport, /* @__PURE__ */ React.createElement(PopupContext.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;
export {
  stdin_default as default,
  sharedPopupProps
};
