"use strict";

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 = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _NavItem = _interopRequireDefault(require("./NavItem"));

var _Dropdown = _interopRequireDefault(require("../Dropdown"));

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

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

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

var defaultProps = {
  classPrefix: 'nav',
  appearance: 'default',
  as: 'div'
};

var Nav = /*#__PURE__*/_react.default.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 = (0, _objectWithoutPropertiesLoose2.default)(props, ["as", "classPrefix", "appearance", "vertical", "justified", "reversed", "pullRight", "className", "children", "activeKey", "onSelect"]);

  var _ref = _react.default.useContext(_Sidenav.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 = (0, _utils.useClassNames)(classPrefix),
      withClassPrefix = _useClassNames.withClassPrefix,
      merge = _useClassNames.merge,
      rootPrefix = _useClassNames.rootPrefix,
      prefix = _useClassNames.prefix;

  var hasWaterline = appearance !== 'default';

  var items = _utils.ReactChildren.mapCloneElement(children, function (item) {
    var _item$type;

    var _item$props = item.props,
        eventKey = _item$props.eventKey,
        active = _item$props.active,
        rest = (0, _objectWithoutPropertiesLoose2.default)(_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 (0, _extends2.default)({}, rest, {
        onSelect: onSelect,
        hasTooltip: hasTooltip,
        active: typeof activeKey === 'undefined' ? active : (0, _utils.shallowEqual)(activeKey, eventKey)
      });
    } else if (displayName === 'Dropdown') {
      return (0, _extends2.default)({}, rest, {
        onSelect: onSelect,
        activeKey: activeKey,
        showHeader: hasTooltip,
        as: 'div'
      });
    }

    return null;
  });

  return /*#__PURE__*/_react.default.createElement(_Navbar.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.default.createElement(Component, (0, _extends2.default)({}, rest, {
      ref: ref,
      className: classes
    }), items, hasWaterline && /*#__PURE__*/_react.default.createElement("div", {
      className: prefix('bar')
    }));
  });
});

Nav.Dropdown = _Dropdown.default;
Nav.Item = _NavItem.default;
Nav.displayName = 'Nav';
Nav.defaultProps = defaultProps;
Nav.propTypes = {
  classPrefix: _propTypes.default.string,
  className: _propTypes.default.string,
  children: _propTypes.default.node,
  appearance: _propTypes.default.oneOf(['default', 'subtle', 'tabs']),
  // Reverse Direction of tabs/subtle
  reversed: _propTypes.default.bool,
  justified: _propTypes.default.bool,
  vertical: _propTypes.default.bool,
  pullRight: _propTypes.default.bool,
  activeKey: _propTypes.default.any,
  onSelect: _propTypes.default.func
};
var _default = Nav;
exports.default = _default;