<script lang="ts">
  import Card from '$lib/components/Card.svelte';
  import CardGrid from '$lib/components/CardGrid.svelte';
	import type { PageProps } from './$types';
	let { data }: PageProps = $props();

  let commander = $state([])
  let slug = $derived(data.slug)

  const colorCorrespondance = {
    "white": "W",
    "blue": "U",
    "black": "B",
    "red": "R",
    "green": "G",
    "azorius": "UW",
    "dimir": "BU",
    "rakdos": "BR",
    "gruul": "GR",
    "selesnya": "GW",
    "orzhov": "BW",
    "izzet": "RU",
    "golgari": "BG",
    "boros": "RW",
    "simic": "GU",
    "esper": "BUW",
    "grixis": "BRU",
    "jund": "BGR",
    "naya": "GRW",
    "bant": "GUW",
    "abzan": "BGW",
    "jeskai": "RUW",
    "sultai": "BGU",
    "mardu": "BRW",
    "temur": "GRU",
    "yore-tiller": "BRUW",
    "glint-eye": "BGRU",
    "dune-brood": "BGRW",
    "ink-treader": "GRUW",
    "witch-maw": "BGUW",
    "five-colors": "BGRUW",
  }

  $effect(() => {
    commander = []
  
    let api_slug = ""
    if(slug == "all") api_slug = "all"
    else if(slug == "colorless") api_slug = "colorless"
    else api_slug = colorCorrespondance[slug]

    fetch("/json/top/commander/"+api_slug)
        .then( response => response.json() )
        .then( data => {
            commander = data
          })
	});
</script>

<div class="flex flex-col w-full items-center">
  <h1>{slug}</h1>

  <h2>Commandants</h2>
  <CardGrid>
    {#each commander as card}
      <Card normalImage={card.NormalImage} url={card.Url} name={card.Name} price={card.Price} cardmarketUri={card.CardmarketUri} numberOfDecks={card.NumberOfDecks}/>
    {/each}
  </CardGrid>
</div>
