import type { PageServerLoad } from './$types';
import { error } from '@sveltejs/kit';
import { GetCustomerInvoiceStore } from '$houdini';

export const load: PageServerLoad = async (event) => {
	const { params, locals, parent } = event;
	const parentData = await parent();

	const invoiceResult = await new GetCustomerInvoiceStore().fetch({
		event,
		metadata: { cookie: locals.cookie },
		variables: { id: params.invoice }
	});

	if (!invoiceResult.data?.invoice) {
		throw error(404, 'Invoice not found');
	}

	return {
		...parentData,
		invoice: invoiceResult
	};
};
