// Deterministic fallback color for a space without an explicit brand color.
//
// MUST stay byte-for-byte equivalent in logic to the backend copy
// (src/spaces/space-color.ts): same palette, same FNV-1a hash, same modulo.
// The cross-space calendar trusts the API's `color` field (which already
// applies this fallback), but this UI copy is the single source of truth the
// backend mirrors and is used wherever the UI must derive a space color on its
// own.

const SPACE_COLOR_PALETTE = [
  '#6366f1', // indigo
  '#0ea5e9', // sky
  '#10b981', // emerald
  '#f59e0b', // amber
  '#ef4444', // red
  '#ec4899', // pink
  '#8b5cf6', // violet
  '#14b8a6', // teal
  '#f97316', // orange
  '#84cc16', // lime
] as const;

function fnv1a(str: string): number {
  let h = 0x811c9dc5;
  for (let i = 0; i < str.length; i++) {
    h ^= str.charCodeAt(i);
    h = Math.imul(h, 0x01000193) >>> 0;
  }
  return h >>> 0;
}

/** Stable hex color for a space id (deterministic palette pick). */
export function spaceColor(id: string): string {
  const idx = fnv1a(id) % SPACE_COLOR_PALETTE.length;
  return SPACE_COLOR_PALETTE[idx]!;
}
