import { useCallback, useEffect, useState } from 'react'

export function useDelay(ms: number): [boolean, () => void] {
  const [delayed, setDelayed] = useState(false)

  useEffect(() => {
    if (delayed) {
      return
    }
    const timeout = setTimeout(() => setDelayed(true), ms)
    return () => clearTimeout(timeout)
  }, [delayed, ms])

  const reset = useCallback(() => setDelayed(false), [setDelayed])

  return [delayed, reset]
}
