"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
exports.getMatches = getMatches;
var _immutable = _interopRequireDefault(require("immutable"));
var _actionCodes = require("../constants/actionCodes");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _default = function _default() {
  let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _immutable.default.Map();
  let action = arguments.length > 1 ? arguments[1] : undefined;
  let count;
  let items;
  switch (action.type) {
    case _actionCodes.ADD_TERM_STARTED:
      return state.setIn([action.meta.partialTerm, action.meta.recordType, action.meta.vocabulary], _immutable.default.Map({
        isAddPending: true
      }));
    case _actionCodes.ADD_TERM_FULFILLED:
      return state.setIn([action.meta.partialTerm, action.meta.recordType, action.meta.vocabulary], _immutable.default.Map({
        newTerm: _immutable.default.fromJS(action.payload.data)
      }));
    case _actionCodes.ADD_TERM_REJECTED:
      return state.setIn([action.meta.partialTerm, action.meta.recordType, action.meta.vocabulary], _immutable.default.Map({
        error: action.payload
      }));
    case _actionCodes.PARTIAL_TERM_SEARCH_STARTED:
      return state.setIn([action.meta.partialTerm, action.meta.recordType, action.meta.vocabulary], _immutable.default.Map({
        isSearchPending: true
      }));
    case _actionCodes.PARTIAL_TERM_SEARCH_FULFILLED:
      count = parseInt(action.payload.data['ns2:abstract-common-list'].itemsInPage, 10);
      if (Number.isNaN(count) || count === 0) {
        items = [];
      } else {
        items = action.payload.data['ns2:abstract-common-list']['list-item'];
        if (!Array.isArray(items)) {
          items = [items];
        }
      }
      return state.setIn([action.meta.partialTerm, action.meta.recordType, action.meta.vocabulary], _immutable.default.Map({
        items
      }));
    case _actionCodes.PARTIAL_TERM_SEARCH_REJECTED:
      return state.setIn([action.meta.partialTerm, action.meta.recordType, action.meta.vocabulary], _immutable.default.Map({
        error: action.payload
      }));
    case _actionCodes.CLEAR_PARTIAL_TERM_SEARCH_RESULTS:
      return state.clear();
    default:
      return state;
  }
};
exports.default = _default;
function getMatches(state) {
  return state;
}