"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 _isNil = _interopRequireDefault(require("lodash/isNil"));

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

var _Ripple = _interopRequireDefault(require("../Ripple"));

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

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

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

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

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

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

var _SidenavItem = _interopRequireDefault(require("../Sidenav/SidenavItem"));

var defaultProps = {
  classPrefix: 'nav-item',
  as: _SafeAnchor.default
};
/**
 * The <Nav.Item> API
 */

var NavItem = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var Component = props.as,
      activeProp = props.active,
      disabled = props.disabled,
      eventKey = props.eventKey,
      className = props.className,
      classPrefix = props.classPrefix,
      style = props.style,
      _children = props.children,
      icon = props.icon,
      divider = props.divider,
      panel = props.panel,
      onClick = props.onClick,
      onSelectProp = props.onSelect,
      rest = (0, _objectWithoutPropertiesLoose2.default)(props, ["as", "active", "disabled", "eventKey", "className", "classPrefix", "style", "children", "icon", "divider", "panel", "onClick", "onSelect"]);

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

  var active = activeProp !== null && activeProp !== void 0 ? activeProp : !(0, _isNil.default)(eventKey) && (0, _utils.shallowEqual)(eventKey, activeKey);
  var emitSelect = (0, _react.useCallback)(function (event) {
    onSelectProp === null || onSelectProp === void 0 ? void 0 : onSelectProp(eventKey, event);
    onSelectFromNav === null || onSelectFromNav === void 0 ? void 0 : onSelectFromNav(eventKey, event);
  }, [eventKey, onSelectProp, onSelectFromNav]);
  var navbar = (0, _react.useContext)(_Navbar.NavbarContext);
  var sidenav = (0, _react.useContext)(_Sidenav.SidenavContext);

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

  var classes = merge(className, withClassPrefix({
    active: active,
    disabled: disabled
  }));
  var handleClick = (0, _react.useCallback)(function (event) {
    if (!disabled) {
      emitSelect(event);
      onClick === null || onClick === void 0 ? void 0 : onClick(event);
    }
  }, [disabled, emitSelect, onClick]);

  if (sidenav !== null && sidenav !== void 0 && sidenav.expanded) {
    return /*#__PURE__*/_react.default.createElement(_SidenavItem.default, props);
  } // If <Nav.Item> is inside collapsed <Sidenav>, render an ARIA `menuitem`


  if (sidenav) {
    return /*#__PURE__*/_react.default.createElement(_MenuItem.default, {
      selected: active,
      disabled: disabled,
      onActivate: emitSelect
    }, function (_ref, menuitemRef) {
      var selected = _ref.selected,
          active = _ref.active,
          menuitem = (0, _objectWithoutPropertiesLoose2.default)(_ref, ["selected", "active"]);
      var classes = merge(className, withClassPrefix({
        focus: active,
        active: selected,
        disabled: disabled
      }));

      var item = /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({
        ref: (0, _utils.mergeRefs)(ref, menuitemRef),
        disabled: Component === _SafeAnchor.default ? disabled : undefined,
        className: classes,
        "data-event-key": eventKey
      }, menuitem, (0, _omit.default)(rest, ['divider', 'panel'])), icon, _children, /*#__PURE__*/_react.default.createElement(_Ripple.default, null)); // Show tooltip when inside a collapse <Sidenav>


      return sidenav ? (0, _utils.appendTooltip)({
        children: function children(triggerProps, triggerRef) {
          return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({
            ref: (0, _utils.mergeRefs)((0, _utils.mergeRefs)(ref, menuitemRef), triggerRef),
            disabled: Component === _SafeAnchor.default ? disabled : undefined,
            className: classes,
            "data-event-key": eventKey
          }, menuitem, (0, _omit.default)(rest, ['divider', 'panel']), triggerProps), icon, _children, /*#__PURE__*/_react.default.createElement(_Ripple.default, null));
        },
        message: _children,
        placement: 'right'
      }) : item;
    });
  }

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

  if (panel) {
    return /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({
      ref: ref,
      style: style,
      className: merge(className, prefix('panel'))
    }, rest), _children);
  }

  if (navbar) {
    return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({
      ref: ref,
      "aria-selected": active || undefined
    }, rest, {
      className: classes,
      onClick: handleClick,
      style: style
    }), icon, _children, /*#__PURE__*/_react.default.createElement(_Ripple.default, null));
  }

  return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({
    ref: ref,
    tabIndex: disabled ? -1 : undefined
  }, rest, {
    className: classes,
    onClick: handleClick,
    style: style,
    "aria-selected": active || undefined
  }), icon, _children, /*#__PURE__*/_react.default.createElement(_Ripple.default, null));
});

NavItem.defaultProps = defaultProps;
NavItem.displayName = 'Nav.Item';
NavItem.propTypes = {
  as: _propTypes.default.elementType,
  active: _propTypes.default.bool,
  disabled: _propTypes.default.bool,
  className: _propTypes.default.string,
  classPrefix: _propTypes.default.string,
  divider: _propTypes.default.bool,
  panel: _propTypes.default.bool,
  onClick: _propTypes.default.func,
  style: _propTypes.default.object,
  icon: _propTypes.default.node,
  onSelect: _propTypes.default.func,
  children: _propTypes.default.node,
  eventKey: _propTypes.default.any
};
var _default = NavItem;
exports.default = _default;