import { jsx as t } from "react/jsx-runtime";
import { useRef as v, useState as C, useEffect as y, useMemo as P } from "react";
import { CardType as o } from "./types.js";
import A from "./Cards/avatar-only.js";
import m from "./Cards/index.js";
import D from "./Container/dialog.js";
import S from "./Container/card.js";
import w from "../Avatar/index.js";
import { isUserDid as x, getUserByDid as U } from "./utils.js";
function L(e) {
  if (!e) return null;
  let r = "";
  return e.fullName ? r = e.fullName.charAt(0).toUpperCase() : e.email ? r = e.email.split("@")[0].charAt(0).toUpperCase() : r = e.did ? e.did.charAt(0).toUpperCase() : "?", r;
}
function B(e) {
  const { cardType: r = o.Detailed, showHoverCard: l } = e, u = l !== void 0 ? l : r === o.AvatarOnly, c = v(null), [i, f] = C(() => e.user || null);
  y(() => {
    let a = !0;
    return e.user ? f(e.user) : e.did && x(e.did) && !e.user && U(e.did).then((d) => {
      a && f(d || { fullName: "Anonymous", did: e.did, avatar: "" });
    }), () => {
      a = !1;
    };
  }, [e.did, e.user]);
  const n = P(() => {
    if (!i)
      return null;
    const { avatar: a = "", ...d } = i;
    if (a) {
      const h = a.split("?")[1] || "";
      return {
        ...d,
        avatar: h ? a : `${a}?imageFilter=resize&w=48&h=48`
      };
    }
    return i;
  }, [i]);
  if (!n)
    return /* @__PURE__ */ t(w, { did: e.did, size: e.avatarSize, useProxyFallback: !0, ...e.avatarProps });
  const s = () => /* @__PURE__ */ t(D, { sx: e.popupSx, children: /* @__PURE__ */ t(
    m,
    {
      ...e,
      shouldShowHoverCard: !1,
      user: n,
      avatarProps: e.popupAvatarProps,
      shortenLabelProps: e.popupShortenLabelProps || e.shortenLabelProps,
      renderFields: e.popupRenderFields,
      renderName: e.popupRenderName,
      infoType: e.popupInfoType || e.infoType,
      didProps: e.popupDidProps || e.didProps,
      showDid: e.popupShowDid || e.showDid
    }
  ) });
  return r === o.AvatarOnly ? /* @__PURE__ */ t(
    A,
    {
      ...e,
      shouldShowHoverCard: u,
      renderCardContent: s,
      user: n
    }
  ) : /* @__PURE__ */ t(S, { containerRef: c, cardType: r, sx: e.sx, children: /* @__PURE__ */ t(
    m,
    {
      ...e,
      shouldShowHoverCard: u,
      renderCardContent: s,
      user: n
    }
  ) });
}
export {
  L as createNameOnlyAvatar,
  B as default
};
