import { jsx as t } from "react/jsx-runtime";
import { useRef as v, useState as A, useEffect as C, useMemo as S } from "react";
import { CardType as d } from "./types.js";
import y from "./Cards/avatar-only.js";
import c from "./Cards/index.js";
import w from "./Container/dialog.js";
import P from "./Container/card.js";
import D from "../Avatar/index.js";
import { default as j } from "./Cards/social-actions.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 = d.Detailed, showHoverCard: l } = e, u = l !== void 0 ? l : r === d.AvatarOnly, m = v(null), [i, s] = A(() => e.user || null);
  C(() => {
    let a = !0;
    return e.user ? s(e.user) : e.did && x(e.did) && !e.user && U(e.did).then((n) => {
      a && s(n || { fullName: "Anonymous", did: e.did, avatar: "" });
    }), () => {
      a = !1;
    };
  }, [e.did, e.user]);
  const o = S(() => {
    if (!i)
      return null;
    const { avatar: a = "", ...n } = i;
    if (a) {
      const h = a.split("?")[1] || "";
      return {
        ...n,
        avatar: h ? a : `${a}?imageFilter=resize&w=48&h=48`
      };
    }
    return i;
  }, [i]);
  if (!o)
    return /* @__PURE__ */ t(D, { did: e.did, size: e.avatarSize, useProxyFallback: !0, ...e.avatarProps });
  const f = () => /* @__PURE__ */ t(w, { sx: e.popupSx, children: /* @__PURE__ */ t(
    c,
    {
      ...e,
      shouldShowHoverCard: !1,
      user: o,
      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,
      showSocialActions: e.popupShowSocialActions === void 0 ? e.showSocialActions : e.popupShowSocialActions
    }
  ) });
  return r === d.AvatarOnly ? /* @__PURE__ */ t(
    y,
    {
      ...e,
      shouldShowHoverCard: u,
      renderCardContent: f,
      user: o
    }
  ) : /* @__PURE__ */ t(P, { containerRef: m, cardType: r, sx: e.sx, children: /* @__PURE__ */ t(
    c,
    {
      ...e,
      shouldShowHoverCard: u,
      renderCardContent: f,
      user: o
    }
  ) });
}
export {
  j as SocialActions,
  L as createNameOnlyAvatar,
  B as default
};
