import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
import React, { useContext, useCallback } from 'react';
import PropTypes from 'prop-types';
import isNil from 'lodash/isNil';
import { useClassNames, shallowEqual } from '../utils';
import Ripple from '../Ripple';
import SafeAnchor from '../SafeAnchor';
import NavContext from '../Nav/NavContext';
var defaultProps = {
  as: 'div',
  classPrefix: 'dropdown'
};
var SidenavItem = /*#__PURE__*/React.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 = _objectWithoutPropertiesLoose(props, ["active", "children", "className", "disabled", "classPrefix", "icon", "eventKey", "style", "onClick", "onSelect", "divider", "panel"]);

  var _useContext = useContext(NavContext),
      activeKey = _useContext.activeKey,
      onSelectFromNav = _useContext.onSelect;

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

  var selected = activeProp !== null && activeProp !== void 0 ? activeProp : !isNil(eventKey) && shallowEqual(activeKey, eventKey);
  var handleClick = 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.createElement("li", _extends({
      ref: ref,
      role: "separator",
      style: style,
      className: merge(className, prefix('divider'))
    }, rest));
  }

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

  return /*#__PURE__*/React.createElement(SafeAnchor, _extends({
    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.createElement(Ripple, null));
});
SidenavItem.displayName = 'Sidenav.Item';
SidenavItem.defaultProps = defaultProps;
SidenavItem.propTypes = {
  classPrefix: PropTypes.string,
  disabled: PropTypes.bool,
  icon: PropTypes.node,
  className: PropTypes.string,
  children: PropTypes.node,
  eventKey: PropTypes.any,
  as: PropTypes.elementType,
  style: PropTypes.object,
  onSelect: PropTypes.func,
  onMouseEnter: PropTypes.func,
  onMouseLeave: PropTypes.func,
  onContextMenu: PropTypes.func,
  onClick: PropTypes.func
};
export default SidenavItem;