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

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

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

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

var defaultProps = {
  as: 'div',
  classPrefix: 'dropdown'
};

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

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

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

  var selected = activeProp !== null && activeProp !== void 0 ? activeProp : !(0, _isNil.default)(eventKey) && (0, _utils.shallowEqual)(activeKey, eventKey);
  var handleClick = (0, _react.useCallback)(function (event) {
    if (disabled) return;
    onSelect === null || onSelect === void 0 ? void 0 : onSelect(eventKey, event);
    onSelectFromNav === null || onSelectFromNav === void 0 ? void 0 : onSelectFromNav(eventKey, event);
    onClick === null || onClick === void 0 ? void 0 : onClick(event);
  }, [disabled, onSelect, onSelectFromNav, eventKey, onClick]);

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

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

  return /*#__PURE__*/_react.default.createElement(_SafeAnchor.default, (0, _extends2.default)({
    ref: ref,
    className: merge(className, withClassPrefix({
      active: selected,
      disabled: disabled
    })),
    onClick: handleClick,
    style: style,
    "aria-selected": selected || undefined,
    "data-event-key": eventKey
  }, rest), icon, children, /*#__PURE__*/_react.default.createElement(_Ripple.default, null));
});

SidenavItem.displayName = 'Sidenav.Item';
SidenavItem.defaultProps = defaultProps;
SidenavItem.propTypes = {
  classPrefix: _propTypes.default.string,
  disabled: _propTypes.default.bool,
  icon: _propTypes.default.node,
  className: _propTypes.default.string,
  children: _propTypes.default.node,
  eventKey: _propTypes.default.any,
  as: _propTypes.default.elementType,
  style: _propTypes.default.object,
  onSelect: _propTypes.default.func,
  onMouseEnter: _propTypes.default.func,
  onMouseLeave: _propTypes.default.func,
  onContextMenu: _propTypes.default.func,
  onClick: _propTypes.default.func
};
var _default = SidenavItem;
exports.default = _default;