'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 _Stimulus = require('../../../../../common/components/layout/sidebar/Stimulus');

var _Stimulus2 = _interopRequireDefault(_Stimulus);

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

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

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

  (0, _createClass3.default)(BuildingStimulus, [{
    key: 'shouldDisableDragHandle',
    value: function shouldDisableDragHandle() {
      var _props = this.props,
          childEntries = _props.childEntries,
          quizEntries = _props.quizEntries;

      return quizEntries.size === (childEntries && childEntries.size || 0) + 1;
    }
  }, {
    key: 'renderDragHandle',
    value: function renderDragHandle() {
      var _props2 = this.props,
          connectDragSource = _props2.connectDragSource,
          sidebarOpen = _props2.sidebarOpen;

      if (sidebarOpen) {
        var dragHandle =
        // This wrapper div is necessary for drag and drop
        _react2.default.createElement(
          'div',
          null,
          _react2.default.createElement(_DragHandle2.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 _react2.default.createElement(
        _Stimulus2.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 _react2.default.createElement(
        'div',
        null,
        _react2.default.createElement(_DragAndDropZone2.default, Object.assign({}, propsForDnDZone, { location: 'above' })),
        this.renderStimulus(),
        _react2.default.createElement(_DragAndDropZone2.default, Object.assign({}, propsForDnDZone, { location: 'in' })),
        _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.renderStimulusWrapper()));
      }
      return this.renderStimulusWrapper();
    }
  }]);
  BuildingStimulus.displayName = 'BuildingStimulus'
  ;
  return BuildingStimulus;
}(_react.Component), _class.propTypes = {
  childEntries: _reactImmutableProptypes2.default.listOf(_reactImmutableProptypes2.default.record).isRequired,
  connectDragPreview: _propTypes2.default.func.isRequired, // inserted by React DnD
  connectDragSource: _propTypes2.default.func.isRequired, // inserted by React DnD
  connectDropTarget: _propTypes2.default.func.isRequired, // inserted by React DnD
  isDragging: _propTypes2.default.bool.isRequired, // inserted by React DnD
  // Passed in by the Sidebar
  displayPosition: _propTypes2.default.number.isRequired,
  isOver: _propTypes2.default.bool,
  itemId: _propTypes2.default.string.isRequired,
  quizEntries: _reactImmutableProptypes2.default.listOf(_reactImmutableProptypes2.default.record).isRequired,
  quizEntry: _reactImmutableProptypes2.default.record.isRequired,
  renderSidebarItem: _propTypes2.default.func.isRequired,
  sidebarOpen: _propTypes2.default.bool.isRequired,
  // Passed in by connect
  draggingAboveHalf: _propTypes2.default.bool.isRequired,
  draggingOver: _propTypes2.default.bool.isRequired,
  dropIntoEmptyStimulus: _propTypes2.default.bool
}, _class.defaultProps = {
  draggingAboveHalf: false,
  dropIntoEmptyStimulus: false,
  isOver: false
}, _temp);
exports.default = BuildingStimulus;