import { cn } from "@/shared/lib/utils";

interface MemberAvatarProps {
  initials: string;
  name?: string;
  size?: number;
  className?: string;
}

const PALETTE = [
  "bg-blue-500",
  "bg-emerald-500",
  "bg-amber-500",
  "bg-rose-500",
  "bg-violet-500",
  "bg-cyan-500",
  "bg-orange-500",
  "bg-pink-500",
];

function hashColor(seed: string): string {
  let hash = 0;
  for (let i = 0; i < seed.length; i++) hash = (hash * 31 + seed.charCodeAt(i)) >>> 0;
  return PALETTE[hash % PALETTE.length];
}

export function MemberAvatar({ initials, name, size = 24, className }: MemberAvatarProps) {
  const color = hashColor(name ?? initials);
  return (
    <span
      className={cn(
        "inline-flex items-center justify-center rounded-full text-[10px] font-semibold text-white shrink-0",
        color,
        className,
      )}
      style={{ width: size, height: size, fontSize: Math.max(9, size * 0.4) }}
      aria-label={name ?? initials}
    >
      {initials}
    </span>
  );
}
