// Six gradient swatches are defined in styles/utilities.scss (.gradient-avatar-1..6).
const GRADIENT_AVATAR_COUNT = 6

// Deterministic, stable gradient key derived from any seed (user id / openid /
// name). A user who hasn't set a real avatar then always gets the SAME colour
// everywhere it appears, instead of a random or generic one.
export function gradientAvatarKey(seed?: string): string {
  const s = seed || ''
  let h = 5381
  for (let i = 0; i < s.length; i++) {
    h = ((h << 5) + h) ^ s.charCodeAt(i)
    h >>>= 0 // keep as unsigned 32-bit
  }
  return `gradient-avatar-${(h % GRADIENT_AVATAR_COUNT) + 1}`
}
