import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
import React, { useCallback, useContext } from 'react';
import PropTypes from 'prop-types';
import isNil from 'lodash/isNil';
import omit from 'lodash/omit';
import Ripple from '../Ripple';
import SafeAnchor from '../SafeAnchor';
import { appendTooltip, mergeRefs, shallowEqual, useClassNames } from '../utils';
import { SidenavContext } from '../Sidenav/Sidenav';
import NavContext from './NavContext';
import { NavbarContext } from '../Navbar/Navbar';
import MenuItem from '../Menu/MenuItem';
import SidenavItem from '../Sidenav/SidenavItem';
var defaultProps = {
  classPrefix: 'nav-item',
  as: SafeAnchor
};
/**
 * The <Nav.Item> API
 */

var NavItem = /*#__PURE__*/React.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 = _objectWithoutPropertiesLoose(props, ["as", "active", "disabled", "eventKey", "className", "classPrefix", "style", "children", "icon", "divider", "panel", "onClick", "onSelect"]);

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

  var active = activeProp !== null && activeProp !== void 0 ? activeProp : !isNil(eventKey) && shallowEqual(eventKey, activeKey);
  var emitSelect = 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 = useContext(NavbarContext);
  var sidenav = useContext(SidenavContext);

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

  var classes = merge(className, withClassPrefix({
    active: active,
    disabled: disabled
  }));
  var handleClick = 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.createElement(SidenavItem, props);
  } // If <Nav.Item> is inside collapsed <Sidenav>, render an ARIA `menuitem`


  if (sidenav) {
    return /*#__PURE__*/React.createElement(MenuItem, {
      selected: active,
      disabled: disabled,
      onActivate: emitSelect
    }, function (_ref, menuitemRef) {
      var selected = _ref.selected,
          active = _ref.active,
          menuitem = _objectWithoutPropertiesLoose(_ref, ["selected", "active"]);

      var classes = merge(className, withClassPrefix({
        focus: active,
        active: selected,
        disabled: disabled
      }));
      var item = /*#__PURE__*/React.createElement(Component, _extends({
        ref: mergeRefs(ref, menuitemRef),
        disabled: Component === SafeAnchor ? disabled : undefined,
        className: classes,
        "data-event-key": eventKey
      }, menuitem, omit(rest, ['divider', 'panel'])), icon, _children, /*#__PURE__*/React.createElement(Ripple, null)); // Show tooltip when inside a collapse <Sidenav>

      return sidenav ? appendTooltip({
        children: function children(triggerProps, triggerRef) {
          return /*#__PURE__*/React.createElement(Component, _extends({
            ref: mergeRefs(mergeRefs(ref, menuitemRef), triggerRef),
            disabled: Component === SafeAnchor ? disabled : undefined,
            className: classes,
            "data-event-key": eventKey
          }, menuitem, omit(rest, ['divider', 'panel']), triggerProps), icon, _children, /*#__PURE__*/React.createElement(Ripple, null));
        },
        message: _children,
        placement: 'right'
      }) : item;
    });
  }

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

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

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

  return /*#__PURE__*/React.createElement(Component, _extends({
    ref: ref,
    tabIndex: disabled ? -1 : undefined
  }, rest, {
    className: classes,
    onClick: handleClick,
    style: style,
    "aria-selected": active || undefined
  }), icon, _children, /*#__PURE__*/React.createElement(Ripple, null));
});
NavItem.defaultProps = defaultProps;
NavItem.displayName = 'Nav.Item';
NavItem.propTypes = {
  as: PropTypes.elementType,
  active: PropTypes.bool,
  disabled: PropTypes.bool,
  className: PropTypes.string,
  classPrefix: PropTypes.string,
  divider: PropTypes.bool,
  panel: PropTypes.bool,
  onClick: PropTypes.func,
  style: PropTypes.object,
  icon: PropTypes.node,
  onSelect: PropTypes.func,
  children: PropTypes.node,
  eventKey: PropTypes.any
};
export default NavItem;