"use strict";

exports.__esModule = true;
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _clsx = _interopRequireDefault(require("clsx"));

var _popperjs = require("@vant/popperjs");

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"));

var _excluded = ["children", "className"];

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 _getRequireWildcardCache(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; }

function _extends() { _extends = Object.assign || 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); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var popupProps = ['overlay', 'duration', 'overlayStyle', 'overlayClass', 'closeOnClickOverlay', 'teleport', 'onClose', 'onOpen', 'onClosed', 'onOpened', 'onClickOverlay'];
var Popover = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
  var children = _ref.children,
      className = _ref.className,
      props = _objectWithoutPropertiesLoose(_ref, _excluded);

  var _useContext = (0, _react.useContext)(_ConfigProviderContext.default),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

  var _createNamespace = createNamespace('popover', prefixCls),
      bem = _createNamespace[0];

  var _useState = (0, _react.useState)(false),
      visible = _useState[0],
      updateShow = _useState[1];

  var popper = (0, _react.useRef)(null);
  var wrapperRef = (0, _react.useRef)();
  var popoverRef = (0, _react.useRef)();

  var createPopperInstance = function createPopperInstance() {
    return (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
        }
      })]
    });
  };

  var updateLocation = function updateLocation() {
    if (!visible) {
      return;
    }

    if (!popper.current) {
      popper.current = createPopperInstance();
    } else {
      var _popper$current;

      (_popper$current = popper.current) == null ? void 0 : _popper$current.setOptions({
        placement: props.placement
      });
    }
  };

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

  var onClickAction = function onClickAction(action, index) {
    if (action.disabled) {
      return;
    }

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

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

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

  var renderAction = function renderAction(action, index) {
    var icon = action.icon,
        text = action.text,
        color = action.color,
        disabled = action.disabled,
        actionClassname = action.className;
    return /*#__PURE__*/_react.default.createElement("div", {
      // role="menuitem"
      key: index,
      className: (0, _clsx.default)(bem('action', {
        disabled: disabled,
        'with-icon': icon
      }), actionClassname),
      style: {
        color: color
      },
      onClick: function onClick() {
        return onClickAction(action, index);
      }
    }, icon ? /*#__PURE__*/_react.default.cloneElement(icon, {
      className: (0, _clsx.default)(bem('action-icon'))
    }) : null, /*#__PURE__*/_react.default.createElement("div", {
      className: (0, _clsx.default)(bem('action-text'), _constant.BORDER_BOTTOM)
    }, text));
  };

  (0, _react.useEffect)(function () {
    return function () {
      if (popper.current) {
        var _popper$current2;

        (_popper$current2 = popper.current) == null ? void 0 : _popper$current2.destroy();
        popper.current = null;
      }
    };
  }, []);
  (0, _react.useEffect)(function () {
    updateLocation();
  }, [visible, props.placement]);
  (0, _react.useEffect)(function () {
    var popupTarget;

    var prevent = function prevent(e) {
      return e.stopPropagation();
    };

    if (popoverRef.current && popoverRef.current.popupRef.current) {
      popupTarget = popoverRef.current.popupRef.current;
      popupTarget.addEventListener('touchstart', prevent);
    }

    return function () {
      if (popupTarget) popupTarget.removeEventListener('touchstart', prevent);
    };
  }, [popoverRef.current]);
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      show: function show() {
        if (visible) {
          updateShow(false);
          setTimeout(function () {
            return updateShow(true);
          }, 0);
          return;
        }

        updateShow(true);
      },
      hide: function hide() {
        return updateShow(false);
      }
    };
  });
  (0, _useClickAway.default)(wrapperRef, onClickAway, 'touchstart');
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("span", {
    ref: wrapperRef,
    className: (0, _clsx.default)(bem('wrapper')),
    onClick: onClickWrapper
  }, props.reference), /*#__PURE__*/_react.default.createElement(_popup.default, _extends({
    ref: popoverRef,
    visible: visible,
    className: (0, _clsx.default)(bem([props.theme])),
    position: "",
    transition: "rv-zoom",
    lockScroll: false
  }, (0, _utils.pick)(props, popupProps)), /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem('arrow'))
  }), /*#__PURE__*/_react.default.createElement("div", {
    role: "menu",
    className: (0, _clsx.default)(bem('content'))
  }, 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;