import { useEffect } from 'react';
import { useFirstMountState } from '../useFirstMountState/index.js';
import { useMountEffect } from '../useMountEffect/index.js';
import { useRafState } from '../useRafState/index.js';
import { isBrowser } from '../util/const.js';
const listeners = new Set();
const callAllListeners = () => {
    for (const l of listeners) {
        l({
            width: window.innerWidth,
            height: window.innerHeight,
        });
    }
};
export function useWindowSize(stateHook = useRafState, measureOnMount) {
    const isFirstMount = useFirstMountState();
    const [size, setSize] = stateHook({
        width: isFirstMount && isBrowser && !measureOnMount ? window.innerWidth : 0,
        height: isFirstMount && isBrowser && !measureOnMount ? window.innerHeight : 0,
    });
    useMountEffect(() => {
        if (measureOnMount) {
            setSize({
                width: window.innerWidth,
                height: window.innerHeight,
            });
        }
    });
    useEffect(() => {
        if (listeners.size === 0) {
            window.addEventListener('resize', callAllListeners, { passive: true });
        }
        listeners.add(setSize);
        return () => {
            listeners.delete(setSize);
            if (listeners.size === 0) {
                window.removeEventListener('resize', callAllListeners);
            }
        };
    }, [setSize]);
    return size;
}
