"use strict";

exports.__esModule = true;
exports.default = exports.MenuButton = exports.StyledMenu = exports.useTabScroll = exports.ScrollableBar = exports.InnerBar = exports.TabBar = 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(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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; }

(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(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; }

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) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_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; }

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 = _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"));

exports.TabBar = TabBar;

var 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;"]);

exports.InnerBar = InnerBar;

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 = _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));
});

exports.ScrollableBar = ScrollableBar;

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 = 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])
  });
};

exports.useTabScroll = useTabScroll;

__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 = (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;"]);
exports.StyledMenu = StyledMenu;
var 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"
})([""]);
exports.MenuButton = MenuButton;

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 = _default2;
exports.default = _default3;
;

(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);
})();