'use strict';

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

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

var _keyBy2 = _interopRequireDefault(_keyBy);

var _immutable = require('immutable');

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

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

var addItemAnalyses = function addItemAnalyses(state, itemAnalyses) {
  return state.setIn(['item_analysis'], (0, _immutable.fromJS)((0, _keyBy2.default)(itemAnalyses, 'itemId')));
};

var addQuizAnalysis = function addQuizAnalysis(state, quizAnalyses) {
  var quizAnalysis = quizAnalyses[0];
  return state.setIn(['quiz_analysis', quizAnalysis.quizId], (0, _immutable.fromJS)(quizAnalysis));
};

var markQuizAnalysisNotFound = function markQuizAnalysisNotFound(state, quizId) {
  return state.setIn(['quiz_analysis', quizId], (0, _immutable.fromJS)({ quizId: quizId, notFound: true }));
};

var markItemAnalysisNotFound = function markItemAnalysisNotFound(state, itemId) {
  return state.setIn(['item_analysis', itemId], (0, _immutable.fromJS)({ itemId: itemId, notFound: true }));
};

var toggleItemAnalysisRow = function toggleItemAnalysisRow(state, itemId) {
  return state.updateIn(['item_analysis_row_open', itemId], false, function (isOpen) {
    return !isOpen;
  });
};

var defaultState = (0, _immutable.fromJS)({});

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

  switch (action.type) {
    case _constants.ADD_ITEM_ANALYSES:
      return addItemAnalyses(state, action.payload);
    case _constants.ADD_QUIZ_ANALYSIS:
      return addQuizAnalysis(state, action.payload);
    case _constants.QUIZ_ANALYSIS_NOT_FOUND:
      return markQuizAnalysisNotFound(state, action.payload);
    case _constants.ITEM_ANALYSIS_NOT_FOUND:
      return markItemAnalysisNotFound(state, action.payload);
    case _constants.TOGGLE_ITEM_ANALYSIS_EXPANDED:
      return toggleItemAnalysisRow(state, action.payload);
    default:
      return state;
  }
};