"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 _SidebarItem = _interopRequireDefault(require("../../../../../common/components/layout/sidebar/SidebarItem"));

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

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

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

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

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

        var handleWrapper = (0, _dragAndDropUtils.usingIE)() ? function (C) {
          return C;
        } : connectDragSource;
        return handleWrapper(dragHandle);
      }
    }
  }, {
    key: "renderItem",
    value: function renderItem() {
      var _this$props2 = this.props,
          draggingOver = _this$props2.draggingOver,
          draggingAboveHalf = _this$props2.draggingAboveHalf,
          inStimulus = _this$props2.inStimulus,
          sidebarOpen = _this$props2.sidebarOpen;
      var propsForDnDZone = {
        inSidebar: true,
        inStimulus: inStimulus,
        sidebarOpen: sidebarOpen,
        draggingOver: draggingOver,
        draggingAboveHalf: draggingAboveHalf
      };
      return _react.default.createElement("div", null, _react.default.createElement(_DragAndDropZone.default, Object.assign({}, propsForDnDZone, {
        location: "above"
      })), _react.default.createElement(_SidebarItem.default, {
        itemId: this.props.itemId,
        itemBody: this.props.itemBody,
        itemName: this.props.itemName,
        inStimulus: this.props.inStimulus,
        position: this.props.displayPosition,
        pointsPossible: this.props.pointsPossible,
        quizEntry: this.props.quizEntry,
        sidebarOpen: this.props.sidebarOpen
      }, this.renderDragHandle()), _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.renderItem()));
      }

      return this.renderItem();
    }
  }]);
  BuildingSidebarItem.displayName = "BuildingSidebarItem";
  return BuildingSidebarItem;
}(_react.Component);

exports.default = BuildingSidebarItem;
BuildingSidebarItem.propTypes = {
  connectDragSource: _propTypes.default.func.isRequired,
  // inserted by React DnD
  connectDragPreview: _propTypes.default.func.isRequired,
  // inserted by React DnD
  connectDropTarget: _propTypes.default.func.isRequired,
  // inserted by React DnD
  disableDraggingOverride: _propTypes.default.bool,
  draggingInSidebar: _propTypes.default.bool.isRequired,
  // Passed in by the Sidebar
  displayPosition: _propTypes.default.number.isRequired,
  inStimulus: _propTypes.default.bool,
  itemBody: _propTypes.default.string,
  itemId: _propTypes.default.string.isRequired,
  itemName: _propTypes.default.string.isRequired,
  pointsPossible: _propTypes.default.number.isRequired,
  quizEntry: _reactImmutableProptypes.default.record.isRequired,
  quizEntries: _reactImmutableProptypes.default.listOf(_reactImmutableProptypes.default.record).isRequired,
  sidebarOpen: _propTypes.default.bool.isRequired,
  // Passed in by connect
  draggingOver: _propTypes.default.bool.isRequired,
  draggingAboveHalf: _propTypes.default.bool.isRequired
};
BuildingSidebarItem.defaultProps = {
  disableDraggingOverride: false,
  inStimulus: false,
  itemBody: ''
};