"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

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

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

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

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

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

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

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _reactImmutableProptypes = _interopRequireDefault(require("react-immutable-proptypes"));

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

var _DragHandle = _interopRequireDefault(require("../../../../../common/components/shared/drag_and_drop/DragHandle"));

var _DragAndDropZone = _interopRequireDefault(require("../../../../../common/components/shared/drag_and_drop/DragAndDropZone"));

var _Stimulus = _interopRequireDefault(require("../../../../../common/components/layout/sidebar/Stimulus"));

var BuildingStimulus =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(BuildingStimulus, _Component);

  function BuildingStimulus() {
    (0, _classCallCheck2.default)(this, BuildingStimulus);
    return (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(BuildingStimulus).apply(this, arguments));
  }

  (0, _createClass2.default)(BuildingStimulus, [{
    key: "shouldDisableDragHandle",
    value: function shouldDisableDragHandle() {
      var _this$props = this.props,
          childEntries = _this$props.childEntries,
          quizEntries = _this$props.quizEntries;
      return quizEntries.size === (childEntries && childEntries.size || 0) + 1;
    }
  }, {
    key: "renderDragHandle",
    value: function renderDragHandle() {
      var _this$props2 = this.props,
          connectDragSource = _this$props2.connectDragSource,
          sidebarOpen = _this$props2.sidebarOpen;

      if (sidebarOpen) {
        var dragHandle = // This wrapper div is necessary for drag and drop
        _react.default.createElement("div", null, _react.default.createElement(_DragHandle.default, {
          disabled: this.shouldDisableDragHandle(),
          displayPosition: this.props.displayPosition,
          onSidebar: true,
          quizEntry: this.props.quizEntry,
          quizEntries: this.props.quizEntries
        }));

        if (this.shouldDisableDragHandle()) {
          return dragHandle;
        }

        var handleWrapper = (0, _dragAndDropUtils.usingIE)() ? function (C) {
          return C;
        } : connectDragSource;
        return handleWrapper(dragHandle);
      }
    }
  }, {
    key: "renderStimulus",
    value: function renderStimulus() {
      return _react.default.createElement(_Stimulus.default, {
        displayPosition: this.props.displayPosition,
        draggingOver: this.props.draggingOver,
        draggingAboveHalf: this.props.draggingAboveHalf,
        dropIntoEmptyStimulus: this.props.dropIntoEmptyStimulus,
        isDragging: this.props.isDragging,
        isOver: this.props.isOver,
        itemId: this.props.itemId,
        stimulusParent: this.props.quizEntry,
        renderSidebarItem: this.props.renderSidebarItem,
        sidebarOpen: this.props.sidebarOpen
      }, this.renderDragHandle());
    }
  }, {
    key: "renderStimulusWrapper",
    value: function renderStimulusWrapper() {
      var propsForDnDZone = {
        inSidebar: true,
        inStimulus: true,
        sidebarOpen: this.props.sidebarOpen,
        draggingOver: this.props.draggingOver,
        draggingAboveHalf: this.props.draggingAboveHalf,
        dropIntoEmptyStimulus: this.props.dropIntoEmptyStimulus
      };
      return _react.default.createElement("div", null, _react.default.createElement(_DragAndDropZone.default, Object.assign({}, propsForDnDZone, {
        location: "above"
      })), this.renderStimulus(), _react.default.createElement(_DragAndDropZone.default, Object.assign({}, propsForDnDZone, {
        location: "in"
      })), _react.default.createElement(_DragAndDropZone.default, Object.assign({}, propsForDnDZone, {
        location: "below"
      })));
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props3 = this.props,
          connectDragPreview = _this$props3.connectDragPreview,
          connectDragSource = _this$props3.connectDragSource,
          connectDropTarget = _this$props3.connectDropTarget,
          sidebarOpen = _this$props3.sidebarOpen;
      var dragConnection = (0, _dragAndDropUtils.chooseDragConnection)(connectDragSource, connectDragPreview);

      if (sidebarOpen && !this.shouldDisableDragHandle()) {
        return dragConnection(connectDropTarget(this.renderStimulusWrapper()));
      }

      return this.renderStimulusWrapper();
    }
  }]);
  BuildingStimulus.displayName = "BuildingStimulus";
  return BuildingStimulus;
}(_react.Component);

exports.default = BuildingStimulus;
BuildingStimulus.propTypes = {
  childEntries: _reactImmutableProptypes.default.listOf(_reactImmutableProptypes.default.record).isRequired,
  connectDragPreview: _propTypes.default.func.isRequired,
  // inserted by React DnD
  connectDragSource: _propTypes.default.func.isRequired,
  // inserted by React DnD
  connectDropTarget: _propTypes.default.func.isRequired,
  // inserted by React DnD
  isDragging: _propTypes.default.bool.isRequired,
  // inserted by React DnD
  // Passed in by the Sidebar
  displayPosition: _propTypes.default.number.isRequired,
  isOver: _propTypes.default.bool,
  itemId: _propTypes.default.string.isRequired,
  quizEntries: _reactImmutableProptypes.default.listOf(_reactImmutableProptypes.default.record).isRequired,
  quizEntry: _reactImmutableProptypes.default.record.isRequired,
  renderSidebarItem: _propTypes.default.func.isRequired,
  sidebarOpen: _propTypes.default.bool.isRequired,
  // Passed in by connect
  draggingAboveHalf: _propTypes.default.bool.isRequired,
  draggingOver: _propTypes.default.bool.isRequired,
  dropIntoEmptyStimulus: _propTypes.default.bool
};
BuildingStimulus.defaultProps = {
  draggingAboveHalf: false,
  dropIntoEmptyStimulus: false,
  isOver: false
};