'use strict';

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

var _immutable = require('immutable');

var _reactRedux = require('../../../../common/react-redux');

var _stateHelper = require('../../../../common/util/stateHelper');

var _stateHelper2 = _interopRequireDefault(_stateHelper);

var _postMessage = require('../../../../common/actions/postMessage');

var _quizSessions = require('../../../../common/actions/quizSessions');

var _updateResults = require('../../../api/updateResults');

var _modifications = require('../../../../common/actions/modifications');

var _alerts = require('../../../../common/actions/alerts');

var _ui = require('../../../../common/actions/ui');

var _QuizSession = require('../../../../common/records/QuizSession');

var _QuizSession2 = _interopRequireDefault(_QuizSession);

var _QuizSessionResult = require('../../../../common/records/QuizSessionResult');

var _QuizSessionResult2 = _interopRequireDefault(_QuizSessionResult);

var _SessionItemResult = require('../../../../common/records/SessionItemResult');

var _SessionItemResult2 = _interopRequireDefault(_SessionItemResult);

var _presenter = require('./presenter');

var _presenter2 = _interopRequireDefault(_presenter);

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

var cachedQuizSessionId = null;
var cachedScope = function cachedScope() {};

var mapStateToProps = exports.mapStateToProps = function mapStateToProps(state, ownProps) {
  var quizSessionId = ownProps.quizSessionId;

  // We try to cache the scope function in order to not trick Redux into thinking something
  // has changed on the component whenever state changes.
  if (cachedQuizSessionId !== quizSessionId) {
    cachedQuizSessionId = quizSessionId;
    cachedScope = function cachedScope(record) {
      return record.get('quizSessionId') === cachedQuizSessionId;
    };
  }

  var quizSession = _QuizSession2.default.create(state.getIn(['quizSessions', quizSessionId]));
  var qsrId = quizSession.authoritativeResult;
  var authoritativeResult = _QuizSessionResult2.default.create(state.getIn(['quizSessionResults', qsrId]));

  var sessionItems = _stateHelper2.default.gradingSessionItems(state.get('sessionItems', (0, _immutable.Map)()), cachedScope);

  var sessionItemResults = _stateHelper2.default.gradingSessionItemResults(state.get('sessionItemResults', (0, _immutable.Map)()), cachedScope);

  var needToLoad = !authoritativeResult.isGraded() || !authoritativeResult.isLoaded() || !quizSession.isLoaded() || sessionItems.size === 0 || sessionItemResults.size === 0;

  var hasSessionItemResultsMods = sessionItemResults.some(function (sessionItemResult) {
    return _SessionItemResult2.default.create(sessionItemResult).hasModifications();
  });

  return {
    hasResultsModifications: hasSessionItemResultsMods || authoritativeResult.hasModifications(),
    needToLoad: needToLoad,
    quiz: _stateHelper2.default.activeQuiz(),
    quizSessionResult: authoritativeResult,
    regradingIdentifier: state.getIn(['regrading', 'regradeEditModeKey'], ''),
    sessionItems: sessionItems,
    updatingGrade: state.getIn(['ui', 'updatingGrade'], false),
    workingQuizSessionResult: authoritativeResult.getWorkingInstance()
  };
};

var actionProps = {
  changeFudgePoints: _modifications.changeFudgePoints,
  fetchQuizSession: _quizSessions.getResultInfo,
  newActiveQuizSession: _quizSessions.newActiveQuizSession,
  postQuizSessionResults: _updateResults.postQuizSessionResults,
  postToParent: _postMessage.postToParent,
  screenreaderNotification: _alerts.screenreaderNotification,
  sessionItemResultsModification: _modifications.sessionItemResultsModification,
  setUpdatingGrade: function setUpdatingGrade(bool) {
    return (0, _ui.set)('updatingGrade', bool);
  },
  successNotification: _alerts.addSuccess
};

exports.default = (0, _reactRedux.connect)(mapStateToProps, actionProps)(_presenter2.default);