"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

function _tslib() {
  const data = require("tslib");

  _tslib = function () {
    return data;
  };

  return data;
}

function _jsxRuntime() {
  const data = require("react/jsx-runtime");

  _jsxRuntime = function () {
    return data;
  };

  return data;
}

function _react() {
  const data = _interopRequireWildcard(require("react"));

  _react = function () {
    return data;
  };

  return data;
}

function _clsx() {
  const data = _interopRequireDefault(require("clsx"));

  _clsx = function () {
    return data;
  };

  return data;
}

function _popperjs() {
  const data = require("@vant/popperjs");

  _popperjs = function () {
    return data;
  };

  return data;
}

var _utils = require("../utils");

var _constant = require("../utils/constant");

var _useClickAway = _interopRequireDefault(require("../hooks/use-click-away"));

var _popup = _interopRequireDefault(require("../popup"));

var _ConfigProviderContext = _interopRequireDefault(require("../config-provider/ConfigProviderContext"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const popupProps = ['overlay', 'duration', 'overlayStyle', 'overlayClass', 'closeOnClickOverlay', 'teleport', 'onClose', 'onOpen', 'onClosed', 'onOpened', 'onClickOverlay'];
const Popover = (0, _react().forwardRef)((_a, ref) => {
  var {
    children,
    className
  } = _a,
      props = (0, _tslib().__rest)(_a, ["children", "className"]);
  const {
    prefixCls,
    createNamespace
  } = (0, _react().useContext)(_ConfigProviderContext.default);
  const [bem] = createNamespace('popover', prefixCls);
  const [visible, updateShow] = (0, _react().useState)(false);
  const popper = (0, _react().useRef)(null);
  const wrapperRef = (0, _react().useRef)();
  const popoverRef = (0, _react().useRef)();

  const createPopperInstance = () => (0, _popperjs().createPopper)(wrapperRef.current, popoverRef.current.popupRef.current, {
    placement: props.placement,
    modifiers: [{
      name: 'computeStyles',
      options: {
        adaptive: false,
        gpuAcceleration: false
      }
    }, (0, _utils.extend)({}, _popperjs().offsetModifier, {
      options: {
        offset: props.offset
      }
    })]
  });

  const updateLocation = () => {
    var _a;

    if (!visible) {
      return;
    }

    if (!popper.current) {
      popper.current = createPopperInstance();
    } else {
      (_a = popper.current) === null || _a === void 0 ? void 0 : _a.setOptions({
        placement: props.placement
      });
    }
  };

  const onClickWrapper = () => {
    if (props.trigger === 'click') {
      updateShow(!visible);
    }
  };

  const onClickAction = (action, index) => {
    var _a;

    if (action.disabled) {
      return;
    }

    (_a = props.onSelect) === null || _a === void 0 ? void 0 : _a.call(props, action, index);

    if (props.closeOnClickAction) {
      updateShow(false);
    }
  };

  const onClickAway = () => {
    if (props.closeOnClickOutside && (!props.overlay || props.closeOnClickOverlay)) {
      updateShow(false);
    }
  };

  const renderAction = (action, index) => {
    const {
      icon,
      text,
      color,
      disabled,
      className: actionClassname
    } = action;
    return (0, _jsxRuntime().jsxs)("div", Object.assign({
      className: (0, _clsx().default)(bem('action', {
        disabled,
        'with-icon': icon
      }), actionClassname),
      style: {
        color
      },
      onClick: () => onClickAction(action, index)
    }, {
      children: [icon ? _react().default.cloneElement(icon, {
        className: (0, _clsx().default)(bem('action-icon'))
      }) : null, (0, _jsxRuntime().jsx)("div", Object.assign({
        className: (0, _clsx().default)(bem('action-text'), _constant.BORDER_BOTTOM)
      }, {
        children: text
      }))]
    }), index);
  };

  (0, _react().useEffect)(() => {
    return () => {
      var _a;

      if (popper.current) {
        (_a = popper.current) === null || _a === void 0 ? void 0 : _a.destroy();
        popper.current = null;
      }
    };
  }, []);
  (0, _react().useEffect)(() => {
    updateLocation();
  }, [visible, props.placement]);
  (0, _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, _react().useImperativeHandle)(ref, () => ({
    show: () => {
      if (visible) {
        updateShow(false);
        setTimeout(() => updateShow(true), 0);
        return;
      }

      updateShow(true);
    },
    hide: () => updateShow(false)
  }));
  (0, _useClickAway.default)(wrapperRef, onClickAway, 'touchstart');
  return (0, _jsxRuntime().jsxs)(_jsxRuntime().Fragment, {
    children: [(0, _jsxRuntime().jsx)("span", Object.assign({
      ref: wrapperRef,
      className: (0, _clsx().default)(bem('wrapper')),
      onClick: onClickWrapper
    }, {
      children: props.reference
    })), (0, _jsxRuntime().jsxs)(_popup.default, Object.assign({
      ref: popoverRef,
      visible: visible,
      className: (0, _clsx().default)(bem([props.theme])),
      position: "",
      transition: "rv-zoom",
      lockScroll: false
    }, (0, _utils.pick)(props, popupProps), {
      children: [(0, _jsxRuntime().jsx)("div", {
        className: (0, _clsx().default)(bem('arrow'))
      }), (0, _jsxRuntime().jsx)("div", Object.assign({
        role: "menu",
        className: (0, _clsx().default)(bem('content'))
      }, {
        children: 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'
};
var _default = Popover;
exports.default = _default;