"use strict";

exports.__esModule = true;
exports.default = exports.ListInfoPropertyName = void 0;
var _immutable = _interopRequireDefault(require("immutable"));
var _lodash = require("lodash");
var _ListHelper;
var _excluded = ["resetList", "scope", "filters", "sorting"],
  _excluded2 = ["sorting", "filters", "scope", "page", "nextPageToLoad", "index", "list", "totalCount"];
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);
})();
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(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 ListInfoPropertyName = exports.ListInfoPropertyName = "listInfo";
var standardInfoKeys = ["scope", "page", "filters", "sorting", "totalCount", "nextPageToLoad", "index", "list"];
var ListReducerHelper = /*#__PURE__*/function () {
  function ListReducerHelper(groupPropertyName) {
    var _this = this;
    _defineProperty(this, "setNextPageToLoad", function (state, nextPageToLoad) {
      return state.setIn([_this.groupPropertyName, "nextPageToLoad"], nextPageToLoad);
    });
    _defineProperty(this, "setResults", function (state, listInfo, forceReset) {
      if (forceReset === void 0) {
        forceReset = false;
      }
      return state.withMutations(function (s) {
        var page = s.getIn([_this.groupPropertyName, "nextPageToLoad"]);
        var entities = _immutable.default.fromJS(listInfo.indexEntities || {});
        if (page === 1 || forceReset) {
          s.setIn([_this.groupPropertyName, "index"], entities);
          s.setIn([_this.groupPropertyName, "list"], _immutable.default.fromJS(listInfo.listEntities));
        } else {
          s.mergeIn([_this.groupPropertyName, "index"], entities);
          s.setIn([_this.groupPropertyName, "list"], s.getIn([_this.groupPropertyName, "list"]).concat(_immutable.default.fromJS(listInfo.listEntities)));
        }
        s.setIn([_this.groupPropertyName, "totalCount"], listInfo.totalCount);
      });
    });
    _defineProperty(this, "setCurrentInfo", function (state, _ref) {
      if (_ref === void 0) {
        _ref = {};
      }
      var _ref2 = _ref,
        resetList = _ref2.resetList,
        scope = _ref2.scope,
        filters = _ref2.filters,
        sorting = _ref2.sorting,
        others = _objectWithoutProperties(_ref2, _excluded);
      return state.withMutations(function (s) {
        if (resetList) {
          s.setIn([_this.groupPropertyName, "nextPageToLoad"], 1);
          s.setIn([_this.groupPropertyName, "page"], null);
          s.setIn([_this.groupPropertyName, "index"], _immutable.default.fromJS({}));
          s.setIn([_this.groupPropertyName, "list"], _immutable.default.fromJS([]));
          s.setIn([_this.groupPropertyName, "totalCount"], 0);
        } else {
          var nextPageToLoad = state.getIn([_this.groupPropertyName, "nextPageToLoad"]);
          s.setIn([_this.groupPropertyName, "nextPageToLoad"], nextPageToLoad);
          s.setIn([_this.groupPropertyName, "page"], nextPageToLoad);
        }
        s.setIn([_this.groupPropertyName, "scope"], scope != null ? scope : null);
        s.setIn([_this.groupPropertyName, "filters"], _immutable.default.fromJS(filters != null ? filters : null));
        s.setIn([_this.groupPropertyName, "sorting"], _immutable.default.fromJS(sorting != null ? sorting : null));
        var otherKeys = Object.keys(others);
        s.get(_this.groupPropertyName).keySeq().forEach(function (key) {
          if (!standardInfoKeys.includes(key) && !otherKeys.includes(key)) {
            s.removeIn([_this.groupPropertyName, key]);
          }
        });
        otherKeys.forEach(function (key) {
          s.setIn([_this.groupPropertyName, key], (0, _lodash.isObject)(others[key]) ? _immutable.default.fromJS(others[key]) : others[key]);
        });
      });
    });
    _defineProperty(this, "addIndexWithMutations", function (mutator, id, value) {
      mutator.setIn([_this.groupPropertyName, "index", id], _immutable.default.fromJS(value));
    });
    _defineProperty(this, "appendIdToListWithMutations", function (mutator, id) {
      mutator.setIn([_this.groupPropertyName, "list"], mutator.getIn([_this.groupPropertyName, "list"]).push(id));
    });
    _defineProperty(this, "updateIndexWithMutations", function (mutator, id, value) {
      var key = [_this.groupPropertyName, "index", id];
      if (mutator.getIn(key)) {
        mutator.setIn(key, _immutable.default.fromJS(value));
      }
    });
    _defineProperty(this, "removeFromIndexWithMutations", function (mutator, id) {
      mutator.removeIn([_this.groupPropertyName, "index", id]);
    });
    _defineProperty(this, "resetListInfo", function (state, propertiesToKeep) {
      if (propertiesToKeep === void 0) {
        propertiesToKeep = {};
      }
      var updatedState = state.setIn([_this.groupPropertyName, "nextPageToLoad"], 1).setIn([_this.groupPropertyName, "index"], _immutable.default.fromJS({})).setIn([_this.groupPropertyName, "list"], _immutable.default.fromJS([])).setIn([_this.groupPropertyName, "totalCount"], 0);
      if (!propertiesToKeep.filters) {
        updatedState = updatedState.setIn([_this.groupPropertyName, "filters"], null);
      }
      if (!propertiesToKeep.sorting) {
        updatedState = updatedState.setIn([_this.groupPropertyName, "sorting"], null);
      }
      if (!propertiesToKeep.scope) {
        updatedState = updatedState.setIn([_this.groupPropertyName, "scope"], null);
      }
      if (!propertiesToKeep.page) {
        updatedState = updatedState.setIn([_this.groupPropertyName, "page"], null);
      }
      return updatedState;
    });
    this.groupPropertyName = groupPropertyName;
  }
  var _proto = ListReducerHelper.prototype;
  // @ts-ignore
  _proto.__reactstandin__regenerateByEval = function __reactstandin__regenerateByEval(key, code) {
    // @ts-ignore
    this[key] = eval(code);
  };
  return ListReducerHelper;
}();
var ListSelectorHelper = /*#__PURE__*/function () {
  function ListSelectorHelper(groupPropertyName) {
    var _this2 = this;
    _defineProperty(this, "getCurrentInfo", function (state) {
      var _listInfo$get, _listInfo$get2;
      var listInfo = state.get(_this2.groupPropertyName) || _immutable.default.Map();
      var info = {
        currentScope: listInfo.get("scope"),
        currentPage: listInfo.get("page"),
        currentFilters: (_listInfo$get = listInfo.get("filters")) == null ? void 0 : _listInfo$get.toJS(),
        currentSorting: (_listInfo$get2 = listInfo.get("sorting")) == null ? void 0 : _listInfo$get2.toJS(),
        totalCount: listInfo.get("totalCount")
      };
      listInfo.mapKeys(function (key, value) {
        if (standardInfoKeys.includes(key)) {
          // standard key, nothing to do
        } else {
          info[key] = value != null && value.toJS ? value.toJS() : value;
        }
      });
      return info;
    });
    _defineProperty(this, "getIndex", function (state) {
      return state.getIn([_this2.groupPropertyName, "index"]);
    });
    _defineProperty(this, "getList", function (state) {
      return state.getIn([_this2.groupPropertyName, "list"]);
    });
    _defineProperty(this, "getNextPageToLoad", function (state) {
      return state.getIn([_this2.groupPropertyName, "nextPageToLoad"]);
    });
    this.groupPropertyName = groupPropertyName;
  }
  var _proto2 = ListSelectorHelper.prototype;
  // @ts-ignore
  _proto2.__reactstandin__regenerateByEval = function __reactstandin__regenerateByEval(key, code) {
    // @ts-ignore
    this[key] = eval(code);
  };
  return ListSelectorHelper;
}();
var ListHelper = /*#__PURE__*/function () {
  function ListHelper(groupPropertyName) {
    var _this3 = this;
    if (groupPropertyName === void 0) {
      groupPropertyName = ListInfoPropertyName;
    }
    _defineProperty(this, "createInitialListInfo", function (additionalValues) {
      if (additionalValues === void 0) {
        additionalValues = null;
      }
      return _this3.createListInfoFrom(_objectSpread({}, additionalValues));
    });
    _defineProperty(this, "createListInfoFrom", function (_ref3) {
      if (_ref3 === void 0) {
        _ref3 = {};
      }
      var _ref4 = _ref3,
        sorting = _ref4.sorting,
        filters = _ref4.filters,
        scope = _ref4.scope,
        page = _ref4.page,
        nextPageToLoad = _ref4.nextPageToLoad,
        index = _ref4.index,
        list = _ref4.list,
        totalCount = _ref4.totalCount,
        additionalValues = _objectWithoutProperties(_ref4, _excluded2);
      return _defineProperty({}, _this3.groupPropertyName, _objectSpread({
        sorting: sorting != null ? sorting : null,
        filters: filters != null ? filters : null,
        scope: scope != null ? scope : null,
        page: page != null ? page : null,
        nextPageToLoad: nextPageToLoad != null ? nextPageToLoad : 1,
        index: index != null ? index : {},
        list: list != null ? list : [],
        totalCount: totalCount != null ? totalCount : 0
      }, additionalValues));
    });
    this.groupPropertyName = groupPropertyName;
    this.reducer = new ListReducerHelper(this.groupPropertyName);
    this.selector = new ListSelectorHelper(this.groupPropertyName);
  }
  var _proto3 = ListHelper.prototype;
  // @ts-ignore
  _proto3.__reactstandin__regenerateByEval = function __reactstandin__regenerateByEval(key, code) {
    // @ts-ignore
    this[key] = eval(code);
  };
  return ListHelper;
}();
_ListHelper = ListHelper;
_defineProperty(ListHelper, "defaultInstance", new _ListHelper());
_defineProperty(ListHelper, "reducer", _ListHelper.defaultInstance.reducer);
_defineProperty(ListHelper, "selector", _ListHelper.defaultInstance.selector);
_defineProperty(ListHelper, "createInitialListInfo", _ListHelper.defaultInstance.createInitialListInfo);
_defineProperty(ListHelper, "createListInfoFrom", _ListHelper.defaultInstance.createListInfoFrom);
var _default = ListHelper;
var _default2 = _default;
var _default3 = exports.default = _default2;
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(ListInfoPropertyName, "ListInfoPropertyName", "/home/vsts/work/1/s/src/utils/ListHelper.js");
  reactHotLoader.register(standardInfoKeys, "standardInfoKeys", "/home/vsts/work/1/s/src/utils/ListHelper.js");
  reactHotLoader.register(ListReducerHelper, "ListReducerHelper", "/home/vsts/work/1/s/src/utils/ListHelper.js");
  reactHotLoader.register(ListSelectorHelper, "ListSelectorHelper", "/home/vsts/work/1/s/src/utils/ListHelper.js");
  reactHotLoader.register(ListHelper, "ListHelper", "/home/vsts/work/1/s/src/utils/ListHelper.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/utils/ListHelper.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(ListInfoPropertyName, "ListInfoPropertyName", "/home/vsts/work/1/s/src/utils/ListHelper.js");
  reactHotLoader.register(standardInfoKeys, "standardInfoKeys", "/home/vsts/work/1/s/src/utils/ListHelper.js");
  reactHotLoader.register(ListReducerHelper, "ListReducerHelper", "/home/vsts/work/1/s/src/utils/ListHelper.js");
  reactHotLoader.register(ListSelectorHelper, "ListSelectorHelper", "/home/vsts/work/1/s/src/utils/ListHelper.js");
  reactHotLoader.register(ListHelper, "ListHelper", "/home/vsts/work/1/s/src/utils/ListHelper.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/utils/ListHelper.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();