import { useRef, useCallback } from 'react';

function useGanttScrollSync() {
    const tableRef = useRef(null);
    const chartRef = useRef(null);
    const isSyncing = useRef(false);
    const handleTableScroll = useCallback(() => {
        if (isSyncing.current)
            return;
        isSyncing.current = true;
        requestAnimationFrame(() => {
            if (tableRef.current && chartRef.current) {
                chartRef.current.scrollTop = tableRef.current.scrollTop;
            }
            isSyncing.current = false;
        });
    }, []);
    const handleChartScroll = useCallback(() => {
        if (isSyncing.current)
            return;
        isSyncing.current = true;
        requestAnimationFrame(() => {
            if (chartRef.current && tableRef.current) {
                tableRef.current.scrollTop = chartRef.current.scrollTop;
            }
            isSyncing.current = false;
        });
    }, []);
    return { tableRef, chartRef, handleTableScroll, handleChartScroll };
}

export { useGanttScrollSync };
