"use strict";

exports.__esModule = true;
exports.selectTabGetter = exports.selectSegmentHrefMapper = exports.selectRoutePath = exports.selectRouteParams = exports.selectRouteHref = exports.selectPrependPathConfig = exports.selectPrependHrefConfig = exports.selectMappedCurrentModuleList = exports.selectCurrentSectionName = exports.selectCurrentModuleName = exports.selectClosingTabHandlerActions = exports.selectClosingTabHandlerActionForEntity = exports.resetLastScope = exports.hasOpenedTabs = exports.getCurrentScopeFromRoute = exports.getCurrentScope = void 0;
var _reselect = require("reselect");
var _immutable = _interopRequireDefault(require("immutable"));
var _settings = require("./settings");
var _parseHelper = require("../utils/parseHelper");
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 getNavigationState = function getNavigationState(state) {
  return state.get("navigation");
};
var selectRoute = (0, _reselect.createSelector)(getNavigationState, function (nav) {
  return nav.get("route");
});

// const selectLocation = createSelector(
// 	selectRoute,
// 	route => route.get("location") || Immutable.Map(),
// );
var selectMatch = (0, _reselect.createSelector)(selectRoute, function (route) {
  return route.get("match") || _immutable.default.Map();
});
var selectRouteParams = exports.selectRouteParams = (0, _reselect.createSelector)(selectMatch, function (match) {
  return match.get("params") || _immutable.default.Map();
});
var selectRoutePath = exports.selectRoutePath = (0, _reselect.createSelector)(selectMatch, function (match) {
  return match.get("path") || "";
});
var selectRouteHref = exports.selectRouteHref = (0, _reselect.createSelector)(selectMatch, function (match) {
  return match.get("url") || "";
});

// Not a selector, as previous calls can change the result.
var lastScope;
var resetLastScope = exports.resetLastScope = function resetLastScope() {
  lastScope = undefined;
};
var getLastRouteScope = function getLastRouteScope(state) {
  var params = selectRouteParams(state);
  if (params.get("scope")) {
    lastScope = params.get("scope");
  }
  return lastScope;
};
var getCurrentScope = exports.getCurrentScope = (0, _reselect.createSelector)(getLastRouteScope, _settings.defaultScopeSelector, function (id, defaultScope) {
  return id || defaultScope || "Global";
});
var getCurrentScopeFromRoute = exports.getCurrentScopeFromRoute = (0, _reselect.createSelector)(getLastRouteScope, function (scope) {
  return scope || null;
});
var selectTabs = (0, _reselect.createSelector)(getNavigationState, function (nav) {
  return nav.get("tabIndex");
});
var selectTabGetter = exports.selectTabGetter = (0, _reselect.createSelector)(selectTabs, function (tabs) {
  return function (path) {
    return tabs.get(path);
  };
});
var selectModuleLists = (0, _reselect.createSelector)(getNavigationState, function (nav) {
  return nav.get("moduleTabs");
});
var selectHrefConfig = (0, _reselect.createSelector)(getNavigationState, function (state) {
  return state.get("config");
});
var selectCurrentPrependPath = (0, _reselect.createSelector)(getNavigationState, function (state) {
  return state.get("currentPrependPath");
});
var selectClosingTabHandlerActions = exports.selectClosingTabHandlerActions = (0, _reselect.createSelector)(getNavigationState, function (nav) {
  var _nav$get$toJS, _nav$get;
  var moduleActions = (_nav$get$toJS = (_nav$get = nav.get("closingTabsHandlerActions")) == null ? void 0 : _nav$get.toJS()) != null ? _nav$get$toJS : {};
  return Object.values(moduleActions).reduce(function (a, b) {
    return a.concat(b);
  }, []);
});
var selectClosingTabHandlerActionForEntity = exports.selectClosingTabHandlerActionForEntity = function selectClosingTabHandlerActionForEntity(module, entityId) {
  return (0, _reselect.createSelector)(getNavigationState, function (nav) {
    var _nav$getIn$toJS, _nav$getIn, _actions$find$closeTa, _actions$find;
    var actions = (_nav$getIn$toJS = (_nav$getIn = nav.getIn(["closingTabsHandlerActions", module])) == null ? void 0 : _nav$getIn.toJS()) != null ? _nav$getIn$toJS : [];
    return (_actions$find$closeTa = (_actions$find = actions.find(function (x) {
      return x.entityId === entityId;
    })) == null ? void 0 : _actions$find.closeTab) != null ? _actions$find$closeTa : null;
  });
};
var selectPrependPathConfig = exports.selectPrependPathConfig = (0, _reselect.createSelector)(selectHrefConfig, selectCurrentPrependPath, function (config, currentPath) {
  return currentPath || config.get("prependPath");
});
var selectPrependHrefConfig = exports.selectPrependHrefConfig = (0, _reselect.createSelector)(selectHrefConfig, function (config) {
  return function (name) {
    return config.getIn([name, "prependHref"]) || config.get("prependHref");
  };
});
var selectCurrentModuleName = exports.selectCurrentModuleName = (0, _reselect.createSelector)(selectPrependPathConfig, selectRoutePath, function (prependPath, path) {
  return new RegExp("^".concat(prependPath)).test(path) ? path.replace(new RegExp("^".concat(prependPath, "([^/]+)(/.*)?$")), "$1") : "";
});
var selectCurrentSectionName = exports.selectCurrentSectionName = (0, _reselect.createSelector)(selectPrependPathConfig, selectRoutePath, function (prependPath, path) {
  //strips the scope and module
  var segmentPath = path.replace(prependPath, "").replace(new RegExp("^([^/]*/){1}"), "");

  //strips param paths
  while (segmentPath.includes(":") && segmentPath.includes("/")) {
    segmentPath = segmentPath.replace(new RegExp("^([^/]*/){1}"), "");
  }

  //strips entity id and deeper pages
  return segmentPath.replace(new RegExp("^([^/]*/){1}"), "").replace(new RegExp("/.*$"), "");
});
var selectCurrentModuleList = (0, _reselect.createSelector)(selectModuleLists, selectCurrentModuleName, function (lists, module) {
  return lists.get(module) || _immutable.default.List();
});
var selectMappedCurrentModuleList = exports.selectMappedCurrentModuleList = (0, _reselect.createSelector)(selectCurrentModuleList, selectTabGetter, function (list, getTab) {
  return list.map(getTab);
});
var segmentHrefMap = (0, _reselect.createSelector)(getNavigationState, function (state) {
  return state.get("mappedHrefs");
});
var selectSegmentHrefMapper = exports.selectSegmentHrefMapper = (0, _reselect.createSelector)(selectPrependPathConfig, segmentHrefMap, function (prependPath, map) {
  return function (href) {
    var otherPath = (0, _parseHelper.getAllAfterPrependHref)(prependPath, href);
    var newPrependPath = href.replace(otherPath, "");
    var hrefMap = map.get(otherPath);
    return hrefMap ? newPrependPath.concat(hrefMap) : href;
  };
});
var hasOpenedTabs = exports.hasOpenedTabs = (0, _reselect.createSelector)(selectModuleLists, function (lists) {
  if (!lists) {
    return false;
  }
  return lists.some(function (module, key) {
    return module.some(function (tab) {
      return tab !== key;
    });
  });
});
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(getNavigationState, "getNavigationState", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectRoute, "selectRoute", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectMatch, "selectMatch", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectRouteParams, "selectRouteParams", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectRoutePath, "selectRoutePath", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectRouteHref, "selectRouteHref", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(lastScope, "lastScope", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(resetLastScope, "resetLastScope", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(getLastRouteScope, "getLastRouteScope", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(getCurrentScope, "getCurrentScope", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(getCurrentScopeFromRoute, "getCurrentScopeFromRoute", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectTabs, "selectTabs", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectTabGetter, "selectTabGetter", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectModuleLists, "selectModuleLists", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectHrefConfig, "selectHrefConfig", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectCurrentPrependPath, "selectCurrentPrependPath", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectClosingTabHandlerActions, "selectClosingTabHandlerActions", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectClosingTabHandlerActionForEntity, "selectClosingTabHandlerActionForEntity", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectPrependPathConfig, "selectPrependPathConfig", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectPrependHrefConfig, "selectPrependHrefConfig", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectCurrentModuleName, "selectCurrentModuleName", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectCurrentSectionName, "selectCurrentSectionName", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectCurrentModuleList, "selectCurrentModuleList", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectMappedCurrentModuleList, "selectMappedCurrentModuleList", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(segmentHrefMap, "segmentHrefMap", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectSegmentHrefMapper, "selectSegmentHrefMapper", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(hasOpenedTabs, "hasOpenedTabs", "/home/vsts/work/1/s/src/selectors/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(getNavigationState, "getNavigationState", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectRoute, "selectRoute", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectMatch, "selectMatch", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectRouteParams, "selectRouteParams", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectRoutePath, "selectRoutePath", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectRouteHref, "selectRouteHref", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(lastScope, "lastScope", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(resetLastScope, "resetLastScope", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(getLastRouteScope, "getLastRouteScope", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(getCurrentScope, "getCurrentScope", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(getCurrentScopeFromRoute, "getCurrentScopeFromRoute", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectTabs, "selectTabs", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectTabGetter, "selectTabGetter", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectModuleLists, "selectModuleLists", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectHrefConfig, "selectHrefConfig", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectCurrentPrependPath, "selectCurrentPrependPath", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectClosingTabHandlerActions, "selectClosingTabHandlerActions", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectClosingTabHandlerActionForEntity, "selectClosingTabHandlerActionForEntity", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectPrependPathConfig, "selectPrependPathConfig", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectPrependHrefConfig, "selectPrependHrefConfig", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectCurrentModuleName, "selectCurrentModuleName", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectCurrentSectionName, "selectCurrentSectionName", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectCurrentModuleList, "selectCurrentModuleList", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectMappedCurrentModuleList, "selectMappedCurrentModuleList", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(segmentHrefMap, "segmentHrefMap", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(selectSegmentHrefMapper, "selectSegmentHrefMapper", "/home/vsts/work/1/s/src/selectors/navigation.js");
  reactHotLoader.register(hasOpenedTabs, "hasOpenedTabs", "/home/vsts/work/1/s/src/selectors/navigation.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();