'use strict';

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

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

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

var _presenter2 = _interopRequireDefault(_presenter);

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

var _stateHelper2 = _interopRequireDefault(_stateHelper);

var _modal = require('../../../../common/actions/modal');

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

var _featureCheck = require('../../../../common/util/featureCheck');

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

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

function mapStateToProps(state) {
  var quizSessionIds = state.getIn(['moderation', 'selectedSessions']);
  var sessions = _stateHelper2.default.findQuizSessions(quizSessionIds).filter(function (qs) {
    return qs.isLoaded() && !qs.pending();
  }).sort(function (qs1, qs2) {
    return qs2.id - qs1.id;
  });

  var sessionId = void 0,
      session = void 0;
  if (sessions.size > 0) {
    session = sessions.get(0);
    sessionId = session.id;
  }

  return {
    sessionId: sessionId || null,
    isComplete: session ? !(session.inProgress() || session.reopeningGraded()) : false,
    reopenOn: (0, _featureCheck.featureOn)('reopen_session')
  };
}

exports.default = (0, _reactRedux.connect)(mapStateToProps, {
  reopenQuizSession: _quizSessions.reopenQuizSession,
  screenreaderNotification: _alerts.screenreaderNotification,
  withConfirm: _modal.withConfirm
})(_presenter2.default);