'use strict';

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

var _redux = require('redux');

var _reactDnd = require('react-dnd');

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

var _dragAndDropUtils = require('../../../../../common/components/shared/drag_and_drop/dragAndDropUtils');

var _dragAndDrop = require('../../../../../common/actions/dragAndDrop');

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

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

var _quizEntries = require('../../../../api/quizEntries');

var _modifications = require('../../../../../common/actions/modifications');

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

var _stateHelper2 = _interopRequireDefault(_stateHelper);

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

var _presenter2 = _interopRequireDefault(_presenter);

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

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

var mapStateToProps = exports.mapStateToProps = function mapStateToProps() {
  var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : Map();
  var props = arguments[1];

  var activeQuiz = _stateHelper2.default.activeQuiz();
  var quizEntriesIncludingTemp = activeQuiz.getWorkingQuizEntries();
  var itemsCollectionSize = quizEntriesIncludingTemp.size || quizEntriesIncludingTemp.length || 0;
  var dndPosition = state.getIn(['dragAndDrop', 'position']);
  var draggingAboveHalf = state.getIn(['dragAndDrop', 'aboveHalf'], false);
  var draggingOver = dndPosition === props.quizEntry.position;
  var dropIntoEmptyStimulus = state.getIn(['dragAndDrop', 'intoEmptyStimulus']);
  return {
    childEntries: props.quizEntry.getNestedEntries(),
    dndPositionObj: draggingOver ? state.get('dragAndDrop') : null, // for DnD
    draggingOver: draggingOver && props.draggingInSidebar,
    draggingAboveHalf: draggingOver && draggingAboveHalf,
    dropIntoEmptyStimulus: draggingOver && dropIntoEmptyStimulus,
    itemsCollectionSize: itemsCollectionSize,
    quizId: activeQuiz.id // forDnD
  };
};

var mapDispatchToProps = exports.mapDispatchToProps = function mapDispatchToProps(dispatch, props) {
  return {
    moveTemporaryQuizEntry: (0, _redux.bindActionCreators)(_modifications.moveTemporaryQuizEntry, dispatch), // for DnD
    moveQuizEntry: (0, _redux.bindActionCreators)(_quizEntries.moveQuizEntry, dispatch), // for DnD
    newHoverPosition: (0, _redux.bindActionCreators)(_dragAndDrop.newHoverPosition, dispatch), // for DnD
    openMoveImmutableModal: (0, _redux.bindActionCreators)(_modal.openMoveImmutableModal, dispatch), // for DnD
    setUi: (0, _redux.bindActionCreators)(_ui.set, dispatch) // for DnD
  };
};

var connectToDragSource = exports.connectToDragSource = function connectToDragSource(Component) {
  return (0, _reactDnd.DragSource)(
  // ItemTypes.QUIZ_ENTRY associates this component drag source with a name
  // dragSource specifies functions React DnD will call for events with this component
  // collectDrag injects props into component
  _dragAndDropUtils.ItemTypes.QUIZ_ENTRY, _dragAndDropUtils.dragSource, _dragAndDropUtils.collectDrag)(Component);
};

exports.default = (0, _redux.compose)((0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps), (0, _dragAndDropUtils.DropTargetCreator)(_constants.SIDEBAR_ITEM_REF), connectToDragSource)(_presenter2.default);