"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

var _taggedTemplateLiteralLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/taggedTemplateLiteralLoose"));

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));

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

var _omit2 = _interopRequireDefault(require("lodash/omit"));

var _Menu = _interopRequireDefault(require("../Menu/Menu"));

var _MenuItem = _interopRequireDefault(require("../Menu/MenuItem"));

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _Sidenav = require("../Sidenav/Sidenav");

var _AngleLeft = _interopRequireDefault(require("@rsuite/icons/legacy/AngleLeft"));

var _AngleRight = _interopRequireDefault(require("@rsuite/icons/legacy/AngleRight"));

var _useCustom2 = _interopRequireDefault(require("../utils/useCustom"));

var _DropdownContext = _interopRequireDefault(require("./DropdownContext"));

var _Menubar = _interopRequireDefault(require("../Menu/Menubar"));

var _SidenavDropdownMenu = _interopRequireDefault(require("../Sidenav/SidenavDropdownMenu"));

var _templateObject, _templateObject2;

/**
 * The <Dropdown.Menu> API
 *
 * @description
 * Note the difference between this component and <Menu> component:
 * <Menu> is used for ARIA menu control logic and is used internally only.
 * This component is only used for supporting submenu syntax and is
 * assigned to Dropdown.Menu
 *
 * @example
 *
 * <Dropdown>
 *   <Dropdown.Item>Item 1</Dropdown.Item>
 *   <Dropdown.Menu title="Submenu">
 *     <Dropdown.Item>Sub item</Dropdown.Item>
 *   </Dropdown.Menu>
 * </Dropdown>
 */
var DropdownMenu = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var onToggle = props.onToggle,
      eventKey = props.eventKey,
      title = props.title,
      activeKey = props.activeKey,
      onSelect = props.onSelect,
      _props$classPrefix = props.classPrefix,
      classPrefix = _props$classPrefix === void 0 ? 'dropdown-menu' : _props$classPrefix,
      children = props.children,
      rest = (0, _objectWithoutPropertiesLoose2.default)(props, ["onToggle", "eventKey", "title", "activeKey", "onSelect", "classPrefix", "children"]);
  var dropdown = (0, _react.useContext)(_DropdownContext.default);
  var sidenav = (0, _react.useContext)(_Sidenav.SidenavContext);

  var _useCustom = (0, _useCustom2.default)('DropdownMenu'),
      rtl = _useCustom.rtl;

  var handleToggleSubmenu = (0, _react.useCallback)(function (_, event) {
    onToggle === null || onToggle === void 0 ? void 0 : onToggle(eventKey, event);
  }, [eventKey, onToggle]);

  var _useClassNames = (0, _utils.useClassNames)(classPrefix),
      merge = _useClassNames.merge,
      prefix = _useClassNames.prefix,
      withClassPrefix = _useClassNames.withClassPrefix;

  var _useClassNames2 = (0, _utils.useClassNames)('dropdown-menu'),
      withMenuClassPrefix = _useClassNames2.withClassPrefix,
      mergeMenuClassName = _useClassNames2.merge;

  var _useClassNames3 = (0, _utils.useClassNames)('dropdown-item'),
      mergeItemClassNames = _useClassNames3.merge,
      withItemClassPrefix = _useClassNames3.withClassPrefix,
      prefixItemClassName = _useClassNames3.prefix;

  var contextValue = (0, _react.useMemo)(function () {
    return {
      activeKey: activeKey,
      onSelect: onSelect
    };
  }, [activeKey, onSelect]); // <Dropdown.Menu> is used outside of <Dropdown>
  // renders a vertical `menubar`

  if (!dropdown) {
    var classes = merge(props.className, withClassPrefix());
    return /*#__PURE__*/_react.default.createElement(_DropdownContext.default.Provider, {
      value: contextValue
    }, /*#__PURE__*/_react.default.createElement(_Menubar.default, {
      vertical: true,
      onActivateItem: function onActivateItem(event) {
        var _dataset = event.target.dataset,
            eventKey = _dataset.eventKey,
            eventKeyType = _dataset.eventKeyType; // Only cast number type for now

        var eventKeyToEmit = eventKeyType === 'number' ? Number(eventKey) : eventKey;
        onSelect === null || onSelect === void 0 ? void 0 : onSelect(eventKeyToEmit, event);
      }
    }, function (menubar, menubarRef) {
      return /*#__PURE__*/_react.default.createElement("ul", (0, _extends2.default)({
        ref: (0, _utils.mergeRefs)(menubarRef, ref),
        className: classes
      }, menubar, rest), children);
    }));
  }

  if (sidenav !== null && sidenav !== void 0 && sidenav.expanded) {
    return /*#__PURE__*/_react.default.createElement(_SidenavDropdownMenu.default, (0, _omit2.default)(props, 'classPrefix'));
  } // Parent menu exists. This is a submenu.
  // Should render a `menuitem` that controls this submenu.


  var _omit = (0, _omit2.default)(rest, ['trigger']),
      icon = _omit.icon,
      className = _omit.className,
      disabled = _omit.disabled,
      menuProps = (0, _objectWithoutPropertiesLoose2.default)(_omit, ["icon", "className", "disabled"]);

  var Icon = rtl ? _AngleLeft.default : _AngleRight.default;
  return /*#__PURE__*/_react.default.createElement(_Menu.default, {
    openMenuOn: ['mouseover', 'click'],
    renderMenuButton: function renderMenuButton(_ref, buttonRef) {
      var open = _ref.open,
          menuButtonProps = (0, _objectWithoutPropertiesLoose2.default)(_ref, ["open"]);
      return /*#__PURE__*/_react.default.createElement(_MenuItem.default, {
        disabled: disabled
      }, function (_ref2, menuitemRef) {
        var selected = _ref2.selected,
            active = _ref2.active,
            menuitem = (0, _objectWithoutPropertiesLoose2.default)(_ref2, ["selected", "active"]);
        var classes = mergeItemClassNames(className, prefixItemClassName("pull-" + (rtl ? 'left' : 'right')), prefixItemClassName(_templateObject || (_templateObject = (0, _taggedTemplateLiteralLoose2.default)(["toggle"]))), // prefixItemClassName`submenu`,
        withItemClassPrefix({
          'with-icon': icon,
          open: open,
          active: selected,
          disabled: disabled,
          focus: active
        }));
        return /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({
          ref: (0, _utils.mergeRefs)(buttonRef, menuitemRef),
          className: classes,
          "data-event-key": eventKey,
          "data-event-key-type": typeof eventKey
        }, menuitem, (0, _omit2.default)(menuButtonProps, ['role'])), icon && /*#__PURE__*/_react.default.cloneElement(icon, {
          className: prefix('menu-icon')
        }), title, /*#__PURE__*/_react.default.createElement(Icon, {
          className: prefix(_templateObject2 || (_templateObject2 = (0, _taggedTemplateLiteralLoose2.default)(["toggle-icon"])))
        }));
      });
    },
    renderMenuPopup: function renderMenuPopup(_ref3, popupRef) {
      var open = _ref3.open,
          popupProps = (0, _objectWithoutPropertiesLoose2.default)(_ref3, ["open"]);
      var menuClassName = mergeMenuClassName(className, withMenuClassPrefix());
      return /*#__PURE__*/_react.default.createElement("ul", (0, _extends2.default)({
        ref: popupRef,
        className: menuClassName,
        hidden: !open
      }, popupProps, menuProps), children);
    },
    onToggleMenu: handleToggleSubmenu
  }, function (_ref4, menuContainerRef) {
    var open = _ref4.open,
        menuContainer = (0, _objectWithoutPropertiesLoose2.default)(_ref4, ["open"]);
    var classes = mergeItemClassNames(className, withItemClassPrefix({
      disabled: disabled,
      open: open,
      submenu: true // focus: hasFocus

    }));
    return /*#__PURE__*/_react.default.createElement("li", (0, _extends2.default)({
      ref: (0, _utils.mergeRefs)(ref, menuContainerRef),
      className: classes
    }, menuContainer));
  });
});

DropdownMenu.displayName = 'Dropdown.Menu';
DropdownMenu.propTypes = {
  active: _propTypes.default.bool,
  activeKey: _propTypes.default.any,
  className: _propTypes.default.string,
  children: _propTypes.default.node,
  icon: _propTypes.default.any,
  classPrefix: _propTypes.default.string,
  pullLeft: _propTypes.default.bool,
  title: _propTypes.default.node,
  open: _propTypes.default.bool,
  trigger: _propTypes.default.oneOf(['click', 'hover']),
  eventKey: _propTypes.default.any,
  expanded: _propTypes.default.bool,
  collapsible: _propTypes.default.bool,
  onSelect: _propTypes.default.func,
  onToggle: _propTypes.default.func
};
var _default = DropdownMenu;
exports.default = _default;