"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 _Sidenav = require("../Sidenav/Sidenav");

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

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

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

var _isNil = _interopRequireDefault(require("lodash/isNil"));

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

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

var _SidenavDropdownItem = _interopRequireDefault(require("../Sidenav/SidenavDropdownItem"));

var _DisclosureContext = _interopRequireWildcard(require("../Disclosure/DisclosureContext"));

var _SafeAnchor = _interopRequireDefault(require("../SafeAnchor"));

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

var defaultProps = {
  as: 'li',
  classPrefix: 'dropdown-item'
};
/**
 * The <Dropdown.Item> API
 * When used inside <Sidenav>, renders a <TreeviewItem>
 * Otherwise renders a <MenuItem>
 */

var DropdownItem = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var classPrefix = props.classPrefix,
      className = props.className,
      activeProp = props.active,
      eventKey = props.eventKey,
      onSelect = props.onSelect,
      icon = props.icon,
      rest = (0, _objectWithoutPropertiesLoose2.default)(props, ["classPrefix", "className", "active", "eventKey", "onSelect", "icon"]);
  var nav = (0, _react.useContext)(_NavContext.default);
  var dropdown = (0, _react.useContext)(_DropdownContext.default);

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

  var handleSelectItem = (0, _react.useCallback)(function (event) {
    var _dropdown$onSelect;

    onSelect === null || onSelect === void 0 ? void 0 : onSelect(eventKey, event);
    dropdown === null || dropdown === void 0 ? void 0 : (_dropdown$onSelect = dropdown.onSelect) === null || _dropdown$onSelect === void 0 ? void 0 : _dropdown$onSelect.call(dropdown, eventKey, event);
  }, [onSelect, eventKey, dropdown]);
  var sidenav = (0, _react.useContext)(_Sidenav.SidenavContext);
  var navbar = (0, _react.useContext)(_Navbar.NavbarContext);
  var disclosure = (0, _react.useContext)(_DisclosureContext.default);

  var _ref = disclosure !== null && disclosure !== void 0 ? disclosure : [],
      dispatchDisclosure = _ref[1];

  var handleClickNavbarDropdownItem = (0, _react.useCallback)(function (event) {
    dispatchDisclosure({
      type: _DisclosureContext.DisclosureActionTypes.Hide
    });
    handleSelectItem === null || handleSelectItem === void 0 ? void 0 : handleSelectItem(event);
  }, [dispatchDisclosure, handleSelectItem]);

  if (sidenav !== null && sidenav !== void 0 && sidenav.expanded) {
    return /*#__PURE__*/_react.default.createElement(_SidenavDropdownItem.default, (0, _extends2.default)({
      ref: ref
    }, props));
  }

  var menuitemSelected = activeProp || !(0, _isNil.default)(eventKey) && ((0, _utils.shallowEqual)(dropdown === null || dropdown === void 0 ? void 0 : dropdown.activeKey, eventKey) || (0, _utils.shallowEqual)(nav === null || nav === void 0 ? void 0 : nav.activeKey, eventKey));
  var Component = rest.as,
      divider = rest.divider,
      panel = rest.panel,
      children = rest.children,
      disabled = rest.disabled,
      restProps = (0, _objectWithoutPropertiesLoose2.default)(rest, ["as", "divider", "panel", "children", "disabled"]);

  if (divider) {
    return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({
      ref: ref,
      role: "separator",
      className: merge(prefix('divider'), className)
    }, restProps));
  }

  if (panel) {
    return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({
      ref: ref,
      role: "none presentation",
      className: merge(prefix('panel'), className)
    }, restProps), children);
  }

  if (navbar) {
    var classes = merge(className, withClassPrefix({
      'with-icon': icon,
      disabled: disabled,
      divider: divider,
      panel: panel,
      active: menuitemSelected
    }));
    var dataAttributes = {
      'data-event-key': eventKey
    };

    if (!(0, _isNil.default)(eventKey) && typeof eventKey !== 'string') {
      dataAttributes['data-event-key-type'] = typeof eventKey;
    }

    return /*#__PURE__*/_react.default.createElement("li", null, /*#__PURE__*/_react.default.createElement(_SafeAnchor.default, (0, _extends2.default)({
      ref: ref,
      className: classes,
      "aria-current": menuitemSelected || undefined
    }, dataAttributes, restProps, {
      onClick: (0, _utils.createChainedFunction)(handleClickNavbarDropdownItem, restProps.onClick)
    }), icon && /*#__PURE__*/_react.default.cloneElement(icon, {
      className: prefix('menu-icon')
    }), children));
  }

  return /*#__PURE__*/_react.default.createElement(_MenuItem.default, {
    selected: menuitemSelected,
    disabled: disabled,
    onActivate: handleSelectItem
  }, function (_ref2, menuitemRef) {
    var selected = _ref2.selected,
        active = _ref2.active,
        menuitem = (0, _objectWithoutPropertiesLoose2.default)(_ref2, ["selected", "active"]);
    var classes = merge(className, withClassPrefix({
      'with-icon': icon,
      active: selected,
      disabled: disabled,
      focus: active,
      divider: divider,
      panel: panel
    }));
    var dataAttributes = {
      'data-event-key': eventKey
    };

    if (!(0, _isNil.default)(eventKey) && typeof eventKey !== 'string') {
      dataAttributes['data-event-key-type'] = typeof eventKey;
    }

    return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({
      ref: (0, _utils.mergeRefs)(ref, menuitemRef),
      className: classes
    }, menuitem, dataAttributes, restProps), icon && /*#__PURE__*/_react.default.cloneElement(icon, {
      className: prefix('menu-icon')
    }), children);
  });
});

DropdownItem.displayName = 'Dropdown.Item';
DropdownItem.defaultProps = defaultProps;
DropdownItem.propTypes = {
  as: _propTypes.default.elementType,
  divider: _propTypes.default.bool,
  panel: _propTypes.default.bool,
  trigger: _propTypes.default.oneOfType([_propTypes.default.array, _propTypes.default.oneOf(['click', 'hover'])]),
  open: (0, _deprecatePropType.default)(_propTypes.default.bool),
  active: _propTypes.default.bool,
  disabled: _propTypes.default.bool,
  pullLeft: (0, _deprecatePropType.default)(_propTypes.default.bool),
  submenu: _propTypes.default.element,
  onSelect: _propTypes.default.func,
  onClick: _propTypes.default.func,
  icon: _propTypes.default.node,
  eventKey: _propTypes.default.any,
  className: _propTypes.default.string,
  style: _propTypes.default.object,
  children: _propTypes.default.node,
  classPrefix: _propTypes.default.string,
  tabIndex: _propTypes.default.number
};
var _default = DropdownItem;
exports.default = _default;