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

export const ssr = false;

/**
 * Mirrors the top-level `/bookmarks` route's three-state load shape so
 * a backend hiccup (5xx, network) surfaces inline within the profile
 * tab rather than bouncing the user out of the tabbed shell.
 */
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 };
        }
        if (e instanceof ApiError) {
            return { bookmarks: [], authenticated: true, error: e.message };
        }
        throw e;
    }
};
