import { o as _ } from "./rolldown-runtime.js";
import { i as y, t as I } from "./vendor/react.js";
import { i as E, s as N, t as g } from "./useRenderElement.js";
import { n as O, t as b } from "./useIsoLayoutEffect.js";
import { a as P, n as j, t as k } from "./useTransitionStatus.js";
import { n as q } from "./useTimeout.js";
var r = /* @__PURE__ */ _(y()), x = /* @__PURE__ */ r.createContext(void 0);
function C() {
  const o = r.useContext(x);
  if (o === void 0) throw new Error(N(13));
  return o;
}
var p = { imageLoadingStatus: () => null }, F = I(), J = /* @__PURE__ */ r.forwardRef(function(t, i) {
  const { className: c, render: l, style: s, ...e } = t, [n, u] = r.useState("idle"), d = { imageLoadingStatus: n }, m = r.useMemo(() => ({
    imageLoadingStatus: n,
    setImageLoadingStatus: u
  }), [n, u]), a = g("span", t, {
    state: d,
    ref: i,
    props: e,
    stateAttributesMapping: p
  });
  return /* @__PURE__ */ (0, F.jsx)(x.Provider, {
    value: m,
    children: a
  });
});
function T(o, { referrerPolicy: t, crossOrigin: i }) {
  const [c, l] = r.useState("idle");
  return b(() => {
    if (!o)
      return l("error"), E;
    let s = !0;
    const e = new window.Image(), n = (u) => () => {
      s && l(u);
    };
    return l("loading"), e.onload = n("loaded"), e.onerror = n("error"), t && (e.referrerPolicy = t), e.crossOrigin = i ?? null, e.src = o, e.complete && l(e.naturalWidth > 0 ? "loaded" : "error"), () => {
      s = !1;
    };
  }, [
    o,
    i,
    t
  ]), c;
}
var V = {
  ...p,
  ...P
}, K = /* @__PURE__ */ r.forwardRef(function(t, i) {
  const { className: c, render: l, onLoadingStatusChange: s, referrerPolicy: e, crossOrigin: n, style: u, ...d } = t, m = C(), a = T(t.src, {
    referrerPolicy: e,
    crossOrigin: n
  }), f = a === "loaded", { mounted: v, transitionStatus: M, setMounted: R } = k(f), S = r.useRef(null), A = O((L) => {
    s?.(L), m.setImageLoadingStatus(L);
  });
  b(() => {
    a !== "idle" && A(a);
  }, [a, A]);
  const h = {
    imageLoadingStatus: a,
    transitionStatus: M
  };
  j({
    open: f,
    ref: S,
    onComplete() {
      f || R(!1);
    }
  });
  const w = g("img", t, {
    state: h,
    ref: [i, S],
    props: d,
    stateAttributesMapping: V,
    enabled: v
  });
  return v ? w : null;
}), Q = /* @__PURE__ */ r.forwardRef(function(t, i) {
  const { className: c, render: l, delay: s, style: e, ...n } = t, { imageLoadingStatus: u } = C(), [d, m] = r.useState(s === void 0), a = q();
  return r.useEffect(() => (s !== void 0 && a.start(s, () => m(!0)), a.clear), [a, s]), g("span", t, {
    state: { imageLoadingStatus: u },
    ref: i,
    props: n,
    stateAttributesMapping: p,
    enabled: u !== "loaded" && d
  });
});
export {
  K as n,
  J as r,
  Q as t
};

//# sourceMappingURL=AvatarFallback.js.map