import React from 'react';
import { Avatar } from 'antd';

const hashStr = (s: string) => {
  let h = 2166136261;
  for (const c of s) {
    h ^= c.charCodeAt(0);
    h = Math.imul(h, 16777619);
  }
  return h >>> 0;
};

const initials = (n: string) =>
  n
    .split(/\s+/)
    .filter(Boolean)
    .slice(0, 2)
    .map((w) => w[0])
    .join('')
    .toUpperCase();

export function UserAvatar({ name, size, className }: { name: string; size: number; className?: string }) {
  return (
    <Avatar size={size} className={`tcx-avatar ${className ?? ''}`} style={{ '--h': hashStr(name) % 360, fontSize: 13 } as React.CSSProperties} aria-hidden="true">
      {initials(name)}
    </Avatar>
  );
}
