var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
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
});
module.exports = __toCommonJS(stdin_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_popperjs = require("@vant/popperjs");
var import_popup = __toESM(require("../popup"));
var import_icon = __toESM(require("../icon"));
var import_utils = require("../utils");
var import_constant = require("../utils/constant");
var import_use_click_away = __toESM(require("../hooks/use-click-away"));
var import_ConfigProviderContext = __toESM(require("../config-provider/ConfigProviderContext"));
function _extends() {
  _extends = Object.assign ? Object.assign.bind() : function(target) {
    for (var i = 1; i < arguments.length; i++) {
      var source = arguments[i];
      for (var key in source) {
        if (Object.prototype.hasOwnProperty.call(source, key)) {
          target[key] = source[key];
        }
      }
    }
    return target;
  };
  return _extends.apply(this, arguments);
}
const popupProps = ["overlay", "duration", "overlayStyle", "overlayClass", "closeOnClickOverlay", "teleport", "onClose", "onOpen", "onClosed", "onOpened", "onClickOverlay"];
const Popover = /* @__PURE__ */ (0, import_react.forwardRef)((props, ref) => {
  const {
    prefixCls,
    createNamespace
  } = (0, import_react.useContext)(import_ConfigProviderContext.default);
  const [bem] = createNamespace("popover", prefixCls);
  const [visible, updateShow] = (0, import_react.useState)(false);
  const popper = (0, import_react.useRef)(null);
  const wrapperRef = (0, import_react.useRef)();
  const popoverRef = (0, import_react.useRef)();
  const createPopperInstance = () => (0, import_popperjs.createPopper)(wrapperRef.current, popoverRef.current.popupRef.current, {
    placement: props.placement,
    modifiers: [{
      name: "computeStyles",
      options: {
        adaptive: false,
        gpuAcceleration: false
      }
    }, (0, import_utils.extend)({}, import_popperjs.offsetModifier, {
      options: {
        offset: props.offset
      }
    })]
  });
  const onClickWrapper = () => {
    if (props.trigger === "click") {
      updateShow(!visible);
    }
  };
  const onClickAction = (action, index) => {
    var _a;
    if (action.disabled) {
      return;
    }
    (_a = props.onSelect) == null ? void 0 : _a.call(props, action, index);
    if (props.closeOnClickAction) {
      updateShow(false);
    }
  };
  const onClickAway = () => {
    if (!props.overlay || props.closeOnClickOverlay) {
      updateShow(false);
    }
  };
  const renderAction = (action, index) => {
    const {
      icon,
      text,
      color,
      disabled,
      className: actionClassname
    } = action;
    return /* @__PURE__ */ import_react.default.createElement("div", {
      key: index,
      className: (0, import_classnames.default)(bem("action", {
        disabled,
        "with-icon": icon
      }), actionClassname),
      style: {
        color
      },
      tabIndex: disabled ? void 0 : 0,
      "aria-disabled": disabled || void 0,
      onClick: () => onClickAction(action, index)
    }, icon && /* @__PURE__ */ import_react.default.createElement(import_icon.default, {
      name: icon,
      classPrefix: props.iconPrefix,
      className: (0, import_classnames.default)(bem("action-icon"))
    }), /* @__PURE__ */ import_react.default.createElement("div", {
      className: (0, import_classnames.default)(bem("action-text"), import_constant.BORDER_BOTTOM)
    }, text));
  };
  (0, import_react.useEffect)(() => {
    return () => {
      var _a;
      if (popper.current) {
        (_a = popper.current) == null ? void 0 : _a.destroy();
        popper.current = null;
      }
    };
  }, []);
  (0, import_react.useEffect)(() => {
    const updateLocation = () => {
      var _a;
      if (!visible) {
        return;
      }
      if (!popper.current) {
        popper.current = createPopperInstance();
      } else {
        (_a = popper.current) == null ? void 0 : _a.setOptions({
          placement: props.placement
        });
      }
    };
    updateLocation();
  }, [visible, props.placement]);
  (0, import_react.useEffect)(() => {
    let popupTarget;
    const prevent = (e) => e.stopPropagation();
    if (popoverRef.current && popoverRef.current.popupRef.current) {
      popupTarget = popoverRef.current.popupRef.current;
      popupTarget.addEventListener("touchstart", prevent);
    }
    return () => {
      if (popupTarget)
        popupTarget.removeEventListener("touchstart", prevent);
    };
  }, [popoverRef.current]);
  (0, import_react.useImperativeHandle)(ref, () => ({
    show: () => {
      if (visible) {
        updateShow(false);
        setTimeout(() => updateShow(true), 0);
        return;
      }
      updateShow(true);
    },
    hide: () => updateShow(false)
  }));
  (0, import_use_click_away.default)(wrapperRef, onClickAway, "touchstart", props.closeOnClickOutside);
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("span", {
    ref: wrapperRef,
    className: (0, import_classnames.default)(bem("wrapper")),
    onClick: onClickWrapper
  }, props.reference), /* @__PURE__ */ import_react.default.createElement(import_popup.default, _extends({
    ref: popoverRef,
    visible,
    className: (0, import_classnames.default)(bem([props.theme])),
    position: "",
    transition: "rc-zoom",
    lockScroll: false
  }, (0, import_utils.pick)(props, popupProps)), /* @__PURE__ */ import_react.default.createElement("div", {
    className: (0, import_classnames.default)(bem("arrow"))
  }), /* @__PURE__ */ import_react.default.createElement("div", {
    role: "menu",
    className: (0, import_classnames.default)(bem("content"))
  }, props.children || props.actions.map(renderAction))));
});
Popover.defaultProps = {
  overlay: false,
  duration: 300,
  closeOnClickAction: true,
  closeOnClickOverlay: true,
  closeOnClickOutside: true,
  offset: [0, 8],
  theme: "light",
  trigger: "click",
  actions: [],
  placement: "bottom"
};
Popover.displayName = "Popover";
var stdin_default = Popover;
