const FNV_OFFSET = 2166136261
const FNV_PRIME = 16777619

export function fnv1a(str: string): number {
  let hash = FNV_OFFSET
  for (let i = 0; i < str.length; i++) {
    hash ^= str.charCodeAt(i)
    hash = Math.imul(hash, FNV_PRIME)
  }
  return hash >>> 0
}

export function hashColor(
  name: string,
  theme: 'light' | 'dark' = 'light',
): { bg: string; text: string; border: string } {
  const hue = fnv1a(name) % 360

  if (theme === 'dark') {
    return {
      bg: `hsl(${hue}, 35%, 20%)`,
      text: `hsl(${hue}, 45%, 75%)`,
      border: `hsl(${hue}, 30%, 30%)`,
    }
  }

  return {
    bg: `hsl(${hue}, 45%, 92%)`,
    text: `hsl(${hue}, 55%, 35%)`,
    border: `hsl(${hue}, 35%, 82%)`,
  }
}
