import { writable, get } from 'svelte/store';

import queue from './queue.js';
import { navigate, currentView, previousView } from './routing.js';


export let appState = writable({
    currentRequest: null,
    pendingRequests: queue(),
    credentialStatus: 'locked',
    setupErrors: [],
});


export async function acceptRequest() {
    let req = await get(appState).pendingRequests.get();
    appState.update($appState => {
        $appState.currentRequest = req;
        return $appState;
    });
    previousView.set(get(currentView));
    navigate('Approve');
}


export function completeRequest() {
    appState.update($appState => {
        $appState.currentRequest = null;
        return $appState;
    });
    currentView.set(get(previousView));
    previousView.set(null);
    acceptRequest();
}
