"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.useColumnResizeGuideLine = useColumnResizeGuideLine;
const React = __importStar(require("react"));
/**
 * 列宽拖拽参考线 Hook
 *
 * 将原本散落在 NormalTableView 中的 document 级 mousedown/mousemove/mouseup
 * 副作用隔离为独立 hook，降低与表格组件的耦合度。
 *
 * @param containerRef 表格滚动容器 ref
 * @returns guideLineRef — 挂载到参考线 DOM 元素上
 */
function useColumnResizeGuideLine(containerRef) {
    const guideLineRef = React.useRef(null);
    React.useEffect(() => {
        const container = containerRef.current;
        if (!container)
            return;
        let rafId = null;
        let startX = 0;
        let headerRightInViewport = 0;
        let cachedClientWidth = 0;
        let targetLeft = 0;
        const flushGuidePosition = () => {
            rafId = null;
            const guideEl = guideLineRef.current;
            if (guideEl) {
                guideEl.style.transform = `translateX(${targetLeft}px)`;
            }
        };
        const handleMouseMove = (e) => {
            const dx = e.clientX - startX;
            const { scrollLeft } = container;
            let left = headerRightInViewport + dx + scrollLeft;
            left = Math.max(scrollLeft, Math.min(scrollLeft + cachedClientWidth, left));
            targetLeft = left;
            if (rafId === null) {
                rafId = requestAnimationFrame(flushGuidePosition);
            }
        };
        const handleMouseUp = () => {
            if (rafId !== null) {
                cancelAnimationFrame(rafId);
                rafId = null;
            }
            const guideEl = guideLineRef.current;
            if (guideEl) {
                guideEl.style.display = 'none';
            }
            document.removeEventListener('mousemove', handleMouseMove);
            document.removeEventListener('mouseup', handleMouseUp);
        };
        const handleMouseDown = (e) => {
            const target = e.target;
            const resizeHandle = target.closest('.ai-table-column-resize-handle');
            if (!resizeHandle || !container.contains(resizeHandle))
                return;
            const th = resizeHandle.closest('th');
            if (!th)
                return;
            const containerRect = container.getBoundingClientRect();
            const thRect = th.getBoundingClientRect();
            startX = e.clientX;
            headerRightInViewport = thRect.right - containerRect.left;
            cachedClientWidth = container.clientWidth;
            targetLeft = headerRightInViewport + container.scrollLeft;
            const guideEl = guideLineRef.current;
            if (guideEl) {
                guideEl.style.display = 'block';
                guideEl.style.transform = `translateX(${targetLeft}px)`;
            }
            document.addEventListener('mousemove', handleMouseMove);
            document.addEventListener('mouseup', handleMouseUp);
        };
        // Capture phase: detect mousedown before stopPropagation in the resize handle
        document.addEventListener('mousedown', handleMouseDown, true);
        return () => {
            document.removeEventListener('mousedown', handleMouseDown, true);
            document.removeEventListener('mousemove', handleMouseMove);
            document.removeEventListener('mouseup', handleMouseUp);
            if (rafId !== null) {
                cancelAnimationFrame(rafId);
            }
        };
    }, [containerRef]);
    return guideLineRef;
}
