"use strict";

exports.__esModule = true;
exports.default = void 0;
var _immutable = _interopRequireDefault(require("immutable"));
var _modules = require("../actions/modules");
var _constants = require("./../constants");
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({
  tree: {},
  visibleModules: [],
  lastScopeAndModuleSelection: {
    scope: null,
    moduleName: null,
    routingPerformed: true
  }
});
var viewStateReducer = function viewStateReducer(state, action) {
  if (state === void 0) {
    state = initialState;
  }
  switch (action.type) {
    case _modules.SET_MODULES_STRUCTURE:
      {
        var modules = action.payload;
        var modulesTree = {};
        var moduleNames = Object.keys(modules);
        for (var _i = 0, _moduleNames = moduleNames; _i < _moduleNames.length; _i++) {
          var moduleName = _moduleNames[_i];
          modulesTree[moduleName] = {};
          var modulePages = modules[moduleName].pages;
          if (modulePages != null) {
            modulesTree[moduleName].pages = {};
            var pagesKeys = Object.keys(modulePages);
            for (var _i2 = 0, _pagesKeys = pagesKeys; _i2 < _pagesKeys.length; _i2++) {
              var pageKey = _pagesKeys[_i2];
              modulesTree[moduleName].pages[pageKey] = {};
              var segments = modulePages[pageKey].segments;
              if (segments != null) {
                modulesTree[moduleName].pages[pageKey][_constants.infoBar] = {};
                var segmentsKeys = Object.keys(segments);
                for (var _i3 = 0, _segmentsKeys = segmentsKeys; _i3 < _segmentsKeys.length; _i3++) {
                  var segmentKey = _segmentsKeys[_i3];
                  modulesTree[moduleName].pages[pageKey][segmentKey.replace("/", "")] = {};
                }
              }
            }
          }
        }
        return state.set("tree", _immutable.default.fromJS(modulesTree));
      }
    case _modules.INITIALIZE_FIRST_MODULE_SCOPE:
      {
        return state.setIn(["lastScopeAndModuleSelection", "scope"], action.payload);
      }
    case _modules.SET_MODULE_AS_VISIBLE:
      {
        var visibleModules = state.get("visibleModules").toJS();
        visibleModules.push(action.payload);
        return state.set("visibleModules", _immutable.default.fromJS(visibleModules));
      }
    case _scopes.APPLICATION_SCOPE_HAS_CHANGED:
      {
        var lastScopeAndModuleSelection = state.get("lastScopeAndModuleSelection").toJS();
        if (action.payload.newScope !== lastScopeAndModuleSelection.scope) {
          state = state.set("visibleModules", _immutable.default.fromJS([]));
        }
        return state.set("lastScopeAndModuleSelection", _immutable.default.fromJS({
          moduleName: action.payload.moduleName,
          scope: action.payload.newScope,
          routingPerformed: false
        }));
      }
    case _modules.SET_ROUTING_PERFORMED:
      return state.setIn(["lastScopeAndModuleSelection", "routingPerformed"], true);
    default:
      return state;
  }
};
var _default = viewStateReducer;
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/modules.js");
  reactHotLoader.register(viewStateReducer, "viewStateReducer", "/home/vsts/work/1/s/src/reducers/modules.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/reducers/modules.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/modules.js");
  reactHotLoader.register(viewStateReducer, "viewStateReducer", "/home/vsts/work/1/s/src/reducers/modules.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/reducers/modules.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();