'use strict';

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

var _actionTypes = require('../constants/action-types');

var types = _interopRequireWildcard(_actionTypes);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

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

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function questionnaire() {
  var state = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
  var action = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];

  switch (action.type) {
    case types.PASS_ANSWER_REQUEST:
      return _lodash2.default.merge({}, state, {
        currQuestionId: action.nextQId,
        error: null
      });
    case types.PASS_ANSWER_SUCCESS:
      var tmpAns = state.ans ? state.ans : [];
      tmpAns.push(action.ans);
      return _lodash2.default.merge({}, state, {
        currQuestionId: action.nextQId,
        error: null,
        ans: tmpAns,
        finished: action.finished,
        showExplanation: action.showExplanation,
        lastUpdated: action.succAt
      });
    case types.PASS_ANSWER_FAILURE:
      return _lodash2.default.merge({}, state, {
        error: action.error,
        lastUpdated: action.failedAt
      });
    case types.RESET_QUESTIONNAIRE_SUCCESS:
      return _lodash2.default.merge({}, state, {
        questionnaireId: action.questionnaireId,
        currQuestionId: action.qId,
        error: null,
        finished: action.finished,
        ans: null,
        showExplanation: false,
        lastUpdated: action.succAt
      });
    case types.RESET_QUESTIONNAIRE_FAILURE:
      return _lodash2.default.merge({}, state, {
        error: action.error,
        lastUpdated: action.failedAt
      });
    case types.RESET_QUESTIONNAIRE_REQUEST:
      return _lodash2.default.merge({}, state, {
        questionnaireId: action.questionnaireId,
        error: null
      });
    case types.GO_NEXT_QUESTION_SUCCESS:
      return _lodash2.default.merge({}, state, {
        currQuestionId: action.nextQId,
        error: null,
        finished: action.finished,
        showExplanation: action.showExplanation,
        lastUpdated: action.succAt
      });
    case types.GO_NEXT_QUESTION_FAILURE:
      return _lodash2.default.merge({}, state, {
        error: action.error,
        lastUpdated: action.failedAt
      });
    case types.GO_NEXT_QUESTION_REQUEST:
      return _lodash2.default.merge({}, state, {
        currQuestionId: action.nextQId,
        error: null
      });
    default:
      return state;
  }
}
function questSetting() {
  var state = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
  var action = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];

  switch (action.type) {
    case types.FETCH_QUESTIONNAIRE_SUCCESS:
      return _lodash2.default.merge({}, state, {
        questionnaireId: action.questionnaireId,
        error: null,
        lastUpdated: action.succAt,
        setting: action.questSetting
      });
    case types.FETCH_QUESTIONNAIRE_REQUEST:
      return _lodash2.default.merge({}, state, {
        questionnaireId: action.questionnaireId,
        error: null
      });
    case types.FETCH_QUESTIONNAIRE_FAILURE:
      return _lodash2.default.merge({}, state, {
        error: action.error,
        lastUpdated: action.failedAt
      });
    default:
      return state;
  }
}