"use strict";

exports.__esModule = true;
exports.default = exports.PRODUCT_MODULE_NAME = exports.CUSTOMER_MODULE_NAME = exports.ORDER_MODULE_NAME = void 0;

var _immutable = _interopRequireDefault(require("immutable"));

var _normalizr = require("normalizr");

var _definitions = require("../schemas/definitions");

var _metadata = require("../schemas/metadata");

var _productDefinitions = require("../schemas/productDefinitions");

var _metadata2 = require("../actions/metadata");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

(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 _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _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(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

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 ORDER_MODULE_NAME = "order";
exports.ORDER_MODULE_NAME = ORDER_MODULE_NAME;
var CUSTOMER_MODULE_NAME = "customer";
exports.CUSTOMER_MODULE_NAME = CUSTOMER_MODULE_NAME;
var PRODUCT_MODULE_NAME = "product";
exports.PRODUCT_MODULE_NAME = PRODUCT_MODULE_NAME;

var arrayToIndex = function arrayToIndex(items, keyField) {
  return items.reduce(function (index, item) {
    var key = item[keyField];
    index[key] = item;
    return index;
  }, {});
};

var initialState = _immutable.default.fromJS({
  lookups: {
    order: {
      index: {},
      list: []
    },
    customer: {
      index: {},
      list: []
    },
    product: {
      index: {},
      list: []
    }
  },
  definitions: {
    customer: {},
    order: {},
    product: {}
  }
});

var lookupReducerHelper = {
  incrementPage: function incrementPage(moduleName, state, action) {
    return state.setIn(["lookups", moduleName, "nextPageToLoad"], action.pageToLoad);
  },
  getPagedLookups: function getPagedLookups(moduleName, state) {
    var _state$getIn;

    var page = (_state$getIn = state.getIn(["lookups", moduleName, "nextPageToLoad"])) != null ? _state$getIn : 1;
    var index = state.getIn(["lookups", moduleName, "index"]);

    var _index$keys = index.keys(),
        _index$keys2 = _toArray(_index$keys),
        keys = _index$keys2.slice(0);

    var pagedKeys = keys.sort(function (a, b) {
      return a == null ? void 0 : a.toLowerCase().localeCompare(b == null ? void 0 : b.toLowerCase());
    }).slice(0, page * _metadata2.lookupsPageLength);
    var pagedList = pagedKeys.filter(function (id) {
      return index.get(id);
    });
    return state.setIn(["lookups", moduleName, "list"], _immutable.default.fromJS(pagedList)).setIn(["lookups", moduleName, "totalCount"], index.size);
  },
  setPagedCurrentInfo: function setPagedCurrentInfo(moduleName, state, action) {
    var nextPageToLoad = action.payload.resetList ? 1 : action.payload.nextPageToLoad;
    var currentPage = action.payload.resetList ? null : nextPageToLoad;
    return state.mergeIn(["lookups", moduleName], _immutable.default.fromJS({
      currentPage: currentPage,
      nextPageToLoad: nextPageToLoad
    }));
  },
  getLookupsQuerySuccess: function getLookupsQuerySuccess(moduleName, state, action) {
    return state.withMutations(function (s) {
      action.payload.forEach(function (lookup) {
        lookup.values = arrayToIndex(lookup.values, "value");
        s.setIn(["lookups", moduleName, "index", lookup.lookupName], _immutable.default.fromJS(lookup));
      });
    });
  }
};

var metadataReducer = function metadataReducer(state, action) {
  if (state === void 0) {
    state = initialState;
  }

  switch (action.type) {
    case _metadata2.INCREMENT_ORDER_LOOKUPS_PAGE:
      {
        return lookupReducerHelper.incrementPage(ORDER_MODULE_NAME, state, action);
      }

    case _metadata2.REFRESH_PAGED_ORDER_LOOKUPS:
      {
        return lookupReducerHelper.getPagedLookups(ORDER_MODULE_NAME, state);
      }

    case _metadata2.SET_PAGED_ORDER_LOOKUPS_CURRENT_INFO:
      {
        return lookupReducerHelper.setPagedCurrentInfo(ORDER_MODULE_NAME, state, action);
      }

    case _metadata2.GET_ORDER_LOOKUPS_SUCCESS:
      {
        return lookupReducerHelper.getLookupsQuerySuccess(ORDER_MODULE_NAME, state, action);
      }

    case _metadata2.INCREMENT_CUSTOMER_LOOKUPS_PAGE:
      {
        return lookupReducerHelper.incrementPage(CUSTOMER_MODULE_NAME, state, action);
      }

    case _metadata2.REFRESH_PAGED_CUSTOMER_LOOKUPS:
      {
        return lookupReducerHelper.getPagedLookups(CUSTOMER_MODULE_NAME, state);
      }

    case _metadata2.SET_PAGED_CUSTOMER_LOOKUPS_CURRENT_INFO:
      {
        return lookupReducerHelper.setPagedCurrentInfo(CUSTOMER_MODULE_NAME, state, action);
      }

    case _metadata2.GET_CUSTOMER_LOOKUPS_SUCCESS:
      {
        return lookupReducerHelper.getLookupsQuerySuccess(CUSTOMER_MODULE_NAME, state, action);
      }

    case _metadata2.GET_PRODUCT_LOOKUPS_SUCCESS:
      {
        return lookupReducerHelper.getLookupsQuerySuccess(PRODUCT_MODULE_NAME, state, action);
      }

    case _metadata2.GET_CUSTOMER_DEFINITIONS_SUCCESS:
      {
        var normalizedDefinitons = (0, _normalizr.normalize)(action.payload, _definitions.definitionsListSchema);
        return state.setIn(["definitions", CUSTOMER_MODULE_NAME], _immutable.default.fromJS(normalizedDefinitons.entities.definitions));
      }

    case _metadata2.GET_ORDER_DEFINITIONS_SUCCESS:
      {
        var _normalizedDefinitons = (0, _normalizr.normalize)(action.payload, _definitions.definitionsListSchema);

        return state.setIn(["definitions", ORDER_MODULE_NAME], _immutable.default.fromJS(_normalizedDefinitons.entities.definitions));
      }

    case _metadata2.GET_PRODUCT_DEFINITIONS_SUCCESS:
      {
        var _normalizedDefinitons2 = (0, _normalizr.normalize)(action.payload, _productDefinitions.productDefinitionsListSchema);

        return state.setIn(["definitions", PRODUCT_MODULE_NAME], _immutable.default.fromJS(_normalizedDefinitons2.entities.definitions));
      }

    case _metadata2.GET_PROFILE_ATTRIBUTE_GROUPS_SUCCESS:
      {
        var _action$payload;

        var normalizedData = (0, _normalizr.normalize)((_action$payload = action.payload) == null ? void 0 : _action$payload.profileAttributeGroups, _metadata.profileAttributeGroupsListSchema);
        return state.set("profileAttributeGroups", _immutable.default.fromJS(normalizedData.entities.metadata));
      }

    case _metadata2.SAVE_ORDER_LOOKUP_SUCCESS:
      return state.setIn(["lookups", ORDER_MODULE_NAME, "index", action.payload.lookupName], _immutable.default.fromJS(action.payload));

    case _metadata2.ADD_ORDER_LOOKUP_SUCCESS:
      {
        state = state.setIn(["lookups", ORDER_MODULE_NAME, "index", action.payload.lookupName], _immutable.default.fromJS(action.payload));
        return lookupReducerHelper.getPagedLookups(ORDER_MODULE_NAME, state);
      }

    case _metadata2.SAVE_CUSTOMER_LOOKUP_SUCCESS:
      return state.setIn(["lookups", CUSTOMER_MODULE_NAME, "index", action.payload.lookupName], _immutable.default.fromJS(action.payload));

    case _metadata2.ADD_CUSTOMER_LOOKUP_SUCCESS:
      {
        state = state.setIn(["lookups", CUSTOMER_MODULE_NAME, "index", action.payload.lookupName], _immutable.default.fromJS(action.payload));
        return lookupReducerHelper.getPagedLookups(CUSTOMER_MODULE_NAME, state);
      }

    case _metadata2.CREATE_PROFILE_DEFINITION_SUCCESS:
      return state.setIn(["definitions", CUSTOMER_MODULE_NAME, action.payload.entityTypeName], _immutable.default.fromJS(action.payload));

    case _metadata2.UPDATE_PROFILE_DEFINITION_SUCCESS:
      return state.setIn(["definitions", CUSTOMER_MODULE_NAME, action.meta.definition.entityTypeName], _immutable.default.fromJS(action.meta.definition));

    default:
      return state;
  }
};

var _default = metadataReducer;
var _default2 = _default;
var _default3 = _default2;
exports.default = _default3;
;

(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;

  if (!reactHotLoader) {
    return;
  }

  reactHotLoader.register(ORDER_MODULE_NAME, "ORDER_MODULE_NAME", "/home/vsts/work/1/s/src/reducers/metadata.js");
  reactHotLoader.register(CUSTOMER_MODULE_NAME, "CUSTOMER_MODULE_NAME", "/home/vsts/work/1/s/src/reducers/metadata.js");
  reactHotLoader.register(PRODUCT_MODULE_NAME, "PRODUCT_MODULE_NAME", "/home/vsts/work/1/s/src/reducers/metadata.js");
  reactHotLoader.register(arrayToIndex, "arrayToIndex", "/home/vsts/work/1/s/src/reducers/metadata.js");
  reactHotLoader.register(initialState, "initialState", "/home/vsts/work/1/s/src/reducers/metadata.js");
  reactHotLoader.register(lookupReducerHelper, "lookupReducerHelper", "/home/vsts/work/1/s/src/reducers/metadata.js");
  reactHotLoader.register(metadataReducer, "metadataReducer", "/home/vsts/work/1/s/src/reducers/metadata.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/reducers/metadata.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(ORDER_MODULE_NAME, "ORDER_MODULE_NAME", "/home/vsts/work/1/s/src/reducers/metadata.js");
  reactHotLoader.register(CUSTOMER_MODULE_NAME, "CUSTOMER_MODULE_NAME", "/home/vsts/work/1/s/src/reducers/metadata.js");
  reactHotLoader.register(PRODUCT_MODULE_NAME, "PRODUCT_MODULE_NAME", "/home/vsts/work/1/s/src/reducers/metadata.js");
  reactHotLoader.register(arrayToIndex, "arrayToIndex", "/home/vsts/work/1/s/src/reducers/metadata.js");
  reactHotLoader.register(initialState, "initialState", "/home/vsts/work/1/s/src/reducers/metadata.js");
  reactHotLoader.register(lookupReducerHelper, "lookupReducerHelper", "/home/vsts/work/1/s/src/reducers/metadata.js");
  reactHotLoader.register(metadataReducer, "metadataReducer", "/home/vsts/work/1/s/src/reducers/metadata.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/reducers/metadata.js");
})();

;

(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();