"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useStickyRights = exports.useStickyLefts = void 0;
const react_1 = require("react");
const useStickyLefts = (count, dataTable) => {
    const refs = (0, react_1.useRef)([]);
    const [lefts, setLefts] = (0, react_1.useState)([]);
    (0, react_1.useEffect)(() => {
        const calculateLefts = () => {
            var _a;
            const newLefts = [];
            let acc = 0;
            for (let i = 0; i < count; i++) {
                const width = ((_a = refs.current[i]) === null || _a === void 0 ? void 0 : _a.offsetWidth) || 0;
                newLefts[i] = acc;
                acc += width;
            }
            setLefts(newLefts);
        };
        // Initial calculate
        calculateLefts();
        // ResizeObserver để lắng nghe thay đổi kích thước từng ref
        const observers = [];
        refs.current.slice(0, count).forEach((cell) => {
            if (!cell)
                return;
            const observer = new ResizeObserver(() => {
                calculateLefts();
            });
            observer.observe(cell);
            observers.push(observer);
        });
        return () => {
            observers.forEach((observer) => observer.disconnect());
        };
    }, [count, dataTable]);
    return { refs, lefts };
};
exports.useStickyLefts = useStickyLefts;
const useStickyRights = (count, dataTable) => {
    const refs = (0, react_1.useRef)([]);
    const [rights, setRights] = (0, react_1.useState)([]);
    (0, react_1.useEffect)(() => {
        const calculateRights = () => {
            var _a;
            const newRights = [];
            let acc = 0;
            for (let i = 0; i < count; i++) {
                const width = ((_a = refs.current[i]) === null || _a === void 0 ? void 0 : _a.offsetWidth) || 0;
                newRights[i] = acc;
                acc += width;
            }
            setRights(newRights);
        };
        calculateRights();
        const observers = [];
        refs.current.slice(-count).forEach((cell) => {
            if (!cell)
                return;
            const observer = new ResizeObserver(() => calculateRights());
            observer.observe(cell);
            observers.push(observer);
        });
        return () => {
            observers.forEach((observer) => observer.disconnect());
        };
    }, [count, dataTable]);
    return { refs, rights };
};
exports.useStickyRights = useStickyRights;
//# sourceMappingURL=useStickyLeft.js.map