import { AVATAR_COLORS, hashString } from "~/lib/avatar-colors";
import { FLAG_PATTERNS, type FlagConfig } from "~/lib/flag-types";

const EXTRA_COLORS = ["#ffffff", "#111827"];
const FLAG_COLORS = [...AVATAR_COLORS, ...EXTRA_COLORS];

function pickColor(seed: number, offset: number, used: Set<string>): string {
  for (let i = 0; i < FLAG_COLORS.length; i++) {
    const color = FLAG_COLORS[(seed + offset + i * 3) % FLAG_COLORS.length];
    if (!used.has(color)) return color;
  }
  return FLAG_COLORS[(seed + offset) % FLAG_COLORS.length];
}

export function generateFlagConfig(seed: string): FlagConfig {
  const hash = hashString(seed || "brackt");
  const pattern = FLAG_PATTERNS[hash % FLAG_PATTERNS.length];
  const used = new Set<string>();
  const colors: string[] = [];

  for (let i = 0; i < 3; i++) {
    const color = pickColor(hash, i * 7 + Math.floor(hash / (i + 1)), used);
    used.add(color);
    colors.push(color);
  }

  return { pattern, colors };
}
