"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

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 = _interopRequireDefault(require("../../../../../common/util/stateHelper"));

var _presenter = _interopRequireDefault(require("./presenter"));

var _constants = require("@instructure/quiz-common/lib/constants");

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

  var activeQuiz = _stateHelper.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

  };
};

exports.mapStateToProps = mapStateToProps;

var mapDispatchToProps = function mapDispatchToProps(dispatch) {
  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

  };
};

exports.mapDispatchToProps = mapDispatchToProps;

var 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.connectToDragSource = connectToDragSource;

var _default = (0, _redux.compose)((0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps), (0, _dragAndDropUtils.DropTargetCreator)(_constants.SIDEBAR_ITEM_REF), connectToDragSource)(_presenter.default);

exports.default = _default;