import { useRef,useState,useMemo, useEffect } from 'react';

const useDelay=()=>{
  const [loading,setLoading] = useState(false);
  const refSet = useRef();
  refSet.current = setLoading;
  useEffect(()=>{
    refSet.current = setLoading;
  },[setLoading]);

  const actions=useMemo(()=>{
    const wait=(ms)=>{
      refSet.current(true);
      return new Promise(res=>{
        setTimeout(()=>{
          refSet.current(false);
          res();
        }, ms);
      })
    }
    return {
      wait,
      loading
    };
  },[loading]);
  return actions;
}

export default useDelay;
