import { useState as f, useRef as D, useCallback as E, useMemo as S, useEffect as T } from "react";
import { noop as A } from "es-toolkit/compat";
const F = (s, t) => (r, o) => {
  const e = new Image();
  return o && (e.crossOrigin = "anonymous"), e.onload = s, e.onerror = t, e.src = r, e;
}, l = (s) => {
  const { src: t, isAnonymous: r = !1, onLoaded: o = A, onFailed: e = A, children: m } = s, [L, c] = f("PENDING"), [I, N] = f(null), u = D(null), a = D(!1), i = E(() => {
    const n = u.current;
    a.current || !n || (c("LOADED"), o(n));
  }, [o]), d = E(() => {
    const n = u.current;
    a.current || !n || (c("FAILED"), e(n));
  }, [e]), g = S(
    () => F(i, d),
    [i, d]
  );
  return T(() => {
    a.current = !1, c("PENDING");
    const n = g(t, r);
    return u.current = n, N(n), () => {
      a.current = !0;
    };
  }, [t, r, g]), m && I ? m({ status: L, image: I }) : null;
};
l.STATUS_PENDING = "PENDING";
l.STATUS_LOADED = "LOADED";
l.STATUS_FAILED = "FAILED";
export {
  l as default
};
//# sourceMappingURL=ImagePreloader.js.map
