import { useEffect, useState } from 'react';

export default function useDocumentVisibility() {
  const [documentVisibility, setDocumentVisibility] = useState(document.visibilityState);

  useEffect(() => {
    const visibilitychange = () => {
      setDocumentVisibility(document.visibilityState);
    };

    document.addEventListener('visibilitychange', visibilitychange);
    return () => {
      document.removeEventListener('visibilitychange', visibilitychange);
    };
  }, []);

  return documentVisibility;
}
