const TEAM_TRANSLATIONS: Record<string, string> = {
  argentina: "Argentine",
  austria: "Autriche",
  belgium: "Belgique",
  brazil: "Brésil",
  canada: "Canada",
  chile: "Chili",
  china: "Chine",
  colombia: "Colombie",
  croatia: "Croatie",
  czechia: "Tchéquie",
  "czech republic": "Tchéquie",
  denmark: "Danemark",
  egypt: "Égypte",
  england: "Angleterre",
  finland: "Finlande",
  france: "France",
  germany: "Allemagne",
  greece: "Grèce",
  ireland: "Irlande",
  italy: "Italie",
  japan: "Japon",
  mexico: "Mexique",
  morocco: "Maroc",
  netherlands: "Pays-Bas",
  norway: "Norvège",
  poland: "Pologne",
  portugal: "Portugal",
  scotland: "Écosse",
  spain: "Espagne",
  sweden: "Suède",
  switzerland: "Suisse",
  turkey: "Turquie",
  ukraine: "Ukraine",
  uruguay: "Uruguay",
  usa: "États-Unis",
  "united states": "États-Unis",
  wales: "Pays de Galles"
};

const COMPETITION_TRANSLATIONS: Record<string, string> = {
  "fifa world cup": "Coupe du monde FIFA",
  "uefa champions league": "Ligue des champions UEFA",
  "uefa europa league": "Ligue Europa UEFA",
  "uefa conference league": "Ligue Europa Conférence",
  "premier league": "Premier League",
  "la liga": "La Liga",
  "serie a": "Serie A",
  bundesliga: "Bundesliga",
  "mixed martial arts": "Arts martiaux mixtes",
  soccer: "Football"
};

const SPORT_TRANSLATIONS: Record<string, string> = {
  baseball: "Baseball",
  basketball: "Basketball",
  football: "Football",
  hockey: "Hockey",
  mma: "MMA",
  rugby: "Rugby",
  soccer: "Football",
  tennis: "Tennis"
};

const SELECTION_TRANSLATIONS: Record<string, string> = {
  draw: "Match nul",
  no: "Non",
  yes: "Oui"
};

export function localizeTeamName(value: string) {
  const trimmed = value.trim();
  if (!trimmed) return value;

  const suffix = extractTeamSuffix(trimmed);
  const key = normalizeKey(suffix.base);
  return TEAM_TRANSLATIONS[key] ? `${TEAM_TRANSLATIONS[key]}${suffix.suffix}` : trimmed;
}

export function localizeSelectionName(value: string) {
  const key = normalizeKey(value);
  return SELECTION_TRANSLATIONS[key] ?? localizeTeamName(value);
}

export function localizeCompetitionName(value: string) {
  const key = normalizeKey(value);
  return COMPETITION_TRANSLATIONS[key] ?? localizeTeamName(value);
}

export function localizeSportName(value: string) {
  const key = normalizeKey(value);
  return SPORT_TRANSLATIONS[key] ?? value;
}

export function localizeEventName(value: string) {
  const parts = value.split(/\s+(?:vs|v\.?|contre)\s+|\s+-\s+/i);
  if (parts.length < 2) return localizeTeamName(value);
  return `${localizeTeamName(parts[0] ?? "")} vs ${localizeTeamName(parts.slice(1).join(" vs "))}`;
}

function extractTeamSuffix(value: string) {
  const suffixes = [
    { pattern: /\s+women$/i, label: " féminines" },
    { pattern: /\s+u21$/i, label: " U21" },
    { pattern: /\s+u20$/i, label: " U20" },
    { pattern: /\s+u19$/i, label: " U19" }
  ];
  for (const suffix of suffixes) {
    if (suffix.pattern.test(value)) {
      return { base: value.replace(suffix.pattern, ""), suffix: suffix.label };
    }
  }
  return { base: value, suffix: "" };
}

function normalizeKey(value: string) {
  return value
    .normalize("NFD")
    .replace(/[\u0300-\u036f]/g, "")
    .toLowerCase()
    .replace(/[^a-z0-9]+/g, " ")
    .trim();
}
