import { useState } from 'react';
import { useEventListener } from '../useEventListener/index.js';
import { useMountEffect } from '../useMountEffect/index.js';
import { isBrowser } from '../util/const.js';
const isDocumentVisible = () => document.visibilityState === 'visible';
export function useDocumentVisibility(initializeWithValue = true) {
    const [isVisible, setIsVisible] = useState(isBrowser && initializeWithValue ? isDocumentVisible() : undefined);
    useMountEffect(() => {
        if (!initializeWithValue) {
            setIsVisible(isDocumentVisible());
        }
    });
    useEventListener(isBrowser ? document : null, 'visibilitychange', () => {
        setIsVisible(isDocumentVisible());
    });
    return isVisible;
}
