'use strict';

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

var _redux = require('redux');

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

var _formatMessage = require('../../../../../common/format-message');

var _formatMessage2 = _interopRequireDefault(_formatMessage);

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

var _stateHelper2 = _interopRequireDefault(_stateHelper);

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

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

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

var _presenter2 = _interopRequireDefault(_presenter);

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

function mapStateToProps(store, props) {
  var quizSession = _stateHelper2.default.activeQuizSession();
  var isAnswered = store.getIn(['taking', 'responses', props.position]) !== void 0;

  return {
    isPinned: quizSession.pins().includes(props.position),
    isAnswered: isAnswered
  };
}

function mapDispatchToProps(dispatch, props) {
  var quizSession = _stateHelper2.default.activeQuizSession();
  var srNotification = (0, _redux.bindActionCreators)(_alerts.screenreaderNotification, dispatch);
  var pinAction = (0, _redux.bindActionCreators)(_quizSessions.pinSessionItem, dispatch);
  var pinSessionItemAction = function pinSessionItemAction(isPinned) {
    var confirmationMessage = isPinned ? 'Objekt ist unfixiert.' : 'Objekt fixiert';

    return function () {
      pinAction(quizSession.id, props.position).then(function () {
        return srNotification(confirmationMessage);
      });
    };
  };
  return {
    pinSessionItem: pinSessionItemAction
  };
}

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