"use strict";

exports.__esModule = true;
exports.default = void 0;
var _immutable = _interopRequireDefault(require("immutable"));
var _navigation = require("../actions/navigation");
var _parseHelper = require("../utils/parseHelper");
var _scopes = require("../actions/scopes");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
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 initialState = _immutable.default.fromJS({
  route: {},
  tabIndex: {},
  moduleTabs: {},
  mappedHrefs: {},
  config: {},
  currentPrependPath: null,
  closingTabsHandlerActions: {}
});
var navigationReducer = function navigationReducer(state, action) {
  if (state === void 0) {
    state = initialState;
  }
  switch (action.type) {
    case _navigation.SET_ROUTE:
      return state.withMutations(function (s) {
        var prependPath = state.get("currentPrependPath") || state.getIn(["config", "prependPath"]);
        s.set("route", _immutable.default.fromJS(action.payload));
        var _action$payload = action.payload,
          href = _action$payload.location.pathname,
          _action$payload$match = _action$payload.match,
          path = _action$payload$match.path,
          params = _action$payload$match.params;
        var remainingSection = (0, _parseHelper.getAllAfterPrependHref)(prependPath, href);
        if (remainingSection) {
          s.setIn(["tabIndex", remainingSection], _immutable.default.fromJS({
            href: href,
            path: path,
            params: params
          }));
          var moduleName = path.replace(new RegExp("^".concat(prependPath, "(\\w+)(?:/.*)?$")), "$1");
          var moduleList = s.getIn(["moduleTabs", moduleName]) || _immutable.default.List();
          if (!moduleList.includes(remainingSection)) {
            s.setIn(["moduleTabs", moduleName], moduleList.push(remainingSection));
          }
        }
      });
    case _navigation.MAP_HREF:
      var prependPath = state.get("currentPrependPath") || state.getIn(["config", "prependPath"]);
      var _action$payload2 = action.payload,
        from = _action$payload2.from,
        to = _action$payload2.to;
      var remainingSectionFrom = (0, _parseHelper.getAllAfterPrependHref)(prependPath, from);
      var remainingSectionTo = (0, _parseHelper.getAllAfterPrependHref)(prependPath, to);
      if (remainingSectionFrom && remainingSectionTo) return state.setIn(["mappedHrefs", remainingSectionFrom], remainingSectionTo);
      return state;
    case _navigation.REMOVE_TAB:
      return state.withMutations(function (s) {
        var prependPath = s.get("currentPrependPath") || s.getIn(["config", "prependPath"]);
        var _action$payload3 = action.payload,
          module = _action$payload3.module,
          path = _action$payload3.path;
        var list = s.getIn(["moduleTabs", module]) || _immutable.default.List();
        var remainingSection = (0, _parseHelper.getAllAfterPrependHref)(prependPath, path);
        if (remainingSection) {
          var index = list.indexOf(remainingSection);
          if (index !== -1) {
            s.deleteIn(["moduleTabs", module, index]);
          }
          s.deleteIn(["tabIndex", remainingSection]);
        }
      });
    case _navigation.REMOVE_MODULE_TABS:
      return state.withMutations(function (s) {
        var module = action.payload.module;
        s.deleteIn(["moduleTabs", module]);
        s.deleteIn(["closingTabsHandlerActions", module]);
        s.get("tabIndex").keySeq().forEach(function (key) {
          if (key.startsWith(module)) {
            s.deleteIn(["tabIndex", key]);
          }
        });
        s.get("mappedHrefs").keySeq().forEach(function (key) {
          if (key.startsWith(module)) {
            s.deleteIn(["mappedHrefs", key]);
          }
        });
      });
    case _navigation.SET_HREF_CONFIG:
      return state.set("config", _immutable.default.fromJS(action.payload));
    case _navigation.SET_CURRENT_PREPEND_PATH:
      return state.set("currentPrependPath", action.payload);
    case _scopes.APPLICATION_SCOPE_HAS_CHANGED:
      return state.withMutations(function (s) {
        return s.set("moduleTabs", _immutable.default.fromJS({})).set("tabIndex", _immutable.default.fromJS({})).set("mappedHrefs", _immutable.default.fromJS({}));
      });
    case _navigation.SET_CLOSING_TAB_HANDLER_ACTIONS:
      return state.setIn(["closingTabsHandlerActions", action.payload.module], _immutable.default.fromJS(action.payload.actions));
    default:
      return state;
  }
};
var _default = navigationReducer;
var _default2 = _default;
var _default3 = exports.default = _default2;
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(initialState, "initialState", "/home/vsts/work/1/s/src/reducers/navigation.js");
  reactHotLoader.register(navigationReducer, "navigationReducer", "/home/vsts/work/1/s/src/reducers/navigation.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/reducers/navigation.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(initialState, "initialState", "/home/vsts/work/1/s/src/reducers/navigation.js");
  reactHotLoader.register(navigationReducer, "navigationReducer", "/home/vsts/work/1/s/src/reducers/navigation.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/reducers/navigation.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();