'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getSessionItemResults = exports.getSessionItems = exports.handleQuizSessionFetch = exports.addQuizSessions = exports.newActiveQuizSession = undefined;
exports.getQuizSessionResult = getQuizSessionResult;
exports.getResultInfo = getResultInfo;

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

var _callHandlers = require('../api/callHandlers');

var _quizSessions = require('../api/quizSessions');

var newActiveQuizSession = exports.newActiveQuizSession = function newActiveQuizSession(activeQuizSessionId) {
  return {
    type: _constants.ACTIVE_QUIZ_SESSION_CHANGE,
    payload: activeQuizSessionId
  };
};

var addQuizSessions = exports.addQuizSessions = function addQuizSessions(quizSessions) {
  var payload = Array.isArray(quizSessions) ? quizSessions : [quizSessions];
  return { type: _constants.ADD_QUIZ_SESSIONS, payload: payload };
};

var handleQuizSessionFetch = exports.handleQuizSessionFetch = function handleQuizSessionFetch(dispatch, quizSession) {
  return dispatch([(0, _callHandlers.handleQuizSessionResponse)(quizSession), newActiveQuizSession(quizSession.id)]);
};

var getSessionItems = exports.getSessionItems = function getSessionItems(quizSessionId) {
  var apiAction = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _quizSessions.fetchSessionItems;

  return function (dispatch) {
    var responseCb = function responseCb(sessionItemsData) {
      return dispatch((0, _callHandlers.handleSessionItemsResponse)(sessionItemsData, quizSessionId));
    };

    return apiAction(quizSessionId, responseCb);
  };
};

var getSessionItemResults = exports.getSessionItemResults = function getSessionItemResults(quizSessionId, quizSessionResultId) {
  var apiAction = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _quizSessions.fetchSessionItemResults;

  return function (dispatch) {
    var responseCb = function responseCb(sessionItemResultsData) {
      return dispatch((0, _callHandlers.handleSessionItemResultsResponse)(sessionItemResultsData, quizSessionId));
    };

    return apiAction(quizSessionId, quizSessionResultId, responseCb);
  };
};

function getQuizSessionResult(authoritativeResultId, quizSessionId) {
  var apiAction = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _quizSessions.fetchQuizSessionResult;

  return function (dispatch, getState) {
    var responseCb = function responseCb(result) {
      return dispatch((0, _callHandlers.handleQuizSessionResultsResponse)([result]));
    };
    return apiAction(quizSessionId, authoritativeResultId, responseCb);
  };
}

// TODO: set callback tracking in fetcher to prevent hitting this again when the quizSession is loaded and graded
// but the sessionItems aren't yet loaded, then remove the throttling functions in results and grading
function getResultInfo(quizSessionId) {
  var apiAction = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _quizSessions.fetchQuizSession;
  var autoRetry = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  var fetchItems = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;

  return function (dispatch, getState) {
    var getSIResults = function getSIResults(quizSessionData) {
      if (quizSessionData.status !== 'graded') {
        return;
      }
      return dispatch(getSessionItemResults(quizSessionId, quizSessionData.authoritative_result.id));
    };
    var getSIs = function getSIs(quizSessionData) {
      if (quizSessionData.status !== 'graded') {
        return;
      }
      return dispatch(getSessionItems(quizSessionId));
    };

    var responseCb = function responseCb(quizSessionData) {
      if (autoRetry && quizSessionData.status === 'autograding') {
        setTimeout(function () {
          getResultInfo(quizSessionId, apiAction, autoRetry, fetchItems)(dispatch, getState);
        }, 2000);
      }
      var promises = [handleQuizSessionFetch(dispatch, quizSessionData)];
      if (fetchItems) {
        promises.push(getSIs(quizSessionData));
        promises.push(getSIResults(quizSessionData));
      }
      return Promise.all(promises);
    };

    return apiAction(quizSessionId, responseCb);
  };
}