import type { Session } from "~/classes/Session";
import type { User } from "~/classes/User";

export function requireToken(): string
{
    const token = getToken();
    if(token != null)
    {
        return token;
    }
    throw new Error("No session set. Log in first.");
}

export function getToken(): string | undefined
{
    const session = getSession();
    if(session != null)
    {
        return session.token;
    }
    return undefined;
}

export function getUser(): User | undefined
{
    const session = getSession();
    if(session != null)
    {
        return session.user;
    }
    return undefined;
}

export function requireUser(): User
{
    const session = getSession();
    if(session != null)
    {
        return session.user;
    }
    throw new Error("User is required but undefined");
}

function getSession()
{
    return useCookie<Session | undefined>('session').value;
}