"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getTemporaryQuizEntry = exports.getQuizEntry = exports.getQuizEntries = exports.getTotalPointsPossible = exports.getQuizById = exports.getActiveQuizCaluclatorType = exports.getActiveQuizId = exports.getActiveQuiz = void 0;

var _immutable = require("immutable");

var _quizSessions = require("./quizSessions");

var _Quiz = _interopRequireDefault(require("../records/Quiz"));

var _QuizEntry = _interopRequireDefault(require("../records/QuizEntry"));

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

var path = 'quizzes';

var getActiveQuiz = function getActiveQuiz(state) {
  return getQuizById(state, getActiveQuizId(state));
};

exports.getActiveQuiz = getActiveQuiz;

var getActiveQuizId = function getActiveQuizId(state) {
  // For some reason this could be defined in one or the other of these, or neither.
  return state.getIn([path, 'activeQuizId']) || (0, _quizSessions.getActiveQuizSession)(state) && (0, _quizSessions.getActiveQuizSession)(state).get('quiz');
};

exports.getActiveQuizId = getActiveQuizId;

var getActiveQuizCaluclatorType = function getActiveQuizCaluclatorType(state) {
  return state.getIn([path, getActiveQuizId(state), 'calculatorType']);
};

exports.getActiveQuizCaluclatorType = getActiveQuizCaluclatorType;

var getQuizById = function getQuizById(state, id) {
  return _Quiz.default.create(state.getIn([path, id]));
};

exports.getQuizById = getQuizById;

var getTotalPointsPossible = function getTotalPointsPossible(state, id) {
  var quizEntries = getQuizEntries(state, id);
  return quizEntries.reduce(function (total, entry) {
    return total + entry.pointsPossible;
  }, 0);
};

exports.getTotalPointsPossible = getTotalPointsPossible;

var getQuizEntries = function getQuizEntries(state, id) {
  var quiz = getQuizById(state, id);
  return quiz.get('quizEntries', (0, _immutable.List)()).map(function (quizEntryId) {
    return getQuizEntry(state, quizEntryId);
  });
};

exports.getQuizEntries = getQuizEntries;

var getQuizEntry = function getQuizEntry(state, id) {
  if (id === _constants.TEMPORARY_QUIZ_ENTRY_ID) {
    return getTemporaryQuizEntry(state);
  }

  return _QuizEntry.default.create(state.getIn(['quizEntries', id]));
};

exports.getQuizEntry = getQuizEntry;

var getTemporaryQuizEntry = function getTemporaryQuizEntry(state) {
  var temporaryQuizEntryData = state.getIn(['modifications', 'quizEntries', _constants.TEMPORARY_QUIZ_ENTRY_ID], (0, _immutable.Map)({
    entry: _constants.TEMPORARY_ITEM_ID
  }));
  return _QuizEntry.default.create(temporaryQuizEntryData);
};

exports.getTemporaryQuizEntry = getTemporaryQuizEntry;