'use strict';

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

var _redux = require('redux');

var _immutable = require('immutable');

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

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

var _stateHelper2 = _interopRequireDefault(_stateHelper);

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

var _sidebar = require('../../../../common/actions/sidebar');

var _scrolling = require('../../../../common/actions/scrolling');

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

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

var _presenter2 = _interopRequireDefault(_presenter);

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

var getPinnedItems = exports.getPinnedItems = function getPinnedItems(state, quizSession) {
  if (state.getIn(['sessionItems'], (0, _immutable.Map)()).size === 0) {
    return (0, _immutable.List)();
  }
  return quizSession.getIn(['takingData', 'pins'], (0, _immutable.List)()).map(function (pinPosition) {
    var si = state.get('sessionItems').find(function (sessionItem) {
      return sessionItem.get('position') === pinPosition;
    });
    return { position: pinPosition, sessionItemId: si.get('id') };
  });
};

var mapStateToProps = exports.mapStateToProps = function mapStateToProps(state, props) {
  var quizSession = _stateHelper2.default.activeQuizSession();
  var pinnedItems = getPinnedItems(state, quizSession);

  return {
    allowBacktracking: _stateHelper2.default.backtrackingIsAllowed(),
    isOneQuestionAtATime: _stateHelper2.default.isOneQuestionAtATime(),
    pinnedItems: pinnedItems,
    quizTitle: quizSession.getQuiz().title,
    sessionItems: quizSession.getSessionItems(),
    sessionNotLoaded: quizSession.id === '0',
    sidebarOpen: props.sidebarOpen || state.getIn(['sidebar', 'isOpen'])
  };
};

var mapDispatchToProps = exports.mapDispatchToProps = function mapDispatchToProps(dispatch, props) {
  return {
    scrollToItem: props.scrollToItem || (0, _redux.bindActionCreators)(_scrolling.scrollToItem, dispatch),
    toggleSidebar: props.toggleSidebar || (0, _redux.bindActionCreators)(_sidebar.toggleSidebar, dispatch),
    screenreaderNotification: (0, _redux.bindActionCreators)(_alerts.screenreaderNotification, dispatch),
    goToItem: function goToItem(sessionItemPosition) {
      return (0, _redux.bindActionCreators)(_taking.setCurrentSessionItemPosition, dispatch)(sessionItemPosition);
    }
  };
};

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