<!-- @migration-task Error while migrating Svelte code: `<tr>` is invalid inside `<table>` -->
<!--
  -   This file is a part of the SteamWar software.
  -
  -   Copyright (C) 2023 SteamWar.de-Serverteam
  -
  -   This program is free software: you can redistribute it and/or modify
  -   it under the terms of the GNU Affero General Public License as published by
  -   the Free Software Foundation, either version 3 of the License, or
  -   (at your option) any later version.
  -
  -   This program is distributed in the hope that it will be useful,
  -   but WITHOUT ANY WARRANTY; without even the implied warranty of
  -   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  -   GNU Affero General Public License for more details.
  -
  -   You should have received a copy of the GNU Affero General Public License
  -   along with this program.  If not, see <https://www.gnu.org/licenses/>.
  -->

<script lang="ts">
    import {window} from "./util.ts";
    import {t} from "astro-i18n";
    import type {ExtendedEvent} from "@type/event.ts";
    import "@styles/table.css"

    export let event: ExtendedEvent;
    export let group: string;
    export let rows: number = 1;

    $: teamPoints = event.teams.map(team => {
        const fights = event.fights.filter(fight => fight.blueTeam.id === team.id || fight.redTeam.id === team.id);
        const points = fights.reduce((acc, fight) => {
            if (fight.ergebnis === 1 && fight.blueTeam.id === team.id) {
                return acc + 3;
            } else if (fight.ergebnis === 2 && fight.redTeam.id === team.id) {
                return acc + 3;
            } else if (fight.ergebnis === 3) {
                return acc + 1;
            } else {
                return acc;
            }
        }, 0);
        return {
            team,
            points,
        };
    }).sort((a, b) => b.points - a.points);
</script>

<div class="p-3 bg-gray-200 dark:bg-neutral-800 rounded-2xl w-3/4 mx-auto">
    <table class="w-full">
        <thead>
            <tr class="font-bold border-b">
                {#each Array(rows) as i (i)}
                    <td>{t("announcements.table.team")}</td>
                    <td>{t("announcements.table.points")}</td>
                {/each}
            </tr>
        </thead>
        <tbody>
            {#each window(teamPoints, rows) as teams}
                <tr>
                    {#each teams as team (team.team.id)}
                        <td>{team.team.name}</td>
                        <td>{team.points}</td>
                    {/each}
                </tr>
            {/each}
        </tbody>
    </table>
</div>
