/*
 * Copyright (c) 2023-2025 Datalayer, Inc.
 * Distributed under the terms of the Modified BSD License.
 */
import { useState, useEffect } from 'react';
export function useVisibilityObserver(navigationRef, children) {
    const [visibilityMap, setVisibilityMap] = useState({});
    const handleIntersection = (entries) => {
        const updatedEntries = {};
        for (const entry of entries) {
            const navitemid = entry.target.getAttribute('data-navitemid');
            if (navitemid) {
                if (entry.isIntersecting) {
                    updatedEntries[navitemid] = true;
                }
                else {
                    updatedEntries[navitemid] = false;
                }
            }
        }
        setVisibilityMap(prev => ({
            ...prev,
            ...updatedEntries,
        }));
    };
    useEffect(() => {
        const observer = new IntersectionObserver(handleIntersection, {
            root: navigationRef.current,
            threshold: 1,
        });
        if (navigationRef.current) {
            const navItems = Array.from(navigationRef.current.children);
            for (const item of navItems) {
                if (item.getAttribute('data-navitemid')) {
                    observer.observe(item);
                }
            }
        }
        return () => observer.disconnect();
    }, [navigationRef, children]);
    return [visibilityMap];
}
