const DDRAGON_VERSION = '16.9.1';

const CHAMPION_KEY_OVERRIDES: Record<string, string> = {
  ChoGath: 'Chogath',
  KaiSa: 'Kaisa',
  "Kha'Zix": 'Khazix',
  "Kog'Maw": 'KogMaw',
  LeBlanc: 'Leblanc',
  "Vel'Koz": 'Velkoz',
  "Bel'Veth": 'Belveth',
  Wukong: 'MonkeyKing',
  'Nunu & Willump': 'Nunu',
  'Renata Glasc': 'Renata',
};

export function getChampionAssetKey(championName: string | null | undefined) {
  if (!championName) return null;
  const trimmed = championName.trim();
  if (CHAMPION_KEY_OVERRIDES[trimmed]) return CHAMPION_KEY_OVERRIDES[trimmed];
  return trimmed.replace(/[\s'.:&]/g, '');
}

export function getChampionSquareUrl(championName: string | null | undefined) {
  const key = getChampionAssetKey(championName);
  if (!key) return null;
  return `https://ddragon.leagueoflegends.com/cdn/${DDRAGON_VERSION}/img/champion/${key}.png`;
}

export function getChampionSplashUrl(championName: string | null | undefined) {
  const key = getChampionAssetKey(championName);
  if (!key) return null;
  return `https://ddragon.leagueoflegends.com/cdn/img/champion/splash/${key}_0.jpg`;
}

const SUMMONER_SPELL_KEY_BY_ID: Record<number, string> = {
  1: 'SummonerBoost',
  3: 'SummonerExhaust',
  4: 'SummonerFlash',
  6: 'SummonerHaste',
  7: 'SummonerHeal',
  11: 'SummonerSmite',
  12: 'SummonerTeleport',
  13: 'SummonerMana',
  14: 'SummonerDot',
  21: 'SummonerBarrier',
  30: 'SummonerPoroRecall',
  31: 'SummonerPoroThrow',
  32: 'SummonerSnowball',
};

export function getSummonerSpellIconUrl(spellId: number | null | undefined) {
  if (!spellId) return null;
  const key = SUMMONER_SPELL_KEY_BY_ID[spellId];
  if (!key) return null;
  return `https://ddragon.leagueoflegends.com/cdn/${DDRAGON_VERSION}/img/spell/${key}.png`;
}

export function getItemIconUrl(itemId: number | null | undefined) {
  if (!itemId) return null;
  return `https://ddragon.leagueoflegends.com/cdn/${DDRAGON_VERSION}/img/item/${itemId}.png`;
}

export function getGgtiTypeAssetUrl(typeCode: string | null | undefined) {
  if (!typeCode) return null;
  const key = typeCode.trim().toLowerCase();
  const allowed = new Set(['limit', 'farm', 'roam', 'shot', 'safe', 'carry', 'bait']);
  if (!allowed.has(key)) return null;
  return `/ggti-types/${key}.png`;
}

export function getProfileIconUrl(profileIconId: number | null | undefined) {
  if (!profileIconId) return null;
  return `https://ddragon.leagueoflegends.com/cdn/${DDRAGON_VERSION}/img/profileicon/${profileIconId}.png`;
}
