"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useDndBasicTableScrollModifier = void 0;
var react_1 = require("react");
var ramda_1 = require("ramda");
var SCROLL_SPEED = 10;
var TRIGGER_SCROLL_DISTANCE = 150;
var useDndBasicTableScrollModifier = function (_a) {
    var tableRef = _a.tableRef;
    var intervalRef = (0, react_1.useRef)();
    var stopScroll = (0, react_1.useCallback)(function () {
        if (intervalRef.current) {
            clearInterval(intervalRef.current);
        }
    }, []);
    var scrollGrid = (0, react_1.useCallback)(function (x, y) {
        var _a, _b;
        var grid = (_b = (_a = tableRef.current) === null || _a === void 0 ? void 0 : _a.gridOuterRef) === null || _b === void 0 ? void 0 : _b.current;
        if (!grid) {
            return;
        }
        stopScroll();
        intervalRef.current = setInterval(function () {
            grid.scrollBy(x, y);
        }, 10);
    }, [stopScroll, tableRef]);
    var scrollModifier = (0, react_1.useCallback)(function (data) {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
        if (data) {
            var gridRect = (_c = (_b = (_a = tableRef.current) === null || _a === void 0 ? void 0 : _a.gridOuterRef) === null || _b === void 0 ? void 0 : _b.current) === null || _c === void 0 ? void 0 : _c.getBoundingClientRect();
            var diffBottom = (gridRect === null || gridRect === void 0 ? void 0 : gridRect.bottom) - (((_d = data.draggingNodeRect) === null || _d === void 0 ? void 0 : _d.bottom) + ((_e = data.transform) === null || _e === void 0 ? void 0 : _e.y));
            var diffTop = ((_f = data.draggingNodeRect) === null || _f === void 0 ? void 0 : _f.top) + ((_g = data.transform) === null || _g === void 0 ? void 0 : _g.y) - (gridRect === null || gridRect === void 0 ? void 0 : gridRect.top);
            var diffLeft = ((_h = data.draggingNodeRect) === null || _h === void 0 ? void 0 : _h.left) + ((_j = data.transform) === null || _j === void 0 ? void 0 : _j.x) - (gridRect === null || gridRect === void 0 ? void 0 : gridRect.left);
            var diffRight = (gridRect === null || gridRect === void 0 ? void 0 : gridRect.right) - (((_k = data.draggingNodeRect) === null || _k === void 0 ? void 0 : _k.right) + ((_l = data.transform) === null || _l === void 0 ? void 0 : _l.x));
            var scrollYDirection = diffTop < diffBottom ? -1 : 1;
            var scrollY_1 = (0, ramda_1.any)((0, ramda_1.gt)(TRIGGER_SCROLL_DISTANCE), [diffTop, diffBottom])
                ? SCROLL_SPEED * scrollYDirection
                : 0;
            var scrollXDirection = diffLeft < diffRight ? -1 : 1;
            var scrollX_1 = (0, ramda_1.any)((0, ramda_1.gt)(TRIGGER_SCROLL_DISTANCE), [diffLeft, diffRight])
                ? SCROLL_SPEED * scrollXDirection
                : 0;
            if (scrollX_1 || scrollY_1) {
                scrollGrid(scrollX_1, scrollY_1);
            }
            else {
                stopScroll();
            }
        }
        return data.transform;
    }, [scrollGrid, stopScroll, tableRef]);
    return {
        scrollModifier: scrollModifier,
        stopScroll: stopScroll
    };
};
exports.useDndBasicTableScrollModifier = useDndBasicTableScrollModifier;
