"use strict";

exports.__esModule = true;
exports.scopeGetter = exports.scopeDefaultCultureSelector = exports.localizedScopesSelectorByIds = exports.localizedScopesSelector = exports.localizedScopeSelector = exports.isCurrentScopeAuthorizedSelector = exports.getScopesSelector = exports.currentScopeSelector = void 0;
var _reselect = require("reselect");
var _immutable = _interopRequireDefault(require("immutable"));
var _navigation = require("./navigation");
var _locale = require("./locale");
var _utils = require("../utils");
var _localizationHelper = require("../utils/localizationHelper");
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 scopeData = function scopeData(state) {
  return state.get("scopes");
};
var getScopesSelector = exports.getScopesSelector = (0, _reselect.createSelector)(scopeData, function (scopes) {
  return scopes;
});
var localizedScopesSelector = exports.localizedScopesSelector = (0, _reselect.createSelector)(scopeData, _locale.currentLocaleOrDefault, function (scopes, locale) {
  return scopes.map(function (scope) {
    return scope.withMutations(function (s) {
      (0, _utils.setTranslationWithFallbackField)(locale, s, "id", "name");
      (0, _utils.setTranslation)(locale, s, "description");
      (0, _utils.setTranslation)(locale, s, "currency", "displayName");
    });
  });
});
var currentScopeSelector = exports.currentScopeSelector = (0, _reselect.createSelector)(_navigation.getCurrentScope, localizedScopesSelector, function (id, scopes) {
  return scopes.get(id) || _immutable.default.Map();
});
var scopeDefaultCultureSelector = exports.scopeDefaultCultureSelector = (0, _utils.memoize)(function (scopeId) {
  return (0, _reselect.createSelector)(scopeData, function (scopes) {
    return scopes.getIn([scopeId, "defaultCulture"]);
  });
});
var isCurrentScopeAuthorizedSelector = exports.isCurrentScopeAuthorizedSelector = (0, _reselect.createSelector)(_navigation.getCurrentScope, localizedScopesSelector, function (id, scopes) {
  return scopes.getIn([id, "isAuthorizedScope"], !scopes.count());
});
var scopeFilter = function scopeFilter(state) {
  return state.getIn(["view", "scopeSelector", "filter"]);
};
var filteredScopesSelector = (0, _reselect.createSelector)(scopeFilter, localizedScopesSelector, function (filter, scopes) {
  if (!filter) return scopes;
  var directHitScopes = scopes.filter(function (s) {
    return (0, _utils.normalizeForSearch)(s.get("name")).includes((0, _utils.normalizeForSearch)(filter));
  });
  var foundScopes = _immutable.default.Map();
  directHitScopes.forEach(function (scope) {
    for (var parent = scope; parent; parent = scopes.get(parent.get("parentScopeId"))) {
      foundScopes = foundScopes.set(parent.get("id"), parent);
    }
  });
  if (foundScopes.size === 0) {
    foundScopes = foundScopes.set("Global", scopes.get("Global"));
  }
  return foundScopes;
});
var scopeGetter = exports.scopeGetter = (0, _reselect.createSelector)(filteredScopesSelector, function (scopes) {
  return function (id) {
    var scope = scopes.get(id);
    if (!scope) return null;
    return scope.toJS();
  };
});
var localizedScopesSelectorByIds = exports.localizedScopesSelectorByIds = (0, _utils.memoize)(function (scopeIds) {
  return (0, _reselect.createSelector)(scopeData, _locale.currentLocaleOrDefault, function (scopes, locale) {
    var scopesMap = _immutable.default.Map();
    scopeIds.forEach(function (scopeId) {
      var scope = scopes.get(scopeId);
      if (scope != null) {
        scope = (0, _utils.setTranslationWithFallbackField)(locale, scope, "id", "name");
        scope = (0, _utils.setTranslationWithFallbackValue)(locale, scope, scope.getIn(["currency", "isoCode"]), "currency", "displayName");
        scopesMap = scopesMap.set(scopeId, scope);
      }
    });
    return scopesMap;
  });
});
var localizedScopeSelector = exports.localizedScopeSelector = (0, _utils.memoize)(function (key) {
  return (0, _reselect.createSelector)(scopeData, _locale.currentLocaleOrDefault, function (scopes, locale) {
    var name = scopes.getIn([key, "name"]);
    if (name == null) return null;
    return (0, _localizationHelper.getLocalization)(name.toJS(), locale, key);
  });
});
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(scopeData, "scopeData", "/home/vsts/work/1/s/src/selectors/scope.js");
  reactHotLoader.register(getScopesSelector, "getScopesSelector", "/home/vsts/work/1/s/src/selectors/scope.js");
  reactHotLoader.register(localizedScopesSelector, "localizedScopesSelector", "/home/vsts/work/1/s/src/selectors/scope.js");
  reactHotLoader.register(currentScopeSelector, "currentScopeSelector", "/home/vsts/work/1/s/src/selectors/scope.js");
  reactHotLoader.register(scopeDefaultCultureSelector, "scopeDefaultCultureSelector", "/home/vsts/work/1/s/src/selectors/scope.js");
  reactHotLoader.register(isCurrentScopeAuthorizedSelector, "isCurrentScopeAuthorizedSelector", "/home/vsts/work/1/s/src/selectors/scope.js");
  reactHotLoader.register(scopeFilter, "scopeFilter", "/home/vsts/work/1/s/src/selectors/scope.js");
  reactHotLoader.register(filteredScopesSelector, "filteredScopesSelector", "/home/vsts/work/1/s/src/selectors/scope.js");
  reactHotLoader.register(scopeGetter, "scopeGetter", "/home/vsts/work/1/s/src/selectors/scope.js");
  reactHotLoader.register(localizedScopesSelectorByIds, "localizedScopesSelectorByIds", "/home/vsts/work/1/s/src/selectors/scope.js");
  reactHotLoader.register(localizedScopeSelector, "localizedScopeSelector", "/home/vsts/work/1/s/src/selectors/scope.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(scopeData, "scopeData", "/home/vsts/work/1/s/src/selectors/scope.js");
  reactHotLoader.register(getScopesSelector, "getScopesSelector", "/home/vsts/work/1/s/src/selectors/scope.js");
  reactHotLoader.register(localizedScopesSelector, "localizedScopesSelector", "/home/vsts/work/1/s/src/selectors/scope.js");
  reactHotLoader.register(currentScopeSelector, "currentScopeSelector", "/home/vsts/work/1/s/src/selectors/scope.js");
  reactHotLoader.register(scopeDefaultCultureSelector, "scopeDefaultCultureSelector", "/home/vsts/work/1/s/src/selectors/scope.js");
  reactHotLoader.register(isCurrentScopeAuthorizedSelector, "isCurrentScopeAuthorizedSelector", "/home/vsts/work/1/s/src/selectors/scope.js");
  reactHotLoader.register(scopeFilter, "scopeFilter", "/home/vsts/work/1/s/src/selectors/scope.js");
  reactHotLoader.register(filteredScopesSelector, "filteredScopesSelector", "/home/vsts/work/1/s/src/selectors/scope.js");
  reactHotLoader.register(scopeGetter, "scopeGetter", "/home/vsts/work/1/s/src/selectors/scope.js");
  reactHotLoader.register(localizedScopesSelectorByIds, "localizedScopesSelectorByIds", "/home/vsts/work/1/s/src/selectors/scope.js");
  reactHotLoader.register(localizedScopeSelector, "localizedScopeSelector", "/home/vsts/work/1/s/src/selectors/scope.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();