'use strict';

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

var _redux = require('redux');

var _immutable = require('immutable');

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

var _makeScrollable = require('../../../../../common/components/shared/functionality/makeScrollable');

var _makeScrollable2 = _interopRequireDefault(_makeScrollable);

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

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

var _QuizSession2 = _interopRequireDefault(_QuizSession);

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

var _presenter2 = _interopRequireDefault(_presenter);

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

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

var _quizzes = require('../../../../../building/api/quizzes');

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

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

var _index = require('../../../../../common/components/ImportModal/index');

var _Response = require('../../../../../common/records/Response');

var _Response2 = _interopRequireDefault(_Response);

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

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

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

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

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

function mapStateToProps(state, ownProps) {
  var quizSessionId = ownProps.quizSessionId;
  var quizSession = _QuizSession2.default.create(state.getIn(['quizSessions', quizSessionId]));
  var allowBacktracking = quizSession.allowBacktracking;
  var isOneQuestionAtATime = quizSession.isOneQuestionAtATime();
  var showPin = quizSession.id && (allowBacktracking || !isOneQuestionAtATime);
  var position = ownProps.sessionItem.position;
  var isPinned = quizSession.pins().includes(position);
  var item = ownProps.sessionItem.getItem();
  var interactionType = item.getInteractionType();

  var defaultResponse = (0, _immutable.fromJS)({
    userResponse: interactionType.getDefaultUserResponse(item.interactionData.toJS())
  });
  var response = new _Response2.default(state.getIn(['taking', 'responses', position], defaultResponse));

  return {
    activeCalculatorId: (0, _ui.getActiveCalculatorId)(state),
    appContainer: state.getIn(['ui', 'appContainer']),
    calculatorPerQuestionEnabled: (0, _featureCheck.featureOn)('calculator_per_question'),
    interactionType: interactionType,
    isPinned: isPinned,
    item: item,
    pointsPossible: ownProps.sessionItem.pointsPossible,
    position: position,
    quizSessionId: quizSessionId,
    response: response,
    scrollKey: ownProps.sessionItem.id,
    timeIsUp: (0, _stateHelper.timeIsUp)(state),
    showPin: showPin
  };
}

function mapDispatchToProps(dispatch) {
  return {
    addError: (0, _redux.bindActionCreators)(_alerts.addError, dispatch),
    authenticateLearnosity: _taking2.authenticateLearnosity,
    editResponse: (0, _redux.bindActionCreators)(_taking.editResponse, dispatch),
    interactionFileUpload: (0, _redux.bindActionCreators)(_quizzes.interactionFileUpload, dispatch),
    pinSessionItem: (0, _redux.bindActionCreators)(_quizSessions.pinSessionItem, dispatch),
    previewResponse: (0, _redux.bindActionCreators)(_taking.previewResponse, dispatch),
    screenreaderNotification: (0, _redux.bindActionCreators)(_alerts.screenreaderNotification, dispatch),
    setActiveCalculatorId: (0, _redux.bindActionCreators)(_ui2.setActiveCalculatorId, dispatch),
    openImportModal: function openImportModal(importModalId) {
      dispatch((0, _modal.openModal)((0, _index.generateImportModalId)(importModalId)));
    }
  };
}

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