'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.quizFetchHandler = undefined;
exports.getExistingQuiz = getExistingQuiz;
exports.getExistingQuizAndItems = getExistingQuizAndItems;

var _Fetcher = require('../util/Fetcher');

var _Fetcher2 = _interopRequireDefault(_Fetcher);

var _quizzes = require('../actions/quizzes');

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

var _interactionTypes = require('./interactionTypes');

var _quizEntries = require('../../building/api/quizEntries');

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

// ==== CALLBACKS ====

var quizFetchHandler = exports.quizFetchHandler = function quizFetchHandler(dispatch, quiz) {
  dispatch([(0, _callHandlers.handleQuizResponse)(quiz), (0, _quizzes.newActiveQuiz)('' + quiz.id)]);
};

// ====================================
// fetch the data for an existing quiz
// and add it as the active quiz
// ====================================

function getExistingQuiz(quizId) {
  var quizUrl = '/api/quizzes/' + quizId;

  return function (dispatch, getState) {
    var fetcher = new _Fetcher2.default();

    return fetcher.get(quizUrl).then(function (quizData) {
      return quizFetchHandler(dispatch, quizData);
    });
  };
}

function getExistingQuizAndItems(quizId) {
  var quizUrl = '/api/quizzes/' + quizId;

  return function (dispatch, getState) {
    var fetcher = new _Fetcher2.default();

    return fetcher.get(quizUrl).then(function (quizData) {
      return quizFetchHandler(dispatch, quizData);
    }).then(function () {
      return dispatch((0, _quizEntries.getQuizEntries)(quizId));
    }).then(function () {
      return dispatch((0, _interactionTypes.interactionTypeFetch)());
    });
  };
}