"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAnimatedListHeight = useAnimatedListHeight;
const react_1 = require("react");
function useAnimatedListHeight() {
    const [contentVisible, setContentVisible] = (0, react_1.useState)(false);
    const [showSkeleton, setShowSkeleton] = (0, react_1.useState)(false);
    const [frozenH, setFrozenH] = (0, react_1.useState)(null);
    const [contentMaxH, setContentMaxH] = (0, react_1.useState)(0);
    const [relaxMaxH, setRelaxMaxH] = (0, react_1.useState)(false);
    const contentRef = (0, react_1.useRef)(null);
    const roRef = (0, react_1.useRef)(null);
    const stableTimerRef = (0, react_1.useRef)(null);
    // Generation counter — tăng mỗi lần reset để invalidate callback cũ
    const genRef = (0, react_1.useRef)(0);
    const cleanup = (0, react_1.useCallback)(() => {
        var _a;
        (_a = roRef.current) === null || _a === void 0 ? void 0 : _a.disconnect();
        roRef.current = null;
        if (stableTimerRef.current) {
            window.clearTimeout(stableTimerRef.current);
            stableTimerRef.current = null;
        }
    }, []);
    const resetForInitLoad = (0, react_1.useCallback)(() => {
        var _a, _b, _c;
        // Invalidate tất cả callback đang pending từ cycle trước
        const gen = ++genRef.current;
        const currentH = ((_a = contentRef.current) === null || _a === void 0 ? void 0 : _a.scrollHeight) || ((_b = contentRef.current) === null || _b === void 0 ? void 0 : _b.getBoundingClientRect().height) || 0;
        if (currentH)
            setFrozenH(currentH);
        setRelaxMaxH(false);
        setContentVisible(false);
        setShowSkeleton(true);
        (_c = roRef.current) === null || _c === void 0 ? void 0 : _c.disconnect();
        roRef.current = null;
        if (stableTimerRef.current) {
            window.clearTimeout(stableTimerRef.current);
            stableTimerRef.current = null;
        }
        return gen; // expose nếu caller cần
    }, []);
    const attachResizeObserver = (0, react_1.useCallback)((gen) => {
        var _a, _b, _c;
        (_a = roRef.current) === null || _a === void 0 ? void 0 : _a.disconnect();
        roRef.current = null;
        roRef.current = new ResizeObserver(() => {
            var _a, _b;
            if (genRef.current !== gen)
                return; // stale
            const h = (_b = (_a = contentRef.current) === null || _a === void 0 ? void 0 : _a.scrollHeight) !== null && _b !== void 0 ? _b : 0;
            setContentMaxH(h);
            if (stableTimerRef.current)
                window.clearTimeout(stableTimerRef.current);
            stableTimerRef.current = window.setTimeout(() => {
                var _a;
                if (genRef.current !== gen)
                    return; // stale
                setRelaxMaxH(true);
                (_a = roRef.current) === null || _a === void 0 ? void 0 : _a.disconnect();
                roRef.current = null;
                stableTimerRef.current = null;
            }, 180);
        });
        if (contentRef.current)
            roRef.current.observe(contentRef.current);
        const h0 = (_c = (_b = contentRef.current) === null || _b === void 0 ? void 0 : _b.scrollHeight) !== null && _c !== void 0 ? _c : 0;
        setContentMaxH(h0);
    }, []);
    const revealAfterInitLoad = (0, react_1.useCallback)(() => {
        const gen = genRef.current;
        setShowSkeleton(false);
        requestAnimationFrame(() => {
            if (genRef.current !== gen)
                return;
            setContentVisible(true);
            setFrozenH(null);
            attachResizeObserver(gen);
        });
    }, [attachResizeObserver]);
    const getMaxHeight = (0, react_1.useCallback)(() => {
        var _a;
        if (relaxMaxH)
            return 'none';
        if (!contentVisible)
            return (_a = frozenH !== null && frozenH !== void 0 ? frozenH : contentMaxH) !== null && _a !== void 0 ? _a : 0;
        if (contentMaxH === 0)
            return 'none';
        return contentMaxH;
    }, [relaxMaxH, contentVisible, frozenH, contentMaxH]);
    const getHeightPx = (0, react_1.useCallback)(() => { var _a; return (_a = frozenH !== null && frozenH !== void 0 ? frozenH : contentMaxH) !== null && _a !== void 0 ? _a : 0; }, [frozenH, contentMaxH]);
    return {
        contentRef,
        contentVisible,
        showSkeleton,
        relaxMaxH,
        contentMaxH,
        frozenH,
        getMaxHeight,
        getHeightPx,
        resetForInitLoad,
        revealAfterInitLoad,
        cleanup,
        setRelaxMaxH,
    };
}
//# sourceMappingURL=useAnimatedListHeight.js.map