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

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


    async function approve() {
        let status = await invoke('get_session_status');
        if (status === 'unlocked') {
            navigate('ShowApproved');
        }
        else if (status === 'locked') {
            navigate('Unlock');
        }
        else {
            navigate('EnterCredentials');
        }
    }

    var appName = null;
    if ($appState.currentRequest.clients.length === 1) {
        let path = $appState.currentRequest.clients[0].exe;
        let m = path.match(/\/([^/]+?$)|\\([^\\]+?$)/);
        appName = m[1] || m[2];
    }
</script>


<div class="flex flex-col space-y-4 p-4 m-auto max-w-max h-screen justify-center">
    <!-- <div class="p-4 rounded-box border-2 border-neutral-content"> -->
        <div class="space-y-1 mb-4">
            <h2 class="text-xl font-bold">{appName ? `"${appName}"` : 'An appplication'} would like to access your AWS credentials.</h2>
            {#each $appState.currentRequest.clients as client}
                <p>Path: {client ? client.exe : 'Unknown'}</p>
                <p>PID: {client ? client.pid : 'Unknown'}</p>
            {/each}
        </div>

        <div class="grid grid-cols-2">
            <Link target="ShowDenied" hotkey="Escape">
                <button class="btn btn-error justify-self-start">
                    Deny
                    <kbd class="ml-2 normal-case px-1 py-0.5 rounded border border-neutral">Esc</kbd>
                </button>
            </Link>

            <Link target="{approve}" hotkey="Enter" shift="{true}">
                <button class="btn btn-success justify-self-end">
                    Approve
                    <kbd class="ml-2 normal-case px-1 py-0.5 rounded border border-neutral">Shift</kbd>
                    <span class="mx-0.5">+</span>
                    <kbd class="normal-case px-1 py-0.5 rounded border border-neutral">Enter</kbd>
                </button>
            </Link>
        </div>
</div>