"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useScrollPreservation = useScrollPreservation;
const react_1 = require("react");
/**
 * Hook to preserve scroll position when older messages are loaded at the top
 * This prevents the chat from jumping when new content is prepended
 * @param {RefObject<T>} containerRef - Reference to the scrollable container element
 * @param {number} messagesCount - Current count of messages in the list
 * @returns {void}
 */
function useScrollPreservation(containerRef, messagesCount) {
    const prevScrollHeight = (0, react_1.useRef)(0);
    const prevMessagesCount = (0, react_1.useRef)(messagesCount);
    (0, react_1.useLayoutEffect)(() => {
        const container = containerRef.current;
        if (!container)
            return;
        const currentScrollHeight = container.scrollHeight;
        // Only adjust scroll if messages were added at the top (older messages loaded)
        if (prevScrollHeight.current > 0 &&
            messagesCount > prevMessagesCount.current &&
            currentScrollHeight > prevScrollHeight.current) {
            // Calculate the height difference and adjust scroll position
            const heightDiff = currentScrollHeight - prevScrollHeight.current;
            container.scrollTop += heightDiff;
        }
        prevScrollHeight.current = currentScrollHeight;
        prevMessagesCount.current = messagesCount;
    }, [containerRef, messagesCount]);
}
//# sourceMappingURL=useScrollPreservation.js.map
