'use strict';

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

var _redux = require('redux');

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

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

var _QuizSession2 = _interopRequireDefault(_QuizSession);

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

var _stateHelper2 = _interopRequireDefault(_stateHelper);

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

var takingApiActions = _interopRequireWildcard(_taking);

var _taking2 = require('../../../../common/actions/taking');

var takingActionCreators = _interopRequireWildcard(_taking2);

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

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

var _presenter2 = _interopRequireDefault(_presenter);

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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function mapStateToProps(state, ownProps) {
  var quizSessionId = ownProps.quizSessionId || state.getIn(['quizSessions', 'activeQuizSessionId']);
  var quizSession = _QuizSession2.default.create(state.getIn(['quizSessions', quizSessionId]));
  var isOneQuestionAtATime = quizSession.isOneQuestionAtATime();

  var currentSessionItemPosition = state.getIn(['taking', 'currentSessionItemPosition']) || 1;
  var quizSessionStatusOnPageLoad = state.getIn(['taking', 'quizSessionStatusOnPageLoad']) || '';
  var useTimer = state.getIn(['taking', 'timerData', 'timerRunning']);
  var useRecording = state.getIn(['taking', 'timerData', 'timeRecording']);
  var resumeDataLoaded = isOneQuestionAtATime && !quizSession.allowBacktracking || state.getIn(['taking', 'responses']) !== void 0;

  return {
    appContainer: state.getIn(['ui', 'appContainer']),
    isOneQuestionAtATime: isOneQuestionAtATime,
    currentSessionItemPosition: currentSessionItemPosition,
    quizSession: quizSession,
    quizSessionId: quizSessionId,
    quizSessionStatusOnPageLoad: quizSessionStatusOnPageLoad,
    resumeDataLoaded: resumeDataLoaded,
    timeIsUp: (0, _stateHelper.timeIsUp)(),
    useTimer: useTimer || useRecording
  };
}

function mapDispatchToProps(dispatch) {
  var boundTakingApiActions = (0, _redux.bindActionCreators)(takingApiActions, dispatch);
  return {
    addInfo: (0, _redux.bindActionCreators)(_alerts.addInfo, dispatch),
    closeSubmitConfirmationModal: boundTakingApiActions.closeSubmitConfirmModal,
    fetchQuizSession: boundTakingApiActions.confirmSessionStartPage,
    getTimeRemaining: boundTakingApiActions.getTimeRemaining,
    getTimerData: _stateHelper2.default.getTimerData.bind(_stateHelper2.default),
    newQuizTakingSession: boundTakingApiActions.newQuizTakingSession,
    onQuizSessionReadyForTaking: boundTakingApiActions.onQuizSessionReadyForTaking,
    recordQuizTime: boundTakingApiActions.recordQuizTime,
    screenreaderNotification: (0, _redux.bindActionCreators)(_alerts.screenreaderNotification, dispatch),
    submitQuiz: boundTakingApiActions.submitQuiz,
    takingActions: (0, _redux.bindActionCreators)(takingActionCreators, dispatch)
  };
}

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