import { createContext, useContext, useEffect, useMemo, useState } from "react";
import { jsx } from "react/jsx-runtime";
//#region ../../platform/i18n/src/locale-context.tsx
const LocaleCtx = createContext(null);
function LocaleProvider({ initialLocale, onLocaleChange, children }) {
	const [locale, setLocaleState] = useState(initialLocale);
	const value = useMemo(() => ({
		locale,
		setLocale(next) {
			setLocaleState(next);
			onLocaleChange?.(next);
		}
	}), [locale, onLocaleChange]);
	return /* @__PURE__ */ jsx(LocaleCtx.Provider, {
		value,
		children
	});
}
function useActiveLocale() {
	const value = useContext(LocaleCtx);
	if (!value) throw new Error("useActiveLocale must be used within a LocaleProvider");
	return value;
}
//#endregion
//#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 { LocaleProvider as a, createTranslationContext as i, createDomainTranslations as n, useActiveLocale as o, useDomainDict as r, createStaticDictAdapter as t };

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