'use strict';

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

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

var _redux = require('redux');

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

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

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

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

var _stateHelper2 = _interopRequireDefault(_stateHelper);

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

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

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

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

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

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

var _disableDraggingOverride = require('../../../../../common/components/shared/functionality/disableDraggingOverride');

var _disableDraggingOverride2 = _interopRequireDefault(_disableDraggingOverride);

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

var _presenter2 = _interopRequireDefault(_presenter);

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 dndPosition = state.getIn(['dragAndDrop', 'position']);
  var draggingAboveHalf = state.getIn(['dragAndDrop', 'aboveHalf'], false);
  var draggingOver = dndPosition === props.quizEntry.position;

  return {
    dndPositionObj: draggingOver ? state.get('dragAndDrop') : null, // for DnD
    draggingOver: draggingOver && props.draggingInSidebar,
    draggingAboveHalf: draggingOver && draggingAboveHalf,
    quizId: activeQuiz.id // for DnD,
  };
};

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
    scrollToItem: props.scrollToItem || (0, _redux.bindActionCreators)(_scrolling.scrollToItem, dispatch)
  };
};

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, _disableDraggingOverride2.default)(_presenter2.default);