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);
}
import React, { forwardRef, useContext, useEffect, useImperativeHandle, useRef, useState } from "react";
import classNames from "classnames";
import { createPopper, offsetModifier } from "@vant/popperjs";
import Popup from "../popup";
import Icon from "../icon";
import { extend, pick } from "../utils";
import { BORDER_BOTTOM } from "../utils/constant";
import useClickAway from "../hooks/use-click-away";
import ConfigProviderContext from "../config-provider/ConfigProviderContext";
const popupProps = ["overlay", "duration", "overlayStyle", "overlayClass", "closeOnClickOverlay", "teleport", "onClose", "onOpen", "onClosed", "onOpened", "onClickOverlay"];
const Popover = /* @__PURE__ */ forwardRef((props, ref) => {
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace("popover", prefixCls);
  const [visible, updateShow] = useState(false);
  const popper = useRef(null);
  const wrapperRef = useRef();
  const popoverRef = useRef();
  const createPopperInstance = () => createPopper(wrapperRef.current, popoverRef.current.popupRef.current, {
    placement: props.placement,
    modifiers: [{
      name: "computeStyles",
      options: {
        adaptive: false,
        gpuAcceleration: false
      }
    }, extend({}, 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__ */ React.createElement("div", {
      key: index,
      className: classNames(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__ */ React.createElement(Icon, {
      name: icon,
      classPrefix: props.iconPrefix,
      className: classNames(bem("action-icon"))
    }), /* @__PURE__ */ React.createElement("div", {
      className: classNames(bem("action-text"), BORDER_BOTTOM)
    }, text));
  };
  useEffect(() => {
    return () => {
      var _a;
      if (popper.current) {
        (_a = popper.current) == null ? void 0 : _a.destroy();
        popper.current = null;
      }
    };
  }, []);
  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]);
  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]);
  useImperativeHandle(ref, () => ({
    show: () => {
      if (visible) {
        updateShow(false);
        setTimeout(() => updateShow(true), 0);
        return;
      }
      updateShow(true);
    },
    hide: () => updateShow(false)
  }));
  useClickAway(wrapperRef, onClickAway, "touchstart", props.closeOnClickOutside);
  return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("span", {
    ref: wrapperRef,
    className: classNames(bem("wrapper")),
    onClick: onClickWrapper
  }, props.reference), /* @__PURE__ */ React.createElement(Popup, _extends({
    ref: popoverRef,
    visible,
    className: classNames(bem([props.theme])),
    position: "",
    transition: "rc-zoom",
    lockScroll: false
  }, pick(props, popupProps)), /* @__PURE__ */ React.createElement("div", {
    className: classNames(bem("arrow"))
  }), /* @__PURE__ */ React.createElement("div", {
    role: "menu",
    className: classNames(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;
export {
  stdin_default as default
};
