"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.Header = void 0;

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

var _reactRouter = require("react-router");

var _reactRouterDom = require("react-router-dom");

var _classnames = _interopRequireDefault(require("classnames"));

var _NavLink = require("./NavLink");

var _NavDropdown = require("./NavDropdown");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var closeIcon = function closeIcon(props) {
  return /*#__PURE__*/_react.default.createElement("svg", props, /*#__PURE__*/_react.default.createElement("title", null, "close"), /*#__PURE__*/_react.default.createElement("path", {
    fill: "#565C65",
    fillRule: "evenodd",
    d: "M57.042 1.15l5.809 5.808a4 4 0 0 1 0 5.657L43.465 32l19.386 19.385a4 4 0 0 1 0 5.657l-5.809 5.809a4 4 0 0 1-5.657 0L32 43.465 12.615 62.851a4 4 0 0 1-5.657 0l-5.809-5.809a4 4 0 0 1 0-5.657L20.535 32 1.149 12.615a4 4 0 0 1 0-5.657l5.809-5.809a4 4 0 0 1 5.657 0L32 20.535 51.385 1.149a4 4 0 0 1 5.657 0z"
  }));
};

closeIcon.defaultProps = {
  xmlns: "http://www.w3.org/2000/svg",
  width: "64",
  height: "64",
  viewBox: "0 0 64 64"
};

var setActiveStateOfNavLink = function setActiveStateOfNavLink(item, index, path) {
  var active;

  if (index === 0 && path === '/') {
    // By convention, the first section should be active when at the root path
    active = true;
  } else {
    active = !!(0, _reactRouter.matchPath)(path, {
      path: item.path
    });
  }

  return _objectSpread(_objectSpread({}, item), {}, {
    active: active
  });
};

var Header = function Header(_ref) {
  var primaryTitle = _ref.primaryTitle,
      secondaryTitle = _ref.secondaryTitle,
      navItems = _ref.navItems,
      _ref$logoutPath = _ref.logoutPath,
      logoutPath = _ref$logoutPath === void 0 ? '/logout' : _ref$logoutPath,
      userFirstName = _ref.userFirstName,
      _ref$showPrimaryNavIt = _ref.showPrimaryNavItems,
      showPrimaryNavItems = _ref$showPrimaryNavIt === void 0 ? true : _ref$showPrimaryNavIt;

  var _useState = (0, _react.useState)(false),
      _useState2 = _slicedToArray(_useState, 2),
      mobileMenuIsVisible = _useState2[0],
      setMobileMenuIsVisible = _useState2[1];

  var location = (0, _reactRouter.useLocation)();

  function hideMenu() {
    setMobileMenuIsVisible(false);
  }

  (0, _react.useEffect)(function () {
    window.addEventListener('resize', hideMenu);
    return function cleanup() {
      window.removeEventListener('resize', hideMenu);
    };
  }, []);
  (0, _react.useEffect)(function () {
    hideMenu();
  }, [location]);
  var primaryNavItems = navItems.filter(function (item) {
    return item.type === 'primary';
  }).map(function (item, index) {
    return setActiveStateOfNavLink(item, index, location.pathname);
  });
  var secondaryNavItems = navItems.filter(function (item) {
    return item.type === 'secondary';
  });
  return /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)({
      'usa-js-mobile-nav--active': mobileMenuIsVisible
    })
  }, /*#__PURE__*/_react.default.createElement("div", {
    "data-testid": "overlay",
    className: (0, _classnames.default)('usa-overlay', {
      'is-visible': mobileMenuIsVisible
    }),
    onClick: hideMenu
  }), /*#__PURE__*/_react.default.createElement("header", {
    className: (0, _classnames.default)('usa-header', 'usa-header--extended', 'usa-header--oec-default'),
    role: "banner"
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: "usa-navbar"
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: "usa-logo",
    id: "extended-logo"
  }, /*#__PURE__*/_react.default.createElement(_reactRouterDom.Link, {
    to: "/",
    "aria-label": "".concat(primaryTitle, " home")
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)('usa-logo__text', 'display-flex', 'flex-align-center')
  }, /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)('primary-title', {
      'primary-title--only': !secondaryTitle
    })
  }, primaryTitle), secondaryTitle && /*#__PURE__*/_react.default.createElement("div", {
    className: "secondary-title"
  }, secondaryTitle))))), /*#__PURE__*/_react.default.createElement("button", {
    className: "usa-menu-btn",
    onClick: function onClick() {
      return setMobileMenuIsVisible(!mobileMenuIsVisible);
    }
  }, "Menu")), /*#__PURE__*/_react.default.createElement("nav", {
    "aria-label": "Primary navigation",
    className: (0, _classnames.default)('usa-nav', {
      'is-visible': mobileMenuIsVisible
    })
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: "usa-nav__inner"
  }, /*#__PURE__*/_react.default.createElement("button", {
    className: "usa-nav__close",
    onClick: hideMenu
  }, /*#__PURE__*/_react.default.createElement("img", {
    src: closeIcon,
    alt: "close"
  })), showPrimaryNavItems && /*#__PURE__*/_react.default.createElement("ul", {
    className: "usa-nav__primary usa-accordion"
  }, primaryNavItems.map(function (item, index) {
    return item.children ? /*#__PURE__*/_react.default.createElement(_NavDropdown.NavDropdown, _extends({}, item, {
      key: index
    })) : /*#__PURE__*/_react.default.createElement(_NavLink.NavLink, _extends({}, item, {
      key: index
    }));
  })), /*#__PURE__*/_react.default.createElement("div", {
    className: "usa-nav__secondary usa-nav__secondary--extended"
  }, /*#__PURE__*/_react.default.createElement("ul", {
    className: "usa-nav__secondary-links"
  }, secondaryNavItems.map(function (item, index) {
    return item.children ? /*#__PURE__*/_react.default.createElement(_NavDropdown.NavDropdown, _extends({}, item, {
      key: index
    })) : /*#__PURE__*/_react.default.createElement(_NavLink.NavLink, _extends({}, item, {
      key: index
    }));
  }), userFirstName && /*#__PURE__*/_react.default.createElement(_NavLink.NavLink, {
    type: "secondary",
    text: "Log out",
    path: logoutPath
  })))))));
};

exports.Header = Header;
var _default = Header;
exports.default = _default;