"use strict";

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

var _extends2 = require("babel-runtime/helpers/extends");

var _extends3 = _interopRequireDefault(_extends2);

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 _reactDnd = require("react-dnd");

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

var __rest = undefined && undefined.__rest || function (s, e) {
    var t = {};
    for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
    }if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
        if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
    }
    return t;
};

function dragDirection(dragIndex, hoverIndex, initialClientOffset, clientOffset, sourceClientOffset) {
    var hoverMiddleY = (initialClientOffset.y - sourceClientOffset.y) / 2;
    var hoverClientY = clientOffset.y - sourceClientOffset.y;
    if (dragIndex < hoverIndex && hoverClientY > hoverMiddleY) {
        return 'downward';
    }
    if (dragIndex > hoverIndex && hoverClientY < hoverMiddleY) {
        return 'upward';
    }
}
/* eslint-disable */

var BodyRow = function (_Component) {
    (0, _inherits3["default"])(BodyRow, _Component);

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

    (0, _createClass3["default"])(BodyRow, [{
        key: "render",
        value: function render() {
            var _a = this.props,
                isOver = _a.isOver,
                connectDragSource = _a.connectDragSource,
                connectDropTarget = _a.connectDropTarget,
                moveRow = _a.moveRow,
                dragRow = _a.dragRow,
                clientOffset = _a.clientOffset,
                sourceClientOffset = _a.sourceClientOffset,
                initialClientOffset = _a.initialClientOffset,
                style = _a.style,
                index = _a.index,
                restProps = __rest(_a, ["isOver", "connectDragSource", "connectDropTarget", "moveRow", "dragRow", "clientOffset", "sourceClientOffset", "initialClientOffset", "style", "index"]);
            var styleObj = (0, _extends3["default"])((0, _extends3["default"])({}, style), { cursor: 'move' });
            var className = restProps.className;

            if (isOver && initialClientOffset) {
                var direction = dragDirection(dragRow.index, index, initialClientOffset, clientOffset, sourceClientOffset);
                if (direction === 'downward') {
                    className += ' drop-over-downward';
                }
                if (direction === 'upward') {
                    className += ' drop-over-upward';
                }
            }
            return connectDragSource(connectDropTarget(_react2["default"].createElement("tr", (0, _extends3["default"])({}, restProps, { className: className, style: styleObj }))));
        }
    }]);
    return BodyRow;
}(_react.Component);

var rowSource = {
    beginDrag: function beginDrag(props) {
        return {
            index: props.index
        };
    }
};
var rowTarget = {
    drop: function drop(props, monitor) {
        var dragIndex = monitor.getItem().index;
        var hoverIndex = props.index;
        // Don't replace items with themselves
        if (dragIndex === hoverIndex) {
            return;
        }
        // Time to actually perform the action
        props.moveRow(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 DragableBodyRow = exports.DragableBodyRow = (0, _reactDnd.DropTarget)('row', rowTarget, function (connect, monitor) {
    return {
        connectDropTarget: connect.dropTarget(),
        isOver: monitor.isOver(),
        sourceClientOffset: monitor.getSourceClientOffset()
    };
})((0, _reactDnd.DragSource)('row', rowSource, function (connect, monitor) {
    return {
        connectDragSource: connect.dragSource(),
        dragRow: monitor.getItem(),
        clientOffset: monitor.getClientOffset(),
        initialClientOffset: monitor.getInitialClientOffset()
    };
})(BodyRow));