import { useEffect, useState } from 'react';

export function useDelayResetState<T>(
  theValue: T,
  startDelay: number,
  delay: number
) {
  const [value, setValue] = useState<T | null>(null);
  useEffect(() => {
    const handler1 = setTimeout(() => {
      setValue(theValue);
    }, startDelay);

    const handler2 = setTimeout(() => {
      setValue(null);
    }, startDelay + delay);
    return () => {
      clearTimeout(handler1);
      clearTimeout(handler2);
    };
  }, [setValue, theValue, delay, startDelay]);

  return value;
}
