"use strict";

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

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

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

var _immutable = require("immutable");

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

var _reduxStore = _interopRequireDefault(require("../../reduxStore"));

var _interactionTypeRecordForItem = _interopRequireDefault(require("./interactionTypeRecordForItem"));

var _withCachedArguments = _interopRequireDefault(require("./withCachedArguments"));

var _withStateCache = _interopRequireDefault(require("./withStateCache"));

var _BankEntry = _interopRequireDefault(require("../records/BankEntry"));

var _Item = _interopRequireDefault(require("../records/Item"));

var _ItemAnalysis = _interopRequireDefault(require("../records/ItemAnalysis"));

var _QtiImport = _interopRequireDefault(require("../records/QtiImport"));

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

var _QuizSession = _interopRequireDefault(require("../records/QuizSession"));

var _QuizSessionEvent = _interopRequireDefault(require("../records/QuizSessionEvent"));

var _QuizSessionResult = _interopRequireDefault(require("../records/QuizSessionResult"));

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

var _QuizAnalysis = _interopRequireDefault(require("../records/QuizAnalysis"));

var _Response = _interopRequireDefault(require("../records/Response"));

var _SessionItem = _interopRequireDefault(require("../records/SessionItem"));

var _SessionItemResult = _interopRequireDefault(require("../records/SessionItemResult"));

var taking = _interopRequireWildcard(require("../selectors/taking"));

var quizSessions = _interopRequireWildcard(require("../selectors/quizSessions"));

var quizzes = _interopRequireWildcard(require("../selectors/quizzes"));

var _dec, _dec2, _dec3, _obj;

// ================================
//  helper to get data and records
//  from the app state
// ================================
var stateHelper = (_dec = (0, _withStateCache.default)(function () {
  return [['interactionTypes']];
}), _dec2 = (0, _withStateCache.default)(function () {
  return [['quizEntries'], ['modifications', 'quizEntries'], ['modifications', 'items'], ['modifications', 'stimuli'], ['editing']];
}), _dec3 = (0, _withStateCache.default)(function () {
  return [['editing']];
}), (_obj = {
  state: function state() {
    return _reduxStore.default.getAppState();
  },
  // ==========================
  //     NON-RECORD HELPERS
  // ==========================
  // Held up by problems on the take page
  getTimerData: function getTimerData() {
    var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.state();
    return taking.getTimerData(state);
  },
  // ========================
  //    FIND & INSTANTIATE
  // ========================
  // SINGLE RECORDS
  activeQuiz: function activeQuiz() {
    return this.findQuiz(quizzes.getActiveQuizId(this.state()));
  },
  isActiveQuizWorkingInstanceOneQuestionAtATime: function isActiveQuizWorkingInstanceOneQuestionAtATime() {
    return this.activeQuiz().getWorkingInstance().isOneQuestionAtATime();
  },
  activeQuizSession: function activeQuizSession() {
    return this.findQuizSession(quizSessions.getActiveQuizSessionId(this.state()));
  },
  findAndInstantiate: function findAndInstantiate(RecordClass, keys) {
    var data = this.state().getIn(keys);
    return RecordClass.create(data);
  },
  findBankEntry: function findBankEntry(id) {
    return this.findAndInstantiate(_BankEntry.default, ['bankEntries', id]);
  },
  findQuiz: function findQuiz(id) {
    return this.findAndInstantiate(_Quiz.default, ['quizzes', id]);
  },
  findQuizSession: function findQuizSession(id) {
    return this.findAndInstantiate(_QuizSession.default, ['quizSessions', id]);
  },
  findSessionItem: function findSessionItem(sessionItemKey) {
    return this.findAndInstantiate(_SessionItem.default, ['sessionItems', sessionItemKey]);
  },
  findQuizSessionResult: function findQuizSessionResult(id) {
    return this.findAndInstantiate(_QuizSessionResult.default, ['quizSessionResults', id]);
  },
  findSessionItemResult: function findSessionItemResult(id) {
    return this.findAndInstantiate(_SessionItemResult.default, ['sessionItemResults', "".concat(id)]);
  },
  findTakingResponse: function findTakingResponse(responseKey) {
    return this.findAndInstantiate(_Response.default, ['taking', 'responses', responseKey]);
  },
  findImport: function findImport(id) {
    return this.findAndInstantiate(_QtiImport.default, ['qtiImports', id]);
  },
  findQuizSessionEvent: function findQuizSessionEvent(createdAt) {
    return this.findAndInstantiate(_QuizSessionEvent.default, ['quizSessionEvents', createdAt]);
  },
  findItem: function findItem(id) {
    if (id === _constants.TEMPORARY_ITEM_ID) {
      return this.findTemporaryItem();
    }

    return this.findAndInstantiate(_Item.default, ['items', id]);
  },
  findQuizEntry: function findQuizEntry(id) {
    if (id === _constants.TEMPORARY_QUIZ_ENTRY_ID) {
      return this.findTemporaryQuizEntry();
    }

    return this.findAndInstantiate(_QuizEntry.default, ['quizEntries', id]);
  },
  findItemAnalysis: function findItemAnalysis(itemKey) {
    var state = _reduxStore.default.getAppState();

    var itemAnalysisData = state.getIn(['reporting', 'item_analysis', itemKey]);
    return new _ItemAnalysis.default(itemAnalysisData);
  },
  findQuizAnalysis: function findQuizAnalysis(quizKey) {
    var state = _reduxStore.default.getAppState();

    var quizAnalysisData = state.getIn(['reporting', 'quiz_analysis', quizKey]);
    return new _QuizAnalysis.default(quizAnalysisData);
  },
  findTemporaryItem: function findTemporaryItem() {
    var temporaryItemData = this.state().getIn(['modifications', 'items', _constants.TEMPORARY_ITEM_ID], (0, _immutable.Map)());
    return _Item.default.create(temporaryItemData);
  },
  findTemporaryQuizEntry: function findTemporaryQuizEntry() {
    var temporaryQuizEntryData = this.state().getIn(['modifications', 'quizEntries', _constants.TEMPORARY_QUIZ_ENTRY_ID], (0, _immutable.Map)({
      entry: _constants.TEMPORARY_ITEM_ID
    }));
    return _QuizEntry.default.create(temporaryQuizEntryData);
  },
  findInteractionType: function findInteractionType(id) {
    var interactionTypeData = this.state().getIn(['interactionTypes', id]) || (0, _immutable.Map)();
    var slug = interactionTypeData.get('slug');
    var IntTypeRecord = (0, _interactionTypeRecordForItem.default)(slug);
    return new IntTypeRecord(interactionTypeData);
  },
  // MULTIPLE RECORDS
  findQuizSessions: function findQuizSessions() {
    var _this = this;

    var ids = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
    return ids.reduce(function (list, id) {
      var quizSession = _this.findQuizSession(id);

      return quizSession ? list.push(quizSession) : list;
    }, (0, _immutable.List)());
  },
  interactionTypes: function interactionTypes() {
    var _this2 = this;

    return this.state().get('interactionTypes', (0, _immutable.Map)()).map(function (intTypeData) {
      return _this2.findInteractionType(intTypeData.get('id'));
    }).valueSeq().toList();
  },
  // MISC.
  __isShowingErrors: function __isShowingErrors(quizEntry) {
    var isInvalid = !quizEntry.getEntry().getWorkingInstance().isValid();
    var isEditing = this.state().getIn(['editing', "".concat(quizEntry.id, "_").concat(_constants.QUIZ_ENTRY_GUID_SUFFIX)]);
    return isInvalid && isEditing;
  },
  quizEntryWithErrorsShowing: function quizEntryWithErrorsShowing() {
    var _this3 = this;

    var qeWithErrors = this.state().get('quizEntries').find(function (qe) {
      return _this3.__isShowingErrors(_QuizEntry.default.create(qe));
    });

    if (qeWithErrors) {
      return qeWithErrors;
    }

    var temp = this.findTemporaryQuizEntry();

    if (this.__isShowingErrors(temp)) {
      return temp;
    }
  },
  editingQuizEntry: function editingQuizEntry() {
    return this.state().get('editing', []).find(function (isEditing, key) {
      return isEditing && key.includes(_constants.QUIZ_ENTRY_GUID_SUFFIX);
    });
  },
  gradingSessionItems: function gradingSessionItems(sessionItems, scope) {
    return sessionItems.filter(scope).map(function (si) {
      return _SessionItem.default.create(si);
    }).sortBy(function (si) {
      return si.position;
    }).toList();
  },
  gradingSessionItemResults: function gradingSessionItemResults(sessionItemResults, scope) {
    return sessionItemResults.toList().filter(scope);
  }
}, ((0, _applyDecoratedDescriptor2.default)(_obj, "interactionTypes", [_dec], Object.getOwnPropertyDescriptor(_obj, "interactionTypes"), _obj), (0, _applyDecoratedDescriptor2.default)(_obj, "quizEntryWithErrorsShowing", [_dec2], Object.getOwnPropertyDescriptor(_obj, "quizEntryWithErrorsShowing"), _obj), (0, _applyDecoratedDescriptor2.default)(_obj, "editingQuizEntry", [_dec3], Object.getOwnPropertyDescriptor(_obj, "editingQuizEntry"), _obj), (0, _applyDecoratedDescriptor2.default)(_obj, "gradingSessionItems", [_withCachedArguments.default], Object.getOwnPropertyDescriptor(_obj, "gradingSessionItems"), _obj), (0, _applyDecoratedDescriptor2.default)(_obj, "gradingSessionItemResults", [_withCachedArguments.default], Object.getOwnPropertyDescriptor(_obj, "gradingSessionItemResults"), _obj)), _obj));

function timeIsUp() {
  var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : stateHelper.state();
  var timerRunning = state.getIn(['taking', 'timerData', 'timerRunning']);
  var noTimeLeft = state.getIn(['taking', 'timerData', 'timeLeftInSeconds']) <= 0;
  return noTimeLeft && timerRunning;
}

var _default = stateHelper;
exports.default = _default;