"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Navigation = Navigation;

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _react = _interopRequireWildcard(require("react"));

var _Box = require("../Box");

var _Flex = require("../Flex");

var _Link = require("../Link");

var _PseudoBox = require("../PseudoBox");

var _router = require("../utils/router");

var _styles = _interopRequireDefault(require("./styles"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var __jsx = _react["default"].createElement;

function Navigation(props) {
  var _useNavigationStyle = (0, _styles["default"])(props),
      style = _useNavigationStyle.root;

  return __jsx(_Flex.Flex, (0, _extends2["default"])({
    as: "header",
    role: "banner",
    align: "center",
    justify: "space-between",
    direction: "row",
    boxShadow: "topNav"
  }, style, props), props.children);
}

Navigation.Primary = function NavigationPrimary(props) {
  return __jsx(_Flex.Flex, (0, _extends2["default"])({
    display: "inline-flex",
    align: "center",
    mr: "spacing-sm"
  }, props), props.children);
};

Navigation.Secondary = function NavigationSecondary(props) {
  return __jsx(_Flex.Flex, (0, _extends2["default"])({
    flexGrow: 1
  }, props), props.children);
};

Navigation.Tertiary = function NavigationTertiary(props) {
  return __jsx(_Flex.Flex, (0, _extends2["default"])({
    justify: "flex-end",
    align: "center"
  }, props), props.children);
};

Navigation.Item = function NavItem(props) {
  var href = props.href,
      exact = props.exact,
      isSubmenuItem = props.isSubmenuItem,
      isActive = props.isActive,
      isParent = props.isParent,
      isMinified = props.isMinified,
      onClick = props.onClick,
      rest = (0, _objectWithoutProperties2["default"])(props, ["href", "exact", "isSubmenuItem", "isActive", "isParent", "isMinified", "onClick"]);

  var _useRouter = (0, _router.useRouter)(),
      pathname = _useRouter.pathname,
      search = _useRouter.search;

  var isLinkActive = false;
  var path = "".concat(pathname).concat(search);

  if (href && href === path && exact) {
    isLinkActive = true;
  } else if (href && path.indexOf(href) > -1 && !exact) {
    isLinkActive = true;
  }

  var _useNavigationStyle2 = (0, _styles["default"])({
    isActive: isActive || isLinkActive,
    isSubmenuItem: isSubmenuItem,
    clickable: !!(href || onClick)
  }),
      navItemStyleProps = _useNavigationStyle2.navItem,
      activeBarStyleProps = _useNavigationStyle2.activeBar;

  var NavElement = href ? _Link.Link : _Flex.Flex;
  return __jsx(NavElement, (0, _extends2["default"])({
    href: !isParent || isParent && isMinified ? href : undefined,
    onClick: onClick
  }, navItemStyleProps, rest), (isActive || isLinkActive) && !isSubmenuItem && __jsx(_Box.Box, activeBarStyleProps), typeof props.children !== 'string' ? _react.Children.map(props.children, function (child, index) {
    if (child) {
      return /*#__PURE__*/(0, _react.cloneElement)(child, {
        isActive: isLinkActive
      });
    }

    return null;
  }) : props.children);
};

Navigation.ItemMedia = function NavItemLeft(props) {
  var icon = props.icon,
      isActive = props.isActive,
      unstyled = props.unstyled;
  var MdiIcon = icon;

  var _useNavigationStyle3 = (0, _styles["default"])({
    isActive: isActive,
    unstyled: unstyled
  }),
      navItemMediaStyleProps = _useNavigationStyle3.navItemMedia;

  return __jsx(_PseudoBox.PseudoBox, (0, _extends2["default"])({
    w: "32px"
  }, navItemMediaStyleProps, props), props.children || MdiIcon && __jsx(MdiIcon, {
    color: "currentColor",
    size: 28
  }) || null);
};

Navigation.ItemText = function NavItemText(props) {
  return __jsx(_Box.Box, {
    flex: "1",
    whiteSpace: "nowrap"
  }, props.children);
};

Navigation.ItemMeta = function NavItemRight(props) {
  return __jsx(_Box.Box, null, props.children);
};
//# sourceMappingURL=Navigation.js.map