"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = exports.PopoverMenu = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireWildcard(require("react"));
var _DropdownBase = _interopRequireDefault(require("../DropdownBase"));
var _PopoverMenuSt = require("./PopoverMenu.st.css.js");
var _ListItemSection = require("../ListItemSection");
var _PopoverMenu = require("./PopoverMenu.constants");
var _PopoverNext = require("../PopoverNext/PopoverNext.constants");
var _mergeRefs = require("../utils/mergeRefs");
var _excluded = ["type"],
  _excluded2 = ["id", "disabled", "dataHook", "skin", "subtitle", "title"];
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/PopoverMenu/PopoverMenu.tsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(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 (var _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); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var DEFAULT_MAX_WIDTH_PX = 204;
var DEFAULT_MIN_WIDTH_PX = 144;

/** PopoverMenu */
var PopoverMenu = exports.PopoverMenu = /*#__PURE__*/(0, _react.forwardRef)(function PopoverMenu(props, forwardedRef) {
  var {
    maxWidth = DEFAULT_MAX_WIDTH_PX,
    minWidth = DEFAULT_MIN_WIDTH_PX,
    placement = _PopoverMenu.PLACEMENT.bottom,
    appendTo = _PopoverNext.APPEND_TO.window,
    textSize = _PopoverMenu.TEXT_SIZES.medium,
    fixed = false,
    flip = true,
    showArrow = true,
    ellipsis = false,
    maxHeight = 'auto',
    triggerElement,
    children,
    dataHook,
    moveBy,
    zIndex,
    className,
    fluid,
    onHide,
    onShow,
    fixedFooter
  } = props;
  var dropdownRef = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(forwardedRef, () => ({
    _open: () => {
      if (dropdownRef.current) {
        dropdownRef.current._open();
      }
    }
  }));
  var filterChildren = children => {
    var _React$Children$map$f, _React$Children$map;
    return (_React$Children$map$f = (_React$Children$map = _react.default.Children.map(children, child => child)) == null ? void 0 : _React$Children$map.filter(child => typeof child !== 'string' && !!child && /*#__PURE__*/_react.default.isValidElement(child))) !== null && _React$Children$map$f !== void 0 ? _React$Children$map$f : [];
  };
  var buildOptions = children => {
    return children.map((child, id) => {
      var displayName = child.type && child.type.displayName;
      if (displayName && displayName === 'PopoverMenu.Divider') {
        return {
          id,
          type: 'divider',
          dataHook: child.props.dataHook
        };
      }
      if (displayName && displayName === 'PopoverMenu.SectionTitle') {
        return {
          id,
          type: 'title',
          title: child.props.title,
          dataHook: child.props.dataHook
        };
      }
      if (displayName && displayName === 'PopoverMenu.MenuItem') {
        return {
          id,
          title: child.props.text,
          onClick: child.props.onClick,
          skin: child.props.skin,
          dataHook: child.props.dataHook,
          prefixIcon: child.props.prefixIcon,
          disabled: child.props.disabled,
          disabledDescription: child.props.disabledDescription,
          subtitle: child.props.subtitle,
          suffixIcon: child.props.suffixIcon
        };
      }
      return {
        id,
        value: child,
        type: 'custom',
        overrideStyle: true
      };
    });
  };
  var renderOptions = () => {
    var filteredChildren = filterChildren(children);
    var options = buildOptions(filteredChildren);
    return options.map(option => {
      var {
          type
        } = option,
        optionRest = (0, _objectWithoutProperties2.default)(option, _excluded);

      // Custom
      if (type === 'custom') {
        return optionRest;
      }

      // Divider
      if (type === 'divider') {
        return (0, _ListItemSection.listItemSectionBuilder)(_objectSpread(_objectSpread({}, optionRest), {}, {
          type: option.type
        }));
      }

      // SectionTitle
      if (type === 'title') {
        return (0, _ListItemSection.listItemSectionBuilder)(_objectSpread(_objectSpread({}, optionRest), {}, {
          type: option.type
        }));
      }
      var {
          id,
          disabled,
          dataHook,
          skin,
          subtitle,
          title
        } = optionRest,
        rest = (0, _objectWithoutProperties2.default)(optionRest, _excluded2);
      return _objectSpread(_objectSpread({}, rest), {}, {
        id,
        disabled,
        as: 'button',
        dataHook: dataHook || "popover-menu-".concat(id),
        skin: skin || 'standard',
        size: textSize,
        className: _PopoverMenuSt.classes.listItem,
        ellipsis,
        subtitle,
        shouldFocusWithoutScroll: true,
        value: 'action',
        title: false,
        optionTitle: title
      });
    });
  };
  var renderTriggerElement = _ref => {
    var {
      toggle,
      open,
      close,
      isOpen,
      ref
    } = _ref;
    if (!triggerElement) {
      return null;
    }
    var handleTriggerClick = e => {
      var _triggerElement$props;
      if (/*#__PURE__*/_react.default.isValidElement(triggerElement) && (_triggerElement$props = triggerElement.props) != null && _triggerElement$props.onClick) {
        triggerElement.props.onClick(e);
      }
      toggle();
    };
    var commonProps = {
      onClick: handleTriggerClick,
      ref,
      'aria-haspopup': 'menu',
      'aria-expanded': isOpen
    };

    // When cloning a React element, merge the user's ref with the internal ref
    if (/*#__PURE__*/_react.default.isValidElement(triggerElement)) {
      var elementWithRef = triggerElement;
      var userRef = elementWithRef.ref;
      var mergedRef = (0, _mergeRefs.mergeRefs)(ref, userRef);
      return /*#__PURE__*/_react.default.cloneElement(triggerElement, _objectSpread(_objectSpread({}, commonProps), {}, {
        ref: mergedRef
      }));
    }

    // When using a function, pass the ref directly
    if (typeof triggerElement === 'function') {
      return triggerElement(_objectSpread({
        toggle,
        open,
        close,
        isOpen
      }, commonProps));
    }
    return null;
  };

  /**
   * If the trigger element is disabled, the popover menu should not trigger open or close.
   * Otherwise, the popover menu should use DropdownBase's default behavior.
   */
  var getDropdownOpenProps = () => {
    // Not bulletproof. We should use popovermenu disabled prop instead.
    // TODO: introduce disabled prop for popovermenu.
    var isDropdownDisabled = false;
    if (/*#__PURE__*/_react.default.isValidElement(triggerElement)) {
      var _triggerElement$props2, _triggerElement$props3;
      isDropdownDisabled = (_triggerElement$props2 = (_triggerElement$props3 = triggerElement.props) == null ? void 0 : _triggerElement$props3.disabled) !== null && _triggerElement$props2 !== void 0 ? _triggerElement$props2 : false;
    }
    return isDropdownDisabled ? {
      open: false
    } : {};
  };
  return /*#__PURE__*/_react.default.createElement(_DropdownBase.default, (0, _extends2.default)({}, getDropdownOpenProps(), {
    ref: dropdownRef,
    className: (0, _PopoverMenuSt.st)(_PopoverMenuSt.classes.root, className),
    popoverContentClassName: (0, _PopoverMenuSt.st)(_PopoverMenuSt.classes.popoverContent, {
      withWidth: Boolean(minWidth || maxWidth)
    }),
    dataHook: dataHook,
    animate: true,
    options: renderOptions(),
    appendTo: appendTo,
    placement: placement,
    minWidth: minWidth,
    maxWidth: maxWidth,
    width: "fit-content",
    flip: flip,
    fixed: fixed,
    showArrow: showArrow,
    tabIndex: -1,
    moveBy: moveBy,
    maxHeight: maxHeight,
    zIndex: zIndex,
    fluid: fluid,
    onHide: onHide,
    onShow: onShow,
    listType: "action",
    autoFocus: true,
    fixedFooter: fixedFooter,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 237,
      columnNumber: 7
    }
  }), _ref2 => {
    var {
      toggle,
      open,
      close,
      isOpen,
      ref
    } = _ref2;
    return renderTriggerElement({
      toggle,
      open,
      close,
      isOpen,
      ref
    });
  });
});
var MenuItem = () => /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null);
MenuItem.displayName = 'PopoverMenu.MenuItem';
var Divider = () => /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null);
Divider.displayName = 'PopoverMenu.Divider';
var SectionTitle = () => /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null);
SectionTitle.displayName = 'PopoverMenu.SectionTitle';
Object.assign(PopoverMenu, {
  displayName: 'PopoverMenu',
  MenuItem,
  Divider,
  SectionTitle
});
var _default = exports.default = PopoverMenu;
//# sourceMappingURL=PopoverMenu.js.map