"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PopoverMenu = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _popover = require("./popover");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const classPrefix = `adm-popover-menu`;
const PopoverMenu = exports.PopoverMenu = (0, _react.forwardRef)((props, ref) => {
  const innerRef = (0, _react.useRef)(null);
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  (0, _react.useImperativeHandle)(ref, () => innerRef.current, []);
  const onClick = (0, _react.useCallback)(e => {
    var _a;
    const {
      onAction
    } = props;
    if (onAction) {
      onAction(e);
    }
    (_a = innerRef.current) === null || _a === void 0 ? void 0 : _a.hide();
  }, [props.onAction]);
  const overlay = (0, _react.useMemo)(() => {
    const whetherScroll = (props === null || props === void 0 ? void 0 : props.maxCount) && props.actions.length > (props === null || props === void 0 ? void 0 : props.maxCount);
    const innerHeight = (props === null || props === void 0 ? void 0 : props.maxCount) && (props === null || props === void 0 ? void 0 : props.maxCount) * 48;
    return _react.default.createElement("div", {
      className: `${classPrefix}-list`
    }, _react.default.createElement("div", {
      className: (0, _classnames.default)(`${classPrefix}-list-inner`, {
        [`${classPrefix}-list-scroll`]: whetherScroll
      }),
      style: {
        height: innerHeight
      }
    }, props.actions.map((action, index) => {
      var _a;
      return _react.default.createElement("a", {
        key: (_a = action.key) !== null && _a !== void 0 ? _a : index,
        className: (0, _classnames.default)(`${classPrefix}-item`, 'adm-plain-anchor', {
          [`${classPrefix}-item-disabled`]: action.disabled
        }),
        onClick: () => {
          var _a;
          if (action.disabled) return;
          onClick(action);
          (_a = action.onClick) === null || _a === void 0 ? void 0 : _a.call(action);
        }
      }, action.icon && _react.default.createElement("div", {
        className: `${classPrefix}-item-icon`
      }, action.icon), _react.default.createElement("div", {
        className: `${classPrefix}-item-text`
      }, action.text));
    })));
  }, [props.actions, onClick]);
  return _react.default.createElement(_popover.Popover, Object.assign({
    ref: innerRef
  }, props, {
    className: (0, _classnames.default)(classPrefix, props.className),
    content: overlay
  }), props.children);
});