import { jsx as _jsx } from "react/jsx-runtime";
import * as React from "react";
import { cn } from "../../lib/utils";
const sizeClasses = {
    sm: "size-7 text-xs",
    md: "size-8 text-sm",
    lg: "size-10 text-base",
};
/** Up to two initials, ignoring any domain prefix like "sitecore\\jdoe". */
export function getAvatarInitials(name) {
    const trimmed = name?.trim();
    if (!trimmed)
        return "?";
    const slashIndex = trimmed.lastIndexOf("\\");
    const cleaned = slashIndex >= 0 && slashIndex < trimmed.length - 1
        ? trimmed.slice(slashIndex + 1)
        : trimmed;
    const parts = cleaned.split(/\s+/).filter(Boolean);
    if (parts.length === 0)
        return "?";
    return parts
        .slice(0, 2)
        .map((part) => part[0])
        .join("")
        .toUpperCase();
}
/**
 * Shared user avatar: renders the uploaded picture when available, falling back to an initials
 * circle. Supports both fallback styling conventions in the codebase — an inline `fallbackColor`
 * (presence, which carries `session.color`) and a `fallbackClassName` (reviews, hash-palette).
 */
export function Avatar({ src, name, size = "sm", fallbackColor, fallbackClassName, bordered = false, title, className, }) {
    const [failed, setFailed] = React.useState(false);
    // Reset the error state when the source changes (e.g. a new ?v= after re-upload).
    React.useEffect(() => setFailed(false), [src]);
    const showImage = !!src && !failed;
    const resolvedTitle = title ?? name;
    const base = cn("inline-grid place-items-center overflow-hidden rounded-full font-medium text-white select-none shrink-0", sizeClasses[size], bordered && "border-2 border-white", className);
    if (showImage) {
        return (_jsx("img", { src: src ?? undefined, alt: resolvedTitle ?? "", title: resolvedTitle, className: cn(base, "object-cover"), onError: () => setFailed(true) }));
    }
    return (_jsx("div", { className: cn(base, 
        // Default background so a missing color/class never yields an invisible chip.
        !fallbackColor && (fallbackClassName || "bg-neutral-grey-50")), style: fallbackColor ? { background: fallbackColor } : undefined, title: resolvedTitle, children: getAvatarInitials(name) }));
}
//# sourceMappingURL=avatar.js.map