import { VITE_BACKEND_URL } from '$env/static/private';
import { endpoints } from '$lib/utils/api';
import type { LayoutServerLoad } from './$types';

export const load: LayoutServerLoad = async ({ locals, fetch }) => {
	try {
		const category = await fetch(`${VITE_BACKEND_URL}${endpoints.category.categories}`, {
			method: 'GET',
			headers: {
				'Content-Type': 'application/json',
				Accept: 'application/json'
			}
		});

		if (!category.ok) {
			return {
				status: 'error',
				message: 'Category fetch failed'
			};
		}
		return {
			locals,
			category: await category.json()
		};
	} catch (err) {
		return {
			status: 'error',
			message: err instanceof Error ? err?.message : 'Unknown error'
		};
	}
};
