import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
import React from 'react';
import PropTypes from 'prop-types';
import NavItem from './NavItem';
import Dropdown from '../Dropdown';
import { ReactChildren, useClassNames, shallowEqual } from '../utils';
import { NavbarContext } from '../Navbar/Navbar';
import { SidenavContext } from '../Sidenav/Sidenav';
var defaultProps = {
  classPrefix: 'nav',
  appearance: 'default',
  as: 'div'
};
var Nav = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var Component = props.as,
      classPrefix = props.classPrefix,
      appearance = props.appearance,
      vertical = props.vertical,
      justified = props.justified,
      reversed = props.reversed,
      pullRight = props.pullRight,
      className = props.className,
      children = props.children,
      activeKeyProp = props.activeKey,
      onSelectProp = props.onSelect,
      rest = _objectWithoutPropertiesLoose(props, ["as", "classPrefix", "appearance", "vertical", "justified", "reversed", "pullRight", "className", "children", "activeKey", "onSelect"]);

  var _ref = React.useContext(SidenavContext) || {},
      _ref$sidenav = _ref.sidenav,
      sidenav = _ref$sidenav === void 0 ? false : _ref$sidenav,
      _ref$expanded = _ref.expanded,
      expanded = _ref$expanded === void 0 ? false : _ref$expanded,
      _ref$activeKey = _ref.activeKey,
      activeKey = _ref$activeKey === void 0 ? activeKeyProp : _ref$activeKey,
      _ref$onSelect = _ref.onSelect,
      onSelect = _ref$onSelect === void 0 ? onSelectProp : _ref$onSelect;

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

  var hasWaterline = appearance !== 'default';
  var items = ReactChildren.mapCloneElement(children, function (item) {
    var _item$type;

    var _item$props = item.props,
        eventKey = _item$props.eventKey,
        active = _item$props.active,
        rest = _objectWithoutPropertiesLoose(_item$props, ["eventKey", "active"]);

    var displayName = item === null || item === void 0 ? void 0 : (_item$type = item.type) === null || _item$type === void 0 ? void 0 : _item$type.displayName;
    var hasTooltip = sidenav && !expanded;

    if (displayName === 'NavItem') {
      return _extends({}, rest, {
        onSelect: onSelect,
        hasTooltip: hasTooltip,
        active: typeof activeKey === 'undefined' ? active : shallowEqual(activeKey, eventKey)
      });
    } else if (displayName === 'Dropdown') {
      return _extends({}, rest, {
        onSelect: onSelect,
        activeKey: activeKey,
        showHeader: hasTooltip,
        as: 'div'
      });
    }

    return null;
  });
  return /*#__PURE__*/React.createElement(NavbarContext.Consumer, null, function (navbar) {
    var classes = merge(className, rootPrefix({
      'navbar-nav': navbar,
      'navbar-right': pullRight,
      'sidenav-nav': sidenav
    }), withClassPrefix(appearance, {
      horizontal: navbar || !vertical && !sidenav,
      vertical: vertical || sidenav,
      justified: justified,
      reversed: reversed
    }));
    return /*#__PURE__*/React.createElement(Component, _extends({}, rest, {
      ref: ref,
      className: classes
    }), items, hasWaterline && /*#__PURE__*/React.createElement("div", {
      className: prefix('bar')
    }));
  });
});
Nav.Dropdown = Dropdown;
Nav.Item = NavItem;
Nav.displayName = 'Nav';
Nav.defaultProps = defaultProps;
Nav.propTypes = {
  classPrefix: PropTypes.string,
  className: PropTypes.string,
  children: PropTypes.node,
  appearance: PropTypes.oneOf(['default', 'subtle', 'tabs']),
  // Reverse Direction of tabs/subtle
  reversed: PropTypes.bool,
  justified: PropTypes.bool,
  vertical: PropTypes.bool,
  pullRight: PropTypes.bool,
  activeKey: PropTypes.any,
  onSelect: PropTypes.func
};
export default Nav;