import { error } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import { createApiClient, extractErrorCode } from '$lib/shared/api.server';
import { getErrorMessage } from '$lib/shared/errors';

export const load: PageServerLoad = async ({ params, fetch }) => {
	const api = createApiClient(fetch);
	const result = await api.GET('/api/users/{id}', { params: { path: { id: params.id } } });

	if (!result.response.ok) {
		throw error(result.response.status, getErrorMessage(extractErrorCode(result.error)));
	}

	return { profileUser: result.data! };
};
