'use strict';

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

var _immutable = require('immutable');

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

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

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

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

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

var _presenter2 = _interopRequireDefault(_presenter);

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

var _QuizSession2 = _interopRequireDefault(_QuizSession);

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

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

  var submitFlag = state.getIn(['taking', 'submitFlag']);
  var response = state.getIn(['taking', 'responses'], (0, _immutable.List)()).find(function (resp) {
    return resp.get('position') === currentSessionItemPosition;
  });

  var _ref = response && response.toJS() || {},
      userResponse = _ref.userResponse;

  // Note: this returns a Map() so things don't 'splode on initial render and sessionItems aren't loaded yet
  //       but it should never really be needed otherwise


  var sessionItem = state.get('sessionItems', (0, _immutable.Map)()).filter(function (record) {
    return record.get('quizSessionId') === quizSessionId;
  }).find(function (record) {
    return record.get('position') === currentSessionItemPosition;
  }) || (0, _immutable.Map)();
  var itemId = sessionItem.getIn(['item', 'id']);

  var sessionItemsCount = quizSession.get('sessionItemsCount');
  var allowBacktracking = quizSession.get('allowBacktracking');
  var isOneQuestionAtATime = quizSession.isOneQuestionAtATime();
  var failedToStart = quizSession.failedToStart();
  // I don't think this exists in the store yet
  var largeFormatResponse = null;
  var isNextItemFetching = state.getIn(['calls', _constants.OQAAT_NO_BACKTRACKING_NEXT_ITEM_FETCH_CALL, 'requestStatus']) === 'calling';

  var currentQuestionIsLast = currentSessionItemPosition === sessionItemsCount || sessionItemsCount === 0;

  var disabled = submitFlag || failedToStart || isNextItemFetching && isOneQuestionAtATime;

  return {
    allowBacktracking: allowBacktracking,
    currentQuestionIsLast: currentQuestionIsLast,
    disabled: disabled,
    isOneQuestionAtATime: isOneQuestionAtATime,
    itemId: itemId,
    largeFormatResponse: largeFormatResponse,
    quizSessionId: quizSessionId,
    userResponse: userResponse
  };
}

var mapDispatchToProps = exports.mapDispatchToProps = {
  nextQuestion: _taking.nextQuestion,
  screenreaderNotification: _alerts.screenreaderNotification,
  submitQuiz: _taking.submitQuiz
};

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