"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useScrollOnDrag = void 0;
var react_1 = require("react");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var SCROLL_MARGIN = 50;
var SPEED_MULTIPLIER = 25;
var UPDATE_DATA_INTERVAL = 100;
var useScrollOnDrag = function (scrollContainer) {
    var scrolling = (0, react_1.useRef)(false);
    var running = (0, react_1.useRef)(false);
    var scrollData = (0, react_1.useRef)(null);
    var getIsScrolling = function () {
        return scrolling === null || scrolling === void 0 ? void 0 : scrolling.current;
    };
    var onStartDragging = function () {
        var container = scrollContainer === null || scrollContainer === void 0 ? void 0 : scrollContainer.current;
        if (container && typeof container.addEventListener === 'function') {
            container.addEventListener('dragover', updateScrollData);
            window.addEventListener('dragover', updateScrollData);
            running.current = true;
        }
    };
    var updateScrollData = (0, react_1.useCallback)((0, mdm_sdk_1.throttle)(function (event) {
        var container = scrollContainer === null || scrollContainer === void 0 ? void 0 : scrollContainer.current;
        if (!container || !running.current)
            return;
        var holder = container.getElementsByClassName('rc-tree-list-holder')[0];
        var calcSpeed = function () {
            var clientY = event.clientY, clientX = event.clientX;
            var _a = container.getBoundingClientRect(), top = _a.top, bottom = _a.bottom, left = _a.left, right = _a.right;
            var maxScroll = holder.scrollHeight - holder.clientHeight;
            var insideContainer = clientY > top && clientY < bottom && clientX >= left && clientX <= right;
            if (insideContainer) {
                if (clientY - top < SCROLL_MARGIN && holder.scrollTop > 0) {
                    return (clientY - top - SCROLL_MARGIN) / SCROLL_MARGIN;
                }
                else if (bottom - clientY < SCROLL_MARGIN && holder.scrollTop < maxScroll) {
                    return -(bottom - clientY - SCROLL_MARGIN) / SCROLL_MARGIN;
                }
            }
            return 0;
        };
        if (holder) {
            scrollData.current = calcSpeed();
            if (scrollData.current) {
                runScrolling(holder);
            }
        }
    }, UPDATE_DATA_INTERVAL), []);
    var removeListeners = (0, react_1.useCallback)(function () {
        var container = scrollContainer === null || scrollContainer === void 0 ? void 0 : scrollContainer.current;
        if (container && typeof container.removeEventListener === 'function') {
            container.removeEventListener('dragover', updateScrollData);
        }
        window.removeEventListener('dragover', updateScrollData);
    }, [scrollContainer, updateScrollData]);
    var onEndDragging = (0, react_1.useCallback)(function () {
        scrolling.current = false;
        scrollData.current = null;
        running.current = false;
        removeListeners();
    }, [removeListeners]);
    var runScrolling = function (holder) {
        if (scrolling === null || scrolling === void 0 ? void 0 : scrolling.current) {
            return;
        }
        scrolling.current = true;
        var scroll = function () {
            if (scrolling.current && scrollData.current) {
                holder.scrollTop += scrollData.current * SPEED_MULTIPLIER;
                requestAnimationFrame(scroll);
            }
            else {
                scrolling.current = false;
            }
        };
        scroll();
    };
    (0, react_1.useEffect)(function () {
        return function () {
            removeListeners();
        };
    }, [removeListeners]);
    return { onStartDragging: onStartDragging, onEndDragging: onEndDragging, getIsScrolling: getIsScrolling };
};
exports.useScrollOnDrag = useScrollOnDrag;
