---
import { als } from "virtual:astro-i18n/als";
import { clientId } from "virtual:astro-i18n/internal";

const config = als.getStore();
if (!config) {
	throw new Error(
		"Using `<I18nClient />` requires adding the `i18n` integration to your Astro config.",
	);
}

const { clientOptions } = config;

const enabledClientFeatures = Object.entries(clientOptions)
	.map(([name, enabled]) => ({ name, enabled }))
	.filter((e) => e.enabled);
if (enabledClientFeatures.length === 0) {
	throw new Error(
		"You need to enabled at least one client feature (`client: {...}`) in the integration config to use `<I18nClient />`.",
	);
}

const data = clientOptions.data ? config.data : undefined;
const translations = clientOptions.translations
	? { ...config.translations, initialized: false }
	: undefined;
const paths = clientOptions.paths ? config.paths : undefined;
---
<script is:inline type="application/json" id={clientId} set:html={JSON.stringify({ clientOptions, data, translations, paths })}></script>

