import _extends from "babel-runtime/helpers/extends";
import _classCallCheck from "babel-runtime/helpers/classCallCheck";
import _createClass from "babel-runtime/helpers/createClass";
import _possibleConstructorReturn from "babel-runtime/helpers/possibleConstructorReturn";
import _inherits from "babel-runtime/helpers/inherits";
var __rest = this && this.__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;
};
import React, { Component } from "react";
import { DragSource, DropTarget } from 'react-dnd';
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) {
    _inherits(BodyRow, _Component);

    function BodyRow() {
        _classCallCheck(this, BodyRow);

        return _possibleConstructorReturn(this, (BodyRow.__proto__ || Object.getPrototypeOf(BodyRow)).apply(this, arguments));
    }

    _createClass(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 = _extends(_extends({}, 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(React.createElement("tr", _extends({}, restProps, { className: className, style: styleObj }))));
        }
    }]);

    return BodyRow;
}(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;
    }
};
export var DragableBodyRow = DropTarget('row', rowTarget, function (connect, monitor) {
    return {
        connectDropTarget: connect.dropTarget(),
        isOver: monitor.isOver(),
        sourceClientOffset: monitor.getSourceClientOffset()
    };
})(DragSource('row', rowSource, function (connect, monitor) {
    return {
        connectDragSource: connect.dragSource(),
        dragRow: monitor.getItem(),
        clientOffset: monitor.getClientOffset(),
        initialClientOffset: monitor.getInitialClientOffset()
    };
})(BodyRow));