import * as i from "react";
import { createContextScope as C } from "../../../../../@radix-ui_react-context@1.1.2_@types_react@19.1.6_react@19.1.0/node_modules/@radix-ui/react-context/dist/index.js";
import { useCallbackRef as h } from "../../../../../@radix-ui_react-use-callback-ref@1.1.1_@types_react@19.1.6_react@19.1.0/node_modules/@radix-ui/react-use-callback-ref/dist/index.js";
import { useLayoutEffect as f } from "../../../../../@radix-ui_react-use-layout-effect@1.1.1_@types_react@19.1.6_react@19.1.0/node_modules/@radix-ui/react-use-layout-effect/dist/index.js";
import { Primitive as v } from "../../../../../@radix-ui_react-primitive@2.1.3_@types_react-dom@19.1.6_@types_react@19.1.6__@types_rea_ec571b14c8dc7e1c1fa1383d9a1e10bd/node_modules/@radix-ui/react-primitive/dist/index.js";
import { useIsHydrated as _ } from "../../../../../@radix-ui_react-use-is-hydrated@0.1.0_@types_react@19.1.6_react@19.1.0/node_modules/@radix-ui/react-use-is-hydrated/dist/index.js";
import { jsx as l } from "react/jsx-runtime";
var m = "Avatar", [y, H] = C(m), [x, A] = y(m), L = i.forwardRef(
  (t, e) => {
    const { __scopeAvatar: o, ...r } = t, [n, a] = i.useState("idle");
    return /* @__PURE__ */ l(
      x,
      {
        scope: o,
        imageLoadingStatus: n,
        onImageLoadingStatusChange: a,
        children: /* @__PURE__ */ l(v.span, { ...r, ref: e })
      }
    );
  }
);
L.displayName = m;
var S = "AvatarImage", E = i.forwardRef(
  (t, e) => {
    const { __scopeAvatar: o, src: r, onLoadingStatusChange: n = () => {
    }, ...a } = t, u = A(S, o), s = N(r, a), d = h((c) => {
      n(c), u.onImageLoadingStatusChange(c);
    });
    return f(() => {
      s !== "idle" && d(s);
    }, [s, d]), s === "loaded" ? /* @__PURE__ */ l(v.img, { ...a, ref: e, src: r }) : null;
  }
);
E.displayName = S;
var I = "AvatarFallback", R = i.forwardRef(
  (t, e) => {
    const { __scopeAvatar: o, delayMs: r, ...n } = t, a = A(I, o), [u, s] = i.useState(r === void 0);
    return i.useEffect(() => {
      if (r !== void 0) {
        const d = window.setTimeout(() => s(!0), r);
        return () => window.clearTimeout(d);
      }
    }, [r]), u && a.imageLoadingStatus !== "loaded" ? /* @__PURE__ */ l(v.span, { ...n, ref: e }) : null;
  }
);
R.displayName = I;
function p(t, e) {
  return t ? e ? (t.src !== e && (t.src = e), t.complete && t.naturalWidth > 0 ? "loaded" : "loading") : "error" : "idle";
}
function N(t, { referrerPolicy: e, crossOrigin: o }) {
  const r = _(), n = i.useRef(null), a = r ? (n.current || (n.current = new window.Image()), n.current) : null, [u, s] = i.useState(
    () => p(a, t)
  );
  return f(() => {
    s(p(a, t));
  }, [a, t]), f(() => {
    const d = (w) => () => {
      s(w);
    };
    if (!a) return;
    const c = d("loaded"), g = d("error");
    return a.addEventListener("load", c), a.addEventListener("error", g), e && (a.referrerPolicy = e), typeof o == "string" && (a.crossOrigin = o), () => {
      a.removeEventListener("load", c), a.removeEventListener("error", g);
    };
  }, [a, o, e]), u;
}
var j = L, B = E, G = R;
export {
  L as Avatar,
  R as AvatarFallback,
  E as AvatarImage,
  G as Fallback,
  B as Image,
  j as Root,
  H as createAvatarScope
};
//# sourceMappingURL=index.js.map
