'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _defineProperty2 = require('babel-runtime/helpers/defineProperty');

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

var _immutable = require('immutable');

var _keyBy = require('lodash/keyBy');

var _keyBy2 = _interopRequireDefault(_keyBy);

var _isArrayLike = require('lodash/isArrayLike');

var _isArrayLike2 = _interopRequireDefault(_isArrayLike);

var _constants = require('../../constants');

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

var addQuizEntries = function addQuizEntries(state, quizEntries) {
  var quizEntriesHash = (0, _keyBy2.default)(quizEntries, 'id');
  return (0, _immutable.fromJS)(quizEntriesHash);
};

var addQuizEntry = function addQuizEntry(state, quizEntry) {
  // This is sad, but because of how we handle our data in the
  // getPersistenceActions/CallHandlers all of our data will be
  // arrays coming in to the store from fetcher, even if it
  // _should_ be a single object.
  var qe = (0, _isArrayLike2.default)(quizEntry) ? (0, _keyBy2.default)(quizEntry, 'id') : (0, _defineProperty3.default)({}, quizEntry.id, quizEntry);
  return state.mergeDeep((0, _immutable.fromJS)(qe));
};

exports.default = function () {
  var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _immutable.Map)();
  var action = arguments[1];

  switch (action.type) {
    case _constants.ADD_QUIZ_ENTRIES:
      return addQuizEntries(state, action.payload);
    case _constants.ADD_QUIZ_ENTRY:
      return addQuizEntry(state, action.payload);
    default:
      return state;
  }
};