'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 _DragContainer = require('../DragContainer');

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

/**
 * Represents an item that can be dragged. Simply render a node inside of it that wraps the drag handle using
 * the `wrapDragHandle` prop automatically passed down by this component.
 *
 * [DragList.Container](/#!/DragContainer) can be used as an out of the box solution, but if a different component is required,
 * you can use `DragList.Item.Consumer`, which uses the React context API.
 */
var DragListItem = function (_React$PureComponent) {
  (0, _inherits3.default)(DragListItem, _React$PureComponent);

  function DragListItem() {
    var _ref;

    var _temp, _this, _ret;

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

    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 = DragListItem.__proto__ || (0, _getPrototypeOf2.default)(DragListItem)).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.
         */
        _this.props.connectDragSource(_react2.default.createElement(
          'div',
          { className: 'DragListItem--handle' },
          dragHandleNode
        ))
      );
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(DragListItem, [{
    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,
          connectDropTarget = _props.connectDropTarget,
          isDragging = _props.isDragging,
          onMove = _props.onMove,
          index = _props.index,
          children = _props.children,
          wrapDragHandle = this.wrapDragHandle;


      return connectDropTarget(_react2.default.createElement(
        'div',
        null,
        _react2.default.createElement(
          _DragContainer.DragContext.Provider,
          { value: { isDragging: isDragging, wrapDragHandle: wrapDragHandle } },
          children
        )
      ));
    }
  }]);
  return DragListItem;
}(_react2.default.PureComponent);

DragListItem.Consumer = _DragContainer.DragContext.Consumer;
DragListItem.propTypes = {
  children: _propTypes2.default.node.isRequired,
  /**
   * DragList.Items that interact with each other should share the same type string value.
   */
  itemType: _propTypes2.default.string.isRequired,
  /**
   * Index of the item. Used for moving items around
   */
  index: _propTypes2.default.number.isRequired,
  /**
   * Called with (fromIndex, toIndex) when the item is moved to a new position
   */
  onMove: _propTypes2.default.func.isRequired
};


var dragSource = {
  beginDrag: function beginDrag(props, monitor, component) {
    var domNode = _reactDom2.default.findDOMNode(component);
    var clientRect = domNode.getBoundingClientRect();
    return {
      index: props.index,
      children: props.children,
      dimensions: clientRect
    };
  }
};

var dropTarget = {
  hover: function hover(props, monitor, component) {
    var dragIndex = monitor.getItem().index;
    var hoverIndex = props.index;

    // Don't replace items with themselves
    if (dragIndex === hoverIndex) {
      return;
    }

    // Determine rectangle on screen
    var hoverBoundingRect = _reactDom2.default.findDOMNode(component).getBoundingClientRect();

    // Get vertical middle
    var hoverMiddleY = (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2;

    // Determine mouse position
    var clientOffset = monitor.getClientOffset();

    // Get pixels to the top
    var hoverClientY = clientOffset.y - hoverBoundingRect.top;

    // Time to actually perform the action
    props.onMove(dragIndex, hoverIndex);

    // Note: we're mutating the monitor item here!
    // Generally it's better to avoid mutations,
    // but it's good here for the sake of performance
    // to avoid expensive index searches.
    monitor.getItem().index = hoverIndex;
  }
};

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

var dropCollect = function dropCollect(connect) {
  return {
    connectDropTarget: connect.dropTarget()
  };
};

exports.default = (0, _reactDnd.DragSource)(function (props) {
  return props.itemType;
}, dragSource, dragCollect)((0, _reactDnd.DropTarget)(function (props) {
  return props.itemType;
}, dropTarget, dropCollect)(DragListItem));