"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _immutable = require("immutable");

var _constants = require("@instructure/quiz-common/lib/constants");

var defaultResponse = function defaultResponse(payload) {
  return (0, _immutable.Map)({
    position: payload.position,
    itemId: payload.itemId,
    userResponse: payload.userResponse || {},
    clientTimestamp: Date.now()
  });
};

var cachedSessionIsCurrent = function cachedSessionIsCurrent(state, payload) {
  return state.get('sessionIdForCurrentItem') === payload;
};

var activeQuizSessionChange = function activeQuizSessionChange(state, payload) {
  if (cachedSessionIsCurrent(state, payload)) {
    return state;
  } else {
    return state.clear();
  }
};

var updateQuizSessionStatusOnPageLoad = function updateQuizSessionStatusOnPageLoad(state, payload) {
  return state.set('quizSessionStatusOnPageLoad', payload.quizSessionStatusOnPageLoad);
};

var setCurrentSessionItemPosition = function setCurrentSessionItemPosition(state, payload) {
  return state.set('currentSessionItemPosition', payload.position);
};

var previousQuestion = function previousQuestion(state, payload) {
  var currentPosition = state.get('currentSessionItemPosition');
  return state.set('currentSessionItemPosition', currentPosition - 1);
};

var backtrackingNextQuestion = function backtrackingNextQuestion(state, payload) {
  var currentPosition = state.get('currentSessionItemPosition', 1);
  return state.set('currentSessionItemPosition', currentPosition + 1);
};

var updateIsTaking = function updateIsTaking(state, payload) {
  return state.set('isTaking', payload.isTaking);
};

var updateTimerInfo = function updateTimerInfo(state, payload) {
  return state.mergeIn(['timerData'], payload);
};

var updateTimerMinimized = function updateTimerMinimized(state, payload) {
  return state.setIn(['timerData', 'timerMinimized'], payload.timerMinimized);
};

var getResponse = function getResponse(payload, state) {
  return state.getIn(['responses', payload.position]) || defaultResponse(payload);
};

var mergeModifiedResponse = function mergeModifiedResponse(state, payload) {
  var responseMap = (0, _immutable.Map)([[payload.get('position'), payload]]);
  return state.mergeIn(['responses'], responseMap);
};

var modifyResponse = function modifyResponse(state, payload) {
  var response = getResponse(payload, state);
  var responseWithMods = response.merge(payload);
  return mergeModifiedResponse(state, responseWithMods);
};

var loadResponses = function loadResponses(state, payload) {
  if (payload.length === 0) {
    return state.set('responses', (0, _immutable.Map)());
  }

  return payload.reduce(modifyResponse, state);
};

var markSessionStartedIdentifier = function markSessionStartedIdentifier(state, payload) {
  return state.get('browserSessionId') === payload.browserSessionId ? state : state.set('browserSessionId', payload.browserSessionId);
};

var updateQuizSubmitFlag = function updateQuizSubmitFlag(state, payload) {
  return state.set('submitFlag', payload.value);
};

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

  switch (action.type) {
    // two types for event logging differentiation
    case _constants.RESPONSE:
    case _constants.PREVIEW_RESPONSE:
      return modifyResponse(state, action.payload);

    case _constants.ACTIVE_QUIZ_SESSION_CHANGE:
      return activeQuizSessionChange(state, action.payload);

    case _constants.PREVIOUS_QUESTION:
      return previousQuestion(state, action.payload);

    case _constants.BACKTRACKING_NEXT_QUESTION:
      return backtrackingNextQuestion(state, action.payload);

    case _constants.LOAD_RESUME_DATA:
      return loadResponses(state, action.payload);

    case _constants.SESSION_STARTED_OR_RESUMED_EVENT:
      return markSessionStartedIdentifier(state, action.payload);

    case _constants.UPDATE_IS_TAKING:
      return updateIsTaking(state, action.payload);

    case _constants.UPDATE_TIMER_MINIMIZED:
      return updateTimerMinimized(state, action.payload);

    case _constants.UPDATE_TIMER_INFO:
      return updateTimerInfo(state, action.payload);

    case _constants.SET_CURRENT_SESSION_ITEM_POSITION:
      return setCurrentSessionItemPosition(state, action.payload);

    case _constants.UPDATE_QUIZ_SESSION_STATUS_ON_PAGE_LOAD:
      return updateQuizSessionStatusOnPageLoad(state, action.payload);

    case _constants.UPDATE_QUIZ_SUBMIT_FLAG:
      return updateQuizSubmitFlag(state, action.payload);

    default:
      return state;
  }
};

exports.default = _default;