<script>
    import { onMount } from 'svelte';
    import { invoke } from '@tauri-apps/api/tauri';

    import { appState } from '../lib/state.js';
    import { navigate } from '../lib/routing.js';
    import Nav from '../ui/Nav.svelte';
    import Icon from '../ui/Icon.svelte';
    import Link from '../ui/Link.svelte';


    onMount(async () => {
        // will block until a request comes in
        let req = await $appState.pendingRequests.get();
        $appState.currentRequest = req;
        navigate('Approve');
    });
</script>


<Nav />

<div class="flex flex-col h-screen items-center justify-center p-4 space-y-4">
    {#await invoke('get_session_status') then status}
        {#if status === 'locked'}
            <img src="/static/padlock-closed.svg" alt="A locked padlock" class="w-32" />
            <h2 class="text-2xl font-bold">Creddy is locked</h2>
            <Link target="Unlock">
                <button class="btn btn-primary">Unlock</button>
            </Link>

        {:else if status === 'unlocked'}
            <img src="/static/padlock-open.svg" alt="An unlocked padlock" class="w-24" />
            <h2 class="text-2xl font-bold">Waiting for requests</h2>

        {:else if status === 'empty'}
            <Link target="EnterCredentials">
                <button class="btn btn-primary">Enter Credentials</button>
            </Link>
        {/if}
    {/await}
</div>