"use strict";

exports.__esModule = true;
exports.default = exports.Modules = exports.Module = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactRouterDom = require("react-router-dom");
var _reactRedux = require("react-redux");
var _withErrorBoundary = _interopRequireDefault(require("../hocs/withErrorBoundary"));
var _navigation = require("../selectors/navigation");
var _scope = require("../selectors/scope");
var _Navigation = _interopRequireDefault(require("./Navigation"));
var _FullPage = _interopRequireDefault(require("./Routing/FullPage"));
var _navigation2 = require("../actions/navigation");
var _modules = require("../actions/modules");
var _settings = require("../selectors/settings");
var _modules2 = require("../selectors/modules");
var _urlPattern = _interopRequireDefault(require("url-pattern"));
var _constants = require("../constants");
var _excluded = ["customPath"];
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 _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
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; }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
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 rerouteOnScopeAndModule = function rerouteOnScopeAndModule(history, currentRoute, scope, module) {
  var params = {
    scope: scope
  };
  var pattern = new _urlPattern.default("/:scope/".concat(module));
  var href = pattern.stringify(params);
  if (currentRoute !== href) {
    history.push(href);
  }
};
var Module = exports.Module = (0, _withErrorBoundary.default)("Module")(__signature__(__signature__(function (_ref) {
  var id = _ref.id,
    config = _ref.config,
    path = _ref.path,
    error = _ref.error,
    location = _ref.location,
    match = _ref.match,
    modulePrependPath = _ref.modulePrependPath;
  var history = (0, _reactRouterDom.useHistory)();
  var scope = (0, _reactRedux.useSelector)(_navigation.getCurrentScopeFromRoute);
  var currentRoute = (0, _reactRedux.useSelector)(_navigation.selectRouteHref);
  var moduleInfo = (0, _reactRedux.useSelector)(_modules2.getScopeModuleInformationSelector);
  var isVisible = moduleInfo.scope != null && moduleInfo.visibleModules.length > 0 && moduleInfo.visibleModules.includes(id);
  _react.default.useEffect(function () {
    if (moduleInfo.scope != null && scope === moduleInfo.scope && moduleInfo.visibleModules.length > 0 && !isVisible) {
      rerouteOnScopeAndModule(history, currentRoute, moduleInfo.scope, moduleInfo.visibleModules[0]);
    }
  }, [history, scope, isVisible, currentRoute, moduleInfo.scope, moduleInfo.visibleModules]);
  return /*#__PURE__*/_react.default.createElement(_FullPage.default, {
    path: path,
    config: config,
    location: location,
    match: match,
    modulePrependPath: modulePrependPath,
    isVisible: isVisible
  });
}, "useHistory{history}\nuseSelector{scope}\nuseSelector{currentRoute}\nuseSelector{moduleInfo}\nuseEffect{}", function () {
  return [_reactRouterDom.useHistory, _reactRedux.useSelector, _reactRedux.useSelector, _reactRedux.useSelector];
}), "useHistory{history}\nuseSelector{scope}\nuseSelector{currentRoute}\nuseSelector{moduleInfo}\nuseEffect{}", function () {
  return [_reactRouterDom.useHistory, _reactRedux.useSelector, _reactRedux.useSelector, _reactRedux.useSelector];
}));
var getHrefFromPath = function getHrefFromPath(path, scope) {
  return path.replace(":scope", scope);
};
var CheckModuleVisibility = function CheckModuleVisibility(_ref2) {
  var _moduleInfo$scope;
  var id = _ref2.id,
    config = _ref2.config,
    moduleInfo = _ref2.moduleInfo;
  var dispatch = (0, _reactRedux.useDispatch)();
  var applicationModules = (0, _reactRedux.useSelector)(_settings.getApplicationModulesSelector);
  var scopeFromRoute = (0, _reactRedux.useSelector)(_navigation.getCurrentScopeFromRoute);
  var hideSelector = function hideSelector(state) {
    return typeof config.hide === "function" ? config.hide(state) : function () {
      var _config$hide;
      return (_config$hide = config.hide) != null ? _config$hide : false;
    };
  };
  var isHidden = (0, _reactRedux.useSelector)(hideSelector((_moduleInfo$scope = moduleInfo.scope) != null ? _moduleInfo$scope : scopeFromRoute));
  var moduleScope = moduleInfo.scope;
  var moduleIsVisible = isHidden === false && !moduleInfo.visibleModules.includes(id);
  _react.default.useEffect(function () {
    // We need to wait for the ROUTE to be set the first time in the Redux/Store before to set module's visibility
    if (scopeFromRoute !== null || applicationModules.includes(_constants.overtureModule.System)) {
      if (moduleScope == null) {
        dispatch((0, _modules.initializeFirstModuleScope)(scopeFromRoute));
      }
      if (moduleIsVisible) {
        dispatch((0, _modules.setModuleAsVisible)(id));
      }
    }
  }, [moduleScope, moduleIsVisible, dispatch, id, scopeFromRoute, applicationModules]);
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null);
};
__signature__(CheckModuleVisibility, "useDispatch{dispatch}\nuseSelector{applicationModules}\nuseSelector{scopeFromRoute}\nuseSelector{isHidden}\nuseEffect{}", function () {
  return [_reactRedux.useDispatch, _reactRedux.useSelector, _reactRedux.useSelector, _reactRedux.useSelector];
});
__signature__(CheckModuleVisibility, "useDispatch{dispatch}\nuseSelector{applicationModules}\nuseSelector{scopeFromRoute}\nuseSelector{isHidden}\nuseEffect{}", function () {
  return [_reactRedux.useDispatch, _reactRedux.useSelector, _reactRedux.useSelector, _reactRedux.useSelector];
});
var Modules = exports.Modules = function Modules(_ref3) {
  var modules = _ref3.modules,
    _ref3$pathConfig = _ref3.pathConfig,
    _ref3$pathConfig2 = _ref3$pathConfig === void 0 ? {} : _ref3$pathConfig,
    customPath = _ref3$pathConfig2.customPath,
    otherConfigs = _objectWithoutProperties(_ref3$pathConfig2, _excluded);
  var dispatch = (0, _reactRedux.useDispatch)();
  var currentScope = (0, _reactRedux.useSelector)(_navigation.getCurrentScope);
  var isAuthorizedScope = (0, _reactRedux.useSelector)(_scope.isCurrentScopeAuthorizedSelector);
  var defaultScope = (0, _reactRedux.useSelector)(_settings.defaultScopeSelector);
  var history = (0, _reactRouterDom.useHistory)();
  var location = (0, _reactRouterDom.useLocation)();
  var currentRoute = (0, _reactRedux.useSelector)(_navigation.selectRouteHref);
  var moduleInfo = (0, _reactRedux.useSelector)(_modules2.getScopeModuleInformationSelector);
  var firstModuleName = Object.keys(modules)[0];
  var destinationModule = moduleInfo.routingPerformed === false && moduleInfo.scope != null && moduleInfo.visibleModules.length > 0 && moduleInfo.moduleName != null ? moduleInfo.visibleModules.includes(moduleInfo.moduleName) ? moduleInfo.moduleName : moduleInfo.visibleModules[0] : null;
  var scopePath = "/:scope/";
  var prependPath = customPath || scopePath;
  var prependHref = getHrefFromPath(prependPath, currentScope);
  Object.keys(otherConfigs).forEach(function (key) {
    var moduleConfig = otherConfigs[key];
    moduleConfig.prependHref = getHrefFromPath(moduleConfig.prependPath, currentScope);
  });
  (0, _react.useEffect)(function () {
    dispatch((0, _navigation2.setHrefConfig)(prependPath, prependHref, otherConfigs));
  }, [dispatch, prependPath, prependHref, otherConfigs]);
  var getModuleConfig = function getModuleConfig(name) {
    return otherConfigs && otherConfigs[name] || {
      prependPath: prependPath,
      prependHref: prependHref
    };
  };
  (0, _react.useEffect)(function () {
    var pathname = location.pathname,
      search = location.search;
    if (!isAuthorizedScope && pathname.includes(currentScope) && defaultScope) {
      history.push(pathname.replace(currentScope, defaultScope) + search);
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [isAuthorizedScope, defaultScope, history, currentScope]);
  _react.default.useEffect(function () {
    dispatch((0, _modules.setModulesStructure)(modules));
  }, [dispatch, modules]);
  (0, _react.useEffect)(function () {
    if (destinationModule != null) {
      rerouteOnScopeAndModule(history, currentRoute, moduleInfo.scope, destinationModule);
      dispatch((0, _modules.setRoutingPerformed)());
    }
  }, [dispatch, history, moduleInfo.scope, destinationModule, currentRoute]);
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_Navigation.default, {
    modules: modules
  }), Object.entries(modules).map(function (_ref4) {
    var _ref5 = _slicedToArray(_ref4, 2),
      name = _ref5[0],
      module = _ref5[1];
    return /*#__PURE__*/_react.default.createElement(CheckModuleVisibility, {
      key: name,
      id: name,
      config: module,
      moduleInfo: moduleInfo
    });
  }), /*#__PURE__*/_react.default.createElement(_reactRouterDom.Switch, null, Object.entries(modules).map(function (_ref6) {
    var _ref7 = _slicedToArray(_ref6, 2),
      name = _ref7[0],
      module = _ref7[1];
    var moduleConfig = getModuleConfig(name);
    var path = "".concat(moduleConfig.prependPath).concat(name);
    return /*#__PURE__*/_react.default.createElement(_reactRouterDom.Route, {
      key: name,
      path: path,
      render: function render(route) {
        return /*#__PURE__*/_react.default.createElement(Module, _extends({
          id: name,
          modulePrependPath: moduleConfig.prependPath,
          config: module,
          path: path
        }, route));
      }
    });
  }), /*#__PURE__*/_react.default.createElement(_reactRouterDom.Redirect, {
    to: "".concat(getModuleConfig(firstModuleName).prependHref).concat(firstModuleName)
  })));
};
__signature__(Modules, "useDispatch{dispatch}\nuseSelector{currentScope}\nuseSelector{isAuthorizedScope}\nuseSelector{defaultScope}\nuseHistory{history}\nuseLocation{location}\nuseSelector{currentRoute}\nuseSelector{moduleInfo}\nuseEffect{}\nuseEffect{}\nuseEffect{}\nuseEffect{}", function () {
  return [_reactRedux.useDispatch, _reactRedux.useSelector, _reactRedux.useSelector, _reactRedux.useSelector, _reactRouterDom.useHistory, _reactRouterDom.useLocation, _reactRedux.useSelector, _reactRedux.useSelector];
});
__signature__(Modules, "useDispatch{dispatch}\nuseSelector{currentScope}\nuseSelector{isAuthorizedScope}\nuseSelector{defaultScope}\nuseHistory{history}\nuseLocation{location}\nuseSelector{currentRoute}\nuseSelector{moduleInfo}\nuseEffect{}\nuseEffect{}\nuseEffect{}\nuseEffect{}", function () {
  return [_reactRedux.useDispatch, _reactRedux.useSelector, _reactRedux.useSelector, _reactRedux.useSelector, _reactRouterDom.useHistory, _reactRouterDom.useLocation, _reactRedux.useSelector, _reactRedux.useSelector];
});
var _default = Modules;
var _default2 = _default;
var _default3 = exports.default = _default2;
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(rerouteOnScopeAndModule, "rerouteOnScopeAndModule", "/home/vsts/work/1/s/src/components/Modules.js");
  reactHotLoader.register(Module, "Module", "/home/vsts/work/1/s/src/components/Modules.js");
  reactHotLoader.register(getHrefFromPath, "getHrefFromPath", "/home/vsts/work/1/s/src/components/Modules.js");
  reactHotLoader.register(CheckModuleVisibility, "CheckModuleVisibility", "/home/vsts/work/1/s/src/components/Modules.js");
  reactHotLoader.register(Modules, "Modules", "/home/vsts/work/1/s/src/components/Modules.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/components/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(rerouteOnScopeAndModule, "rerouteOnScopeAndModule", "/home/vsts/work/1/s/src/components/Modules.js");
  reactHotLoader.register(Module, "Module", "/home/vsts/work/1/s/src/components/Modules.js");
  reactHotLoader.register(getHrefFromPath, "getHrefFromPath", "/home/vsts/work/1/s/src/components/Modules.js");
  reactHotLoader.register(CheckModuleVisibility, "CheckModuleVisibility", "/home/vsts/work/1/s/src/components/Modules.js");
  reactHotLoader.register(Modules, "Modules", "/home/vsts/work/1/s/src/components/Modules.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/components/Modules.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();