<script lang="ts" context="module">
    import {CheckIfAniListLoggedIn, GetAniListLoggedInUser, GetAniListUserWatchingList} from "../../wailsjs/go/main/App";
    import {MediaListSort} from "../anilist/types/AniListTypes";
    import { aniListUser, watchListPage, animePerPage, aniListPrimary, aniListLoggedIn, aniListWatchlist } from "./GlobalVariablesAndHelperFunctions.svelte"

    let isAniListPrimary: boolean
    let page: number
    let perPage: number

    aniListPrimary.subscribe(value => isAniListPrimary = value)
    watchListPage.subscribe(value => page = value)
    animePerPage.subscribe(value => perPage = value)

    export const CheckIfAniListLoggedInAndLoadWatchList = async () => {
        await CheckIfAniListLoggedIn().then(loggedIn => {
            if (loggedIn) {
                GetAniListLoggedInUser().then(user => {
                    aniListUser.set(user)
                    if (isAniListPrimary) {
                        GetAniListUserWatchingList(page, perPage, MediaListSort.UpdatedTimeDesc).then((watchList) => {
                            aniListWatchlist.set(watchList)
                            aniListLoggedIn.set(loggedIn)
                        })
                    } else {
                        aniListLoggedIn.set(loggedIn)
                    }
                })
            }
        })
    }
</script>