"use strict";

exports.__esModule = true;
exports.useTabScroll = exports.default = exports.TabBar = exports.StyledMenu = exports.ScrollableBar = exports.MenuButton = exports.InnerBar = void 0;
var _react = _interopRequireWildcard(require("react"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _reactRouterDom = require("react-router-dom");
var _utils = require("../../utils");
var _DropMenu = _interopRequireDefault(require("../DropMenu"));
var _IconButton = _interopRequireDefault(require("../IconButton"));
var _Tab = _interopRequireDefault(require("./Tab"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
var TabBar = exports.TabBar = _styledComponents.default.div.withConfig({
  displayName: "Bar__TabBar",
  componentId: "sc-9rl7eq-0"
})(["flex:0 0 10px;max-height:0px;padding:0 0 0 10px;margin:0 200px 0 0;display:flex;align-items:flex-end;width:calc(100% - 210px);font-family:", ";font-size:13px;text-transform:uppercase;"], (0, _utils.getThemeProp)(["fonts", "header"], "sans-serif"));
var InnerBar = exports.InnerBar = _styledComponents.default.div.withConfig({
  displayName: "Bar__InnerBar",
  componentId: "sc-9rl7eq-1"
})(["position:relative;flex:0 1 100%;margin-left:-10px;padding-left:10px;z-index:0;overflow:hidden;min-width:0;box-sizing:border-box;height:50px;display:flex;align-items:flex-end;scroll-behavior:smooth;"]);
var Fade = _styledComponents.default.div.withConfig({
  displayName: "Bar__Fade",
  componentId: "sc-9rl7eq-2"
})(["position:absolute;left:0;bottom:11px;height:38px;width:10px;z-index:1;background-color:red;box-shadow:0px 0 5px 5px #fff;clip-path:polygon(-10px 0,-10px 100%,30px 100%,30px 0);visibility:", ";"], (0, _utils.ifFlag)("scroll", "visible", "hidden"));
var ScrollableBar = exports.ScrollableBar = _react.default.forwardRef(function (_ref, ref) {
  var children = _ref.children;
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(Fade, {
    scroll: 0
  }), /*#__PURE__*/_react.default.createElement(InnerBar, {
    id: "navigationPageTabs",
    ref: ref
  }, children));
});
var getTabEdges = function getTabEdges(pages, tabRefs) {
  var lastWidth = 0;
  return pages.map(function (_ref2) {
    var href = _ref2.href;
    var tabWidth = (0, _utils.safeGet)(tabRefs, href, "offsetWidth") || 0;
    lastWidth += tabWidth;
    return lastWidth;
  });
};
var useTabScroll = exports.useTabScroll = function useTabScroll(pages, debug, refs) {
  if (debug === void 0) {
    debug = false;
  }
  var barRef = (0, _react.useRef)(null);
  var tabRefs = (0, _react.useRef)({});
  if (debug) {
    barRef = refs.barRef;
    tabRefs = refs.tabRefs;
  }
  var _useState = (0, _react.useState)(0),
    _useState2 = _slicedToArray(_useState, 2),
    barWidth = _useState2[0],
    setBarWidth = _useState2[1];
  var _useState3 = (0, _react.useState)([]),
    _useState4 = _slicedToArray(_useState3, 2),
    tabEdges = _useState4[0],
    setTabEdges = _useState4[1];
  var setWidthOfBar = (0, _react.useCallback)(function () {
    setBarWidth((0, _utils.safeGet)(barRef, "current", "offsetWidth") || 0);
  }, [barRef, setBarWidth]);
  var setEdgesOfTabs = (0, _react.useCallback)(function () {
    setTabEdges(getTabEdges(pages, tabRefs.current));
  }, [pages, tabRefs, setTabEdges]);
  (0, _react.useEffect)(function () {
    setWidthOfBar();
    window.addEventListener("resize", setWidthOfBar);
    return function () {
      return window.removeEventListener("resize", setWidthOfBar);
    };
  }, [pages, setWidthOfBar]);
  (0, _react.useEffect)(function () {
    setEdgesOfTabs(pages);
    window.addEventListener("resize", setEdgesOfTabs);
    return function () {
      return window.removeEventListener("resize", setEdgesOfTabs);
    };
  }, [pages, setEdgesOfTabs]);
  var lastActiveIndex = (0, _react.useRef)(-1);
  var activeIndex = pages.findIndex(function (page) {
    return page.active;
  });
  if (activeIndex === -1) {
    activeIndex = lastActiveIndex.current;
  } else {
    lastActiveIndex.current = activeIndex;
  }
  var _useState5 = (0, _react.useState)(pages.length),
    _useState6 = _slicedToArray(_useState5, 2),
    lastShownTab = _useState6[0],
    setLastShown = _useState6[1];
  (0, _react.useEffect)(function () {
    var barElem = barRef.current;
    /* istanbul ignore if */
    if (!barElem) return;
    var activeEdge = tabEdges[activeIndex + 1] || tabEdges[activeIndex] || 0;
    var scrollEdge = Math.max(activeEdge - barWidth + 7, 0);
    barElem.scrollLeft = scrollEdge;
    var edgeIndex = tabEdges.findIndex(function (edge) {
      return edge > barWidth + barElem.scrollLeft - 10;
    }) - 1;
    if (tabEdges[tabEdges.length - 1] <= barWidth) {
      setLastShown(pages.length);
    } else if (edgeIndex > activeIndex) {
      setLastShown(edgeIndex);
    } else {
      setLastShown(activeIndex + 1);
    }
  }, [setLastShown, pages, barRef, barWidth, tabEdges, activeIndex]);
  var history = (0, _reactRouterDom.useHistory)();
  return _objectSpread(_objectSpread({}, debug ? {
    barWidth: barWidth,
    tabEdges: tabEdges
  } : {}), {}, {
    hiddenTabs: barWidth < tabEdges[tabEdges.length - 1],
    lastShownTab: lastShownTab,
    tabMenuItems: pages.map(function (_ref3) {
      var label = _ref3.label,
        href = _ref3.href;
      return {
        id: href,
        label: label,
        handler: function handler() {
          history.push(href);
        }
      };
    }),
    getTabRef: (0, _react.useCallback)(function (node) {
      /* istanbul ignore else */
      if (node) {
        var href = (0, _utils.safeGet)(node, "dataset", "href");
        tabRefs.current[href] = node;
      }
    }, [tabRefs]),
    getBarRef: (0, _react.useCallback)(function (node) {
      /* istanbul ignore else */
      if (node && node !== barRef.current) {
        barRef.current = node;
      }
    }, [barRef])
  });
};
__signature__(useTabScroll, "useRef{barRef}\nuseRef{tabRefs}\nuseState{[barWidth, setBarWidth](0)}\nuseState{[tabEdges, setTabEdges]([])}\nuseCallback{setWidthOfBar}\nuseCallback{setEdgesOfTabs}\nuseEffect{}\nuseEffect{}\nuseRef{lastActiveIndex}\nuseState{[lastShownTab, setLastShown](pages.length)}\nuseEffect{}\nuseHistory{history}\nuseCallback{}\nuseCallback{}", function () {
  return [_reactRouterDom.useHistory];
});
__signature__(useTabScroll, "useRef{barRef}\nuseRef{tabRefs}\nuseState{[barWidth, setBarWidth](0)}\nuseState{[tabEdges, setTabEdges]([])}\nuseCallback{setWidthOfBar}\nuseCallback{setEdgesOfTabs}\nuseEffect{}\nuseEffect{}\nuseRef{lastActiveIndex}\nuseState{[lastShownTab, setLastShown](pages.length)}\nuseEffect{}\nuseHistory{history}\nuseCallback{}\nuseCallback{}", function () {
  return [_reactRouterDom.useHistory];
});
var StyledMenu = exports.StyledMenu = (0, _styledComponents.default)(_DropMenu.default).attrs(function () {
  return {
    alignRight: true
  };
}).withConfig({
  displayName: "Bar__StyledMenu",
  componentId: "sc-9rl7eq-3"
})(["position:absolute;top:10px;right:140px;text-transform:none;"]);
var MenuButton = exports.MenuButton = (0, _styledComponents.default)(_IconButton.default).attrs(function (props) {
  return {
    icon: (0, _utils.ifFlag)("open", (0, _utils.getThemeProp)(["icons", "indicators", "more"], "arrow-more"), (0, _utils.getThemeProp)(["icons", "indicators", "more"], "arrow-more"))(props)
  };
}).withConfig({
  displayName: "Bar__MenuButton",
  componentId: "sc-9rl7eq-4"
})([""]);
var Bar = function Bar(_ref4) {
  var module = _ref4.module,
    pages = _ref4.pages;
  var _useTabScroll = useTabScroll(pages),
    hiddenTabs = _useTabScroll.hiddenTabs,
    tabMenuItems = _useTabScroll.tabMenuItems,
    lastShownTab = _useTabScroll.lastShownTab,
    getTabRef = _useTabScroll.getTabRef,
    getBarRef = _useTabScroll.getBarRef;
  return /*#__PURE__*/_react.default.createElement(TabBar, null, /*#__PURE__*/_react.default.createElement(_Tab.default, {
    module: true,
    icon: module.icon,
    href: module.href,
    mappedFrom: module.mappedFrom,
    label: module.label,
    active: module.active
  }), /*#__PURE__*/_react.default.createElement(ScrollableBar, {
    ref: getBarRef
  }, pages.map(function (_ref5, index) {
    var href = _ref5.href,
      mappedFrom = _ref5.mappedFrom,
      label = _ref5.label,
      mustTruncate = _ref5.mustTruncate,
      active = _ref5.active,
      icon = _ref5.icon,
      outsideScope = _ref5.outsideScope,
      scopeNotSupported = _ref5.scopeNotSupported,
      close = _ref5.close;
    return /*#__PURE__*/_react.default.createElement(_Tab.default, {
      ref: getTabRef,
      key: href,
      icon: icon,
      href: href,
      mappedFrom: mappedFrom,
      label: label,
      mustTruncate: mustTruncate,
      active: active,
      close: close,
      outsideScope: outsideScope,
      scopeNotSupported: scopeNotSupported,
      hide: index > lastShownTab
    });
  })), hiddenTabs ? /*#__PURE__*/_react.default.createElement(StyledMenu, {
    id: "navigationTabs",
    menuItems: tabMenuItems
  }, /*#__PURE__*/_react.default.createElement(MenuButton, null)) : null);
};
__signature__(Bar, "useTabScroll{{ hiddenTabs, tabMenuItems, lastShownTab, getTabRef, getBarRef }}", function () {
  return [useTabScroll];
});
__signature__(Bar, "useTabScroll{{ hiddenTabs, tabMenuItems, lastShownTab, getTabRef, getBarRef }}", function () {
  return [useTabScroll];
});
var _default = Bar;
var _default2 = _default;
var _default3 = exports.default = _default2;
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(TabBar, "TabBar", "/home/vsts/work/1/s/src/components/Navigation/Bar.js");
  reactHotLoader.register(InnerBar, "InnerBar", "/home/vsts/work/1/s/src/components/Navigation/Bar.js");
  reactHotLoader.register(Fade, "Fade", "/home/vsts/work/1/s/src/components/Navigation/Bar.js");
  reactHotLoader.register(ScrollableBar, "ScrollableBar", "/home/vsts/work/1/s/src/components/Navigation/Bar.js");
  reactHotLoader.register(getTabEdges, "getTabEdges", "/home/vsts/work/1/s/src/components/Navigation/Bar.js");
  reactHotLoader.register(useTabScroll, "useTabScroll", "/home/vsts/work/1/s/src/components/Navigation/Bar.js");
  reactHotLoader.register(StyledMenu, "StyledMenu", "/home/vsts/work/1/s/src/components/Navigation/Bar.js");
  reactHotLoader.register(MenuButton, "MenuButton", "/home/vsts/work/1/s/src/components/Navigation/Bar.js");
  reactHotLoader.register(Bar, "Bar", "/home/vsts/work/1/s/src/components/Navigation/Bar.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/components/Navigation/Bar.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(TabBar, "TabBar", "/home/vsts/work/1/s/src/components/Navigation/Bar.js");
  reactHotLoader.register(InnerBar, "InnerBar", "/home/vsts/work/1/s/src/components/Navigation/Bar.js");
  reactHotLoader.register(Fade, "Fade", "/home/vsts/work/1/s/src/components/Navigation/Bar.js");
  reactHotLoader.register(ScrollableBar, "ScrollableBar", "/home/vsts/work/1/s/src/components/Navigation/Bar.js");
  reactHotLoader.register(getTabEdges, "getTabEdges", "/home/vsts/work/1/s/src/components/Navigation/Bar.js");
  reactHotLoader.register(useTabScroll, "useTabScroll", "/home/vsts/work/1/s/src/components/Navigation/Bar.js");
  reactHotLoader.register(StyledMenu, "StyledMenu", "/home/vsts/work/1/s/src/components/Navigation/Bar.js");
  reactHotLoader.register(MenuButton, "MenuButton", "/home/vsts/work/1/s/src/components/Navigation/Bar.js");
  reactHotLoader.register(Bar, "Bar", "/home/vsts/work/1/s/src/components/Navigation/Bar.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/components/Navigation/Bar.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();