'use strict';

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

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _class, _temp;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactImmutableProptypes = require('react-immutable-proptypes');

var _reactImmutableProptypes2 = _interopRequireDefault(_reactImmutableProptypes);

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

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

var _DragHandle2 = _interopRequireDefault(_DragHandle);

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

var _DragAndDropZone2 = _interopRequireDefault(_DragAndDropZone);

var _SidebarItem = require('../../../../../common/components/layout/sidebar/SidebarItem');

var _SidebarItem2 = _interopRequireDefault(_SidebarItem);

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

var BuildingSidebarItem = (_temp = _class = function (_Component) {
  (0, _inherits3.default)(BuildingSidebarItem, _Component);

  function BuildingSidebarItem() {
    (0, _classCallCheck3.default)(this, BuildingSidebarItem);
    return (0, _possibleConstructorReturn3.default)(this, (BuildingSidebarItem.__proto__ || Object.getPrototypeOf(BuildingSidebarItem)).apply(this, arguments));
  }

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

      if (sidebarOpen) {
        var dragHandle =
        // This wrapper div is necessary for drag and drop
        _react2.default.createElement(
          'div',
          null,
          _react2.default.createElement(_DragHandle2.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 _props2 = this.props,
          draggingOver = _props2.draggingOver,
          draggingAboveHalf = _props2.draggingAboveHalf,
          inStimulus = _props2.inStimulus,
          sidebarOpen = _props2.sidebarOpen;


      var propsForDnDZone = {
        inSidebar: true,
        inStimulus: inStimulus,
        sidebarOpen: sidebarOpen,
        draggingOver: draggingOver,
        draggingAboveHalf: draggingAboveHalf
      };
      return _react2.default.createElement(
        'div',
        null,
        _react2.default.createElement(_DragAndDropZone2.default, Object.assign({}, propsForDnDZone, { location: 'above' })),
        _react2.default.createElement(
          _SidebarItem2.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()
        ),
        _react2.default.createElement(_DragAndDropZone2.default, Object.assign({}, propsForDnDZone, { location: 'below' }))
      );
    }
  }, {
    key: 'render',
    value: function render() {
      var _props3 = this.props,
          connectDragPreview = _props3.connectDragPreview,
          connectDragSource = _props3.connectDragSource,
          connectDropTarget = _props3.connectDropTarget,
          sidebarOpen = _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), _class.propTypes = {
  connectDragSource: _propTypes2.default.func.isRequired, // inserted by React DnD
  connectDragPreview: _propTypes2.default.func.isRequired, // inserted by React DnD
  connectDropTarget: _propTypes2.default.func.isRequired, // inserted by React DnD
  disableDraggingOverride: _propTypes2.default.bool,
  draggingInSidebar: _propTypes2.default.bool.isRequired,
  // Passed in by the Sidebar
  displayPosition: _propTypes2.default.number.isRequired,
  inStimulus: _propTypes2.default.bool,
  itemBody: _propTypes2.default.string,
  itemId: _propTypes2.default.string.isRequired,
  itemName: _propTypes2.default.string.isRequired,
  pointsPossible: _propTypes2.default.number.isRequired,
  quizEntry: _reactImmutableProptypes2.default.record.isRequired,
  quizEntries: _reactImmutableProptypes2.default.listOf(_reactImmutableProptypes2.default.record).isRequired,
  sidebarOpen: _propTypes2.default.bool.isRequired,
  // Passed in by connect
  draggingOver: _propTypes2.default.bool.isRequired,
  draggingAboveHalf: _propTypes2.default.bool.isRequired
}, _class.defaultProps = {
  disableDraggingOverride: false,
  inStimulus: false,
  itemBody: ''
}, _temp);
exports.default = BuildingSidebarItem;