<script lang="ts">
    import {
        aniListAnime,
        GetAnimeSingleItem,
    } from "./helperModules/GlobalVariablesAndHelperFunctions.svelte";
    import {onMount} from "svelte";
    import Router from "svelte-spa-router"
    import Home from "./routes/Home.svelte";
    import {wrap} from "svelte-spa-router/wrap";
    import Spinner from "./helperComponents/Spinner.svelte";
    import Header from "./helperComponents/Header.svelte";
    import {CheckIfAniListLoggedInAndLoadWatchList} from "./helperModules/CheckIfAniListLoggedInAndLoadWatchList.svelte";
    import { CheckIfMALLoggedInAndSetUser } from "./helperModules/CheckIfMyAnimeListLoggedIn.svelte";
    import {CheckIfSimklLoggedInAndSetUser} from "./helperModules/CheckIsSimklLoggedIn.svelte"
    import {CheckIfAniListLoggedIn} from "../wailsjs/go/main/App";
    import {AniListGetSingleAnimeDefaultData} from "./helperDefaults/AniListGetSingleAnime";

    onMount(async () => {
        await CheckIfAniListLoggedInAndLoadWatchList()
        await CheckIfMALLoggedInAndSetUser()
        await CheckIfSimklLoggedInAndSetUser()
    })
</script>

<Header />
<Router routes={{
    '/': Home,
    '/anime/:id': wrap({
        asyncComponent: () => import('./routes/AnimeRoutePage.svelte'),
        conditions: [
            async () => await CheckIfAniListLoggedIn(),
            async (detail) => {
                aniListAnime.update(value => {
                    value = AniListGetSingleAnimeDefaultData
                    return value
                })
                await GetAnimeSingleItem(Number(detail.params.id), true)
                return Object.keys($aniListAnime).length!==0
            },
        ],
        loadingComponent: Spinner
    }),
    // '*': "Not Found"
}} />
