import { useState as i, useRef as c, useEffect as d } from "react";
function R({
  value: e,
  options: o = { leading: !1 },
  wait: s,
  fn: t
}) {
  const [m, l] = i(e), f = c(!1), a = c(null), n = c(!1), [g, r] = i(!1), u = () => window.clearTimeout(a.current);
  return d(() => {
    f.current && (!n.current && o.leading ? (r(!0), n.current = !0, t && t(), l(e), r(!1)) : (u(), r(!0), a.current = window.setTimeout(() => {
      n.current = !1, t && t(), l(e), r(!1);
    }, s)));
  }, [e, o.leading, s]), d(() => (f.current = !0, u), []), { _value: m, cancel: u, loading: g };
}
export {
  R as useDebouncedValue
};
