<script lang="ts" context="module">
    import {CheckIfSimklLoggedIn, GetSimklLoggedInUser, SimklGetUserWatchlist} from "../../wailsjs/go/main/App";
    import { simklLoggedIn, simklUser, simklPrimary, simklWatchList } from "./GlobalVariablesAndHelperFunctions.svelte";

    let isSimklPrimary: boolean
    simklPrimary.subscribe(value => isSimklPrimary = value)

    export const CheckIfSimklLoggedInAndSetUser = async () => {
        await CheckIfSimklLoggedIn().then(loggedIn => {
            if (loggedIn) {
                GetSimklLoggedInUser().then(user => {
                    if (Object.keys(user).length === 0) {
                        simklLoggedIn.set(false)
                    } else {
                        simklUser.set(user)
                        if (isSimklPrimary) {
                            SimklGetUserWatchlist().then(result => {
                                simklWatchList.set(result)
                                simklLoggedIn.set(loggedIn)
                            })
                        } else {
                            simklLoggedIn.set(loggedIn)
                        }
                    }
                })
            }
        })
    }
</script>