'use strict';

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

var _redux = require('redux');

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

var _constants = require('../../../../constants');

var _taking = require('../../../api/taking');

var takingActions = _interopRequireWildcard(_taking);

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

var uiActions = _interopRequireWildcard(_ui);

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

var _stateHelper2 = _interopRequireDefault(_stateHelper);

var _Quiz = require('../../../../common/records/Quiz');

var _Quiz2 = _interopRequireDefault(_Quiz);

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

var _presenter2 = _interopRequireDefault(_presenter);

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

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function mapStateToProps(state, __props__) {
  var helper = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _stateHelper2.default;

  var quizSession = helper.activeQuizSession();
  var quiz = _Quiz2.default.create(state.getIn(['quizzes', quizSession.quiz]));

  var useQuizSessionOQAAT = !quizSession.pending();
  var allowBacktracking = useQuizSessionOQAAT ? quizSession.allowBacktracking : quiz.allowBacktracking;

  var isOneAtATime = useQuizSessionOQAAT ? quizSession.isOneQuestionAtATime() : quiz.isOneQuestionAtATime();

  var shouldShowLinkButton = quiz.quizEntriesCount > 0 && (quizSession.submitted() || !quiz.requireStudentAccessCode || quizSession.acceptedStudentAccessCode);

  var storedAccessCode = state.getIn(['ui', _constants.STUDENT_ACCESS_CODE_VALUE]);

  return {
    allowBacktracking: allowBacktracking,
    isOneAtATime: isOneAtATime,
    shouldShowLinkButton: shouldShowLinkButton,
    needQuiz: quizSession.id !== '0',
    quiz: quiz,
    quizSession: quizSession,
    accessCode: storedAccessCode === void 0 ? '' : storedAccessCode,
    accessCodeStatus: state.getIn(['ui', _constants.STUDENT_ACCESS_CODE_STATUS])
  };
}

var mapDispatchToProps = function mapDispatchToProps(dispatch) {
  return {
    confirmSessionStartPage: (0, _redux.bindActionCreators)(takingActions, dispatch).confirmSessionStartPage,
    submitStudentAccessCode: (0, _redux.bindActionCreators)(takingActions, dispatch).submitStudentAccessCode,
    updateAccessCodeUi: function updateAccessCodeUi(code) {
      return dispatch(uiActions.set(_constants.STUDENT_ACCESS_CODE_VALUE, code));
    },
    clearAccessCodeStatus: function clearAccessCodeStatus() {
      return dispatch(uiActions.set(_constants.STUDENT_ACCESS_CODE_STATUS, void 0));
    }
  };
};

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