'use strict';

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

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

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

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

var _stateHelper2 = _interopRequireDefault(_stateHelper);

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

var _presenter2 = _interopRequireDefault(_presenter);

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

function getAttemptData(attempt) {
  var authoritativeResult = void 0,
      quizSession = void 0;
  var resultObject = {};
  if (attempt.quizSessionId) {
    quizSession = _stateHelper2.default.findQuizSession(attempt.quizSessionId);
    authoritativeResult = quizSession.findAuthoritativeResult();
    resultObject = authoritativeResult.toObject();
  }

  return Object.assign({}, attempt, {
    score: resultObject.score,
    pointsPossible: resultObject.pointsPossible
  });
}

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

  if (!attemptHistory || attemptHistory.length === 0) return {};

  return {
    attemptHistory: attemptHistory.map(getAttemptData)
  };
}

var mapDispatchToProps = exports.mapDispatchToProps = { getQuizSessions: _quizSessions.getQuizSessions };

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