import type { PageServerLoad } from './$types';
import { PUBLIC_MEDUSA_KEY } from '$env/static/public';

export const load = (async ({ params, cookies }) => {
	const regionId = cookies.get('selectedRegion');

	const res = await fetch(
		`http://localhost:9000/store/products/${params.id}?region_id=${regionId}`,
		{
			headers: {
				'x-publishable-api-key': PUBLIC_MEDUSA_KEY,
			},
		}
	);

	if (!res.ok) {
		console.error(await res.text());
		throw new Error('Failed to fetch product');
	}

	const { product } = await res.json();

	return {
		product
	};
}) satisfies PageServerLoad;

