import { listMyBookmarks } from '$lib/api/bookmarks';
import { ApiError } from '$lib/api/client';
import type { PageLoad } from './$types';

export const ssr = false;

export const load: PageLoad = async () => {
    try {
        const page = await listMyBookmarks();
        return { bookmarks: page.items, authenticated: true, error: null };
    } catch (e) {
        if (e instanceof ApiError && e.status === 401) {
            return { bookmarks: [], authenticated: false, error: null };
        }
        // Anything else (502 upstream_unavailable from a backend
        // restart, 500 internal_error) is rendered inline rather than
        // re-thrown — SvelteKit's generic error.html is not the right
        // UX for a transient API blip and the user is already
        // authenticated as far as we know.
        if (e instanceof ApiError) {
            return { bookmarks: [], authenticated: true, error: e.message };
        }
        throw e;
    }
};
