'use strict';

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

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

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 _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

var _reactDnd = require('react-dnd');

var _reactDndHtml5Backend = require('react-dnd-html5-backend');

var _DragItemContainer = require('./styles/DragItemContainer');

var _DragItemContainer2 = _interopRequireDefault(_DragItemContainer);

var _DragContainer = require('../DragContainer');

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

/**
 * Used to wrap an item used in DragGroup. See DragGroup documentation.
 */
var DragGroupItem = function (_React$Component) {
  (0, _inherits3.default)(DragGroupItem, _React$Component);

  function DragGroupItem() {
    var _ref;

    var _temp, _this, _ret;

    (0, _classCallCheck3.default)(this, DragGroupItem);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = DragGroupItem.__proto__ || (0, _getPrototypeOf2.default)(DragGroupItem)).call.apply(_ref, [this].concat(args))), _this), _this.wrapDragHandle = function (dragHandleNode) {
      return (
        /**
         * The element passed to connectDragSource must be a plain
         * React element, not a component. Thus, to do proper
         * vendor prefixing with styling, we assign a class name
         * and target it from DragItemContainer's styles; see
         * styles/DragItemContainer.js
         */
        _this.props.connectDragSource(_react2.default.createElement(
          'div',
          {
            className: 'DragGroupItem--handle',
            style: {
              display: _this.props.canDrag ? 'block' : 'none'
            }
          },
          dragHandleNode
        ))
      );
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(DragGroupItem, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      // Use empty image as a drag preview so browsers don't draw it
      // and we can draw whatever we want on the custom drag layer instead.
      this.props.connectDragPreview((0, _reactDndHtml5Backend.getEmptyImage)(), {
        // IE fallback: specify that we'd rather screenshot the node
        // when it already knows it's being dragged so we can hide it with CSS.
        captureDraggingState: true
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var _props = this.props,
          connectDragSource = _props.connectDragSource,
          connectDragPreview = _props.connectDragPreview,
          isDragging = _props.isDragging,
          children = _props.children,
          wrapDragHandle = this.wrapDragHandle;


      return _react2.default.createElement(
        _DragItemContainer2.default,
        { isDragging: isDragging },
        _react2.default.createElement(
          _DragContainer.DragContext.Provider,
          { value: { isDragging: isDragging, wrapDragHandle: wrapDragHandle } },
          children
        )
      );
    }
  }]);
  return DragGroupItem;
}(_react2.default.Component);

DragGroupItem.propTypes = {
  /**
   * Must be exactly one node. The children will be passed a
   * `wrapDragHandle` prop which MUST be used to wrap the draggable
   * portion of the child content inside the child's render function
   */
  children: _propTypes2.default.node.isRequired,

  /**
   * Called with (fromGroupId, toGroupId) when the item is moved
   * between groups.
   */
  onMove: _propTypes2.default.func.isRequired,

  /**
   * Provided by parent DragGroup
   * Indicates the current parent's group id.
   */
  groupId: _propTypes2.default.func
};


var itemSource = {
  beginDrag: function beginDrag(props, monitor, component) {
    var domNode = _reactDom2.default.findDOMNode(component);
    var clientRect = domNode.getBoundingClientRect();
    return {
      children: props.children,
      dimensions: clientRect
    };
  },
  endDrag: function endDrag(props, monitor, component) {
    if (!monitor.didDrop()) {
      return;
    }

    var item = monitor.getItem();
    var dropResult = monitor.getDropResult();
    if (dropResult.type === 'group') {
      props.onMove(props.groupId, dropResult.groupId);
    }
  }
};

var collect = function collect(connect, monitor) {
  return {
    connectDragSource: connect.dragSource(),
    connectDragPreview: connect.dragPreview(),
    isDragging: monitor.isDragging()
  };
};

exports.default = (0, _reactDnd.DragSource)(function (props) {
  return props.itemType;
}, itemSource, collect)(DragGroupItem);