"use strict";

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

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

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

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

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

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

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

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

var _DropdownMenu = _interopRequireDefault(require("./DropdownMenu"));

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

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

var _deprecatePropType = _interopRequireDefault(require("../utils/deprecatePropType"));

var _DropdownItem = _interopRequireDefault(require("./DropdownItem"));

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

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

var _DropdownToggle = _interopRequireDefault(require("./DropdownToggle"));

var _MenuContext = _interopRequireDefault(require("../Menu/MenuContext"));

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

var _kebabCase = _interopRequireDefault(require("lodash/kebabCase"));

var _Navbar = require("../Navbar/Navbar");

var _Disclosure = _interopRequireDefault(require("../Disclosure/Disclosure"));

var _SidenavDropdown = _interopRequireDefault(require("../Sidenav/SidenavDropdown"));

var _NavContext = _interopRequireDefault(require("../Nav/NavContext"));

var defaultProps = {
  as: 'div',
  classPrefix: 'dropdown',
  placement: 'bottomStart',
  trigger: 'click'
};
/**
 * The <Dropdown> API
 * When used inside <Sidenav>, renders a <TreeviewRootItem>;
 * Otherwise renders a <MenuRoot>
 */

var Dropdown = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var activeKey = props.activeKey,
      onSelectProp = props.onSelect,
      rest = (0, _objectWithoutPropertiesLoose2.default)(props, ["activeKey", "onSelect"]);
  var Component = rest.as,
      title = rest.title,
      onClose = rest.onClose,
      onOpen = rest.onOpen,
      onToggle = rest.onToggle,
      eventKey = rest.eventKey,
      trigger = rest.trigger,
      placement = rest.placement,
      renderTitle = rest.renderTitle,
      toggleAs = rest.toggleAs,
      toggleClassName = rest.toggleClassName,
      classPrefix = rest.classPrefix,
      className = rest.className,
      disabled = rest.disabled,
      children = rest.children,
      menuStyle = rest.menuStyle,
      style = rest.style,
      menuProps = (0, _objectWithoutPropertiesLoose2.default)(rest, ["as", "title", "onClose", "onOpen", "onToggle", "eventKey", "trigger", "placement", "renderTitle", "toggleAs", "toggleClassName", "classPrefix", "className", "disabled", "children", "menuStyle", "style"]);

  var _useContext = (0, _react.useContext)(_NavContext.default),
      onSelectFromNav = _useContext.onSelect;

  var emitSelect = (0, _react.useCallback)(function (eventKey, event) {
    onSelectProp === null || onSelectProp === void 0 ? void 0 : onSelectProp(eventKey, event); // If <Dropdown> is inside <Nav>, also trigger `onSelect` on <Nav>

    onSelectFromNav === null || onSelectFromNav === void 0 ? void 0 : onSelectFromNav(eventKey, event);
  }, [onSelectProp, onSelectFromNav]);

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

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

  var _useClassNames3 = (0, _utils.useClassNames)('nav-item'),
      withNavItemClassPrefix = _useClassNames3.withClassPrefix,
      mergeNavItemClassNames = _useClassNames3.merge;

  var menuButtonTriggers = (0, _react.useMemo)(function () {
    if (!trigger) {
      return undefined;
    }

    var triggerMap = {
      hover: 'mouseover',
      click: 'click',
      contextMenu: 'contextmenu'
    };

    if (!Array.isArray(trigger)) {
      return [triggerMap[trigger]];
    }

    return trigger.map(function (t) {
      return triggerMap[t];
    });
  }, [trigger]);
  var parentMenu = (0, _react.useContext)(_MenuContext.default);
  var sidenav = (0, _react.useContext)(_Sidenav.SidenavContext);
  var navbar = (0, _react.useContext)(_Navbar.NavbarContext); // Render a disclosure when inside expanded <Sidenav>

  if (sidenav !== null && sidenav !== void 0 && sidenav.expanded) {
    return /*#__PURE__*/_react.default.createElement(_DropdownContext.default.Provider, {
      value: {
        activeKey: activeKey,
        onSelect: emitSelect
      }
    }, /*#__PURE__*/_react.default.createElement(_SidenavDropdown.default, (0, _extends2.default)({
      ref: ref
    }, rest)));
  } // Renders a disclosure when used inside <Navbar>


  if (navbar) {
    return /*#__PURE__*/_react.default.createElement(_DropdownContext.default.Provider, {
      value: {
        activeKey: activeKey,
        onSelect: emitSelect
      }
    }, /*#__PURE__*/_react.default.createElement(_Disclosure.default, {
      hideOnClickOutside: true
    }, function (_ref, containerRef) {
      var _withClassPrefix;

      var open = _ref.open;
      var classes = merge(className, withClassPrefix((_withClassPrefix = {}, _withClassPrefix["placement-" + (0, _kebabCase.default)((0, _utils.placementPolyfill)(placement))] = !!placement, _withClassPrefix.disabled = disabled, _withClassPrefix.open = open, _withClassPrefix)));
      return /*#__PURE__*/_react.default.createElement(Component, {
        ref: (0, _utils.mergeRefs)(ref, containerRef),
        className: classes,
        style: style
      }, /*#__PURE__*/_react.default.createElement(_Disclosure.default.Button, null, function (buttonProps, buttonRef) {
        return /*#__PURE__*/_react.default.createElement(_DropdownToggle.default, (0, _extends2.default)({
          ref: buttonRef,
          as: renderTitle ? 'span' : toggleAs,
          className: toggleClassName,
          placement: placement,
          disabled: disabled
        }, (0, _omit.default)(buttonProps, ['open']), menuProps), title);
      }), /*#__PURE__*/_react.default.createElement(_Disclosure.default.Content, null, function (_ref2, elementRef) {
        var open = _ref2.open;
        var menuClassName = mergeMenuClassName(className, withMenuClassPrefix());
        return /*#__PURE__*/_react.default.createElement("ul", {
          ref: elementRef,
          className: menuClassName,
          style: menuStyle,
          hidden: !open
        }, children);
      }));
    }));
  }

  var renderMenuButton = function renderMenuButton(menuButtonProps, menuButtonRef) {
    return /*#__PURE__*/_react.default.createElement(_DropdownToggle.default, (0, _extends2.default)({
      ref: menuButtonRef,
      as: renderTitle ? 'span' : toggleAs,
      className: toggleClassName,
      placement: placement,
      disabled: disabled
    }, (0, _omit.default)(menuButtonProps, ['open']), menuProps), title);
  };

  if (parentMenu) {
    renderMenuButton = function renderMenuButton(menuButtonProps, buttonRef) {
      return /*#__PURE__*/_react.default.createElement(_MenuItem.default, {
        disabled: disabled
      }, function (_ref3, menuitemRef) {
        var active = _ref3.active,
            menuitemProps = (0, _objectWithoutPropertiesLoose2.default)(_ref3, ["active"]);
        return /*#__PURE__*/_react.default.createElement(_DropdownToggle.default, (0, _extends2.default)({
          ref: (0, _utils.mergeRefs)(buttonRef, menuitemRef),
          as: renderTitle ? 'span' : toggleAs,
          className: mergeNavItemClassNames(toggleClassName, withNavItemClassPrefix({
            focus: active
          }))
        }, menuButtonProps, (0, _omit.default)(menuitemProps, ['onClick']), menuProps), title);
      });
    };
  }

  return /*#__PURE__*/_react.default.createElement(_DropdownContext.default.Provider, {
    value: {
      activeKey: activeKey,
      onSelect: emitSelect
    }
  }, /*#__PURE__*/_react.default.createElement(_Menu.default, {
    menuButtonText: title,
    renderMenuButton: renderMenuButton,
    openMenuOn: menuButtonTriggers,
    renderMenuPopup: function renderMenuPopup(_ref4, popupRef) {
      var open = _ref4.open,
          popupProps = (0, _objectWithoutPropertiesLoose2.default)(_ref4, ["open"]);
      var menuClassName = mergeMenuClassName(className, withMenuClassPrefix()); // When inside a collapsed <Sidenav>, render a header in menu

      var showHeader = !!sidenav;
      return /*#__PURE__*/_react.default.createElement("ul", (0, _extends2.default)({
        ref: popupRef,
        className: menuClassName,
        style: menuStyle,
        hidden: !open
      }, popupProps), showHeader && /*#__PURE__*/_react.default.createElement("div", {
        className: prefix('header')
      }, title), children);
    },
    onToggleMenu: function onToggleMenu(open, event) {
      onToggle === null || onToggle === void 0 ? void 0 : onToggle(open);
      sidenav === null || sidenav === void 0 ? void 0 : sidenav.onOpenChange(eventKey, event);

      if (open) {
        onOpen === null || onOpen === void 0 ? void 0 : onOpen();
      } else {
        onClose === null || onClose === void 0 ? void 0 : onClose();
      }
    }
  }, function (_ref5, menuContainerRef) {
    var _withClassPrefix2;

    var open = _ref5.open,
        menuContainer = (0, _objectWithoutPropertiesLoose2.default)(_ref5, ["open"]);
    var classes = merge(className, withClassPrefix((_withClassPrefix2 = {}, _withClassPrefix2["placement-" + (0, _kebabCase.default)((0, _utils.placementPolyfill)(placement))] = !!placement, _withClassPrefix2.disabled = disabled, _withClassPrefix2.open = open, _withClassPrefix2.submenu = !!parentMenu, _withClassPrefix2)));
    return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({
      ref: (0, _utils.mergeRefs)(ref, menuContainerRef),
      className: classes
    }, menuContainer, {
      style: style
    }));
  }));
});

Dropdown.Item = _DropdownItem.default;
Dropdown.Menu = _DropdownMenu.default;
Dropdown.displayName = 'Dropdown';
Dropdown.defaultProps = defaultProps;
Dropdown.propTypes = {
  activeKey: _propTypes.default.any,
  classPrefix: _propTypes.default.string,
  trigger: _propTypes.default.oneOfType([_propTypes.default.array, _propTypes.default.oneOf(['click', 'hover', 'contextMenu'])]),
  placement: _propTypes.default.oneOf(_utils.PLACEMENT_8),
  title: _propTypes.default.node,
  disabled: _propTypes.default.bool,
  icon: _propTypes.default.node,
  menuStyle: _propTypes.default.object,
  className: _propTypes.default.string,
  toggleClassName: _propTypes.default.string,
  children: _propTypes.default.node,
  open: (0, _deprecatePropType.default)(_propTypes.default.bool),
  eventKey: _propTypes.default.any,
  as: _propTypes.default.elementType,
  toggleAs: _propTypes.default.elementType,
  noCaret: _propTypes.default.bool,
  style: _propTypes.default.object,
  onClose: _propTypes.default.func,
  onOpen: _propTypes.default.func,
  onToggle: _propTypes.default.func,
  onSelect: _propTypes.default.func,
  onMouseEnter: _propTypes.default.func,
  onMouseLeave: _propTypes.default.func,
  onContextMenu: _propTypes.default.func,
  onClick: _propTypes.default.func,
  renderTitle: _propTypes.default.func
};
var _default = Dropdown;
exports.default = _default;