const AVATAR_COLORS = [
  "#1a73e8", "#e8710a", "#1e8e3e", "#d93025", "#9334e6",
  "#185abc", "#b06000", "#137333", "#a50e0e", "#7627bb",
  "#0d652d", "#c5221f", "#e37400", "#1967d2", "#8430ce",
]

export function avatarColor(name: string): string {
  let hash = 0
  for (let i = 0; i < name.length; i++) {
    hash = name.charCodeAt(i) + ((hash << 5) - hash)
  }
  return AVATAR_COLORS[Math.abs(hash) % AVATAR_COLORS.length]!
}

export function senderInitial(name: string): string {
  const clean = name.replace(/[[\].,]/g, "").trim()
  return clean.charAt(0).toUpperCase() || "?"
}

export function cleanSenderName(raw: string): string {
  return raw.split(",")[0]?.trim().replace(/\.\./g, " ").replace(/\[.*?\]/g, "").trim() || raw
}

export function resolveSenderEmail(displayName: string, explicit?: string): string {
  if (explicit) return explicit
  const n = cleanSenderName(displayName)
  return `${n.toLowerCase().replace(/\s+/g, ".")}@example.com`
}
