import { createContext, useContext, useEffect, useState } from "react";
//#region ../../platform/i18n/src/translation-api-context-factory.ts
function createTranslationContext(domainName) {
	const Ctx = createContext(null);
	function useTranslation() {
		const api = useContext(Ctx);
		if (!api) throw new Error(`use${domainName}Translation must be used within a ${domainName}TranslationProvider`);
		return api;
	}
	return {
		Context: Ctx,
		Provider: Ctx.Provider,
		useTranslation
	};
}
//#endregion
//#region ../../platform/i18n/src/use-domain-dict.ts
function useDomainDict(domain, locale) {
	const [dict, setDict] = useState(domain.fallback);
	useEffect(() => {
		let cancelled = false;
		if (locale === domain.fallbackLocale) {
			setDict(domain.fallback);
			return;
		}
		domain.load(locale).then((next) => {
			if (!cancelled) setDict(next);
		}).catch(() => {
			if (!cancelled) setDict(domain.fallback);
		});
		return () => {
			cancelled = true;
		};
	}, [domain, locale]);
	return dict;
}
//#endregion
//#region ../../platform/i18n/src/translations.ts
function createDomainTranslations(config) {
	const fallbackLocale = config.fallbackLocale ?? "en";
	return {
		fallback: config.fallback,
		fallbackLocale,
		async load(locale) {
			if (locale === fallbackLocale) return config.fallback;
			const loader = config.loaders?.[locale];
			if (!loader) return config.fallback;
			const target = await loader();
			return {
				...config.fallback,
				...target
			};
		}
	};
}
//#endregion
//#region ../../platform/i18n/src/static-dict-adapter.ts
function createStaticDictAdapter(locale, dict) {
	return {
		locale,
		t(key, ...args) {
			const template = dict[key] ?? key;
			const params = args[0];
			if (!params) return template;
			return template.replace(/\{\{(\w+)\}\}/g, (match, name) => name in params ? String(params[name]) : match);
		}
	};
}
//#endregion
export { createTranslationContext as i, createDomainTranslations as n, useDomainDict as r, createStaticDictAdapter as t };

//# sourceMappingURL=static-dict-adapter-BGSA0IiX.mjs.map