import { ApiError } from '$lib/api/client';
import { listMyReadProgress } from '$lib/api/read_progress';
import { listMyUploads } from '$lib/api/uploads';
import type { PageLoad } from './$types';

export const ssr = false;

export const load: PageLoad = async () => {
    try {
        const [progress, uploads] = await Promise.all([
            listMyReadProgress({ limit: 100 }),
            listMyUploads({ limit: 100 })
        ]);
        return {
            authenticated: true,
            progress: progress.items,
            uploads: uploads.items,
            error: null
        };
    } catch (e) {
        if (e instanceof ApiError && e.status === 401) {
            return { authenticated: false, progress: [], uploads: [], error: null };
        }
        if (e instanceof ApiError) {
            return { authenticated: true, progress: [], uploads: [], error: e.message };
        }
        throw e;
    }
};
