import { createContext, use, 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 = use(LocaleCtx);
	if (!value) throw new Error("useActiveLocale must be used within a LocaleProvider");
	return value;
}
//#endregion
//#region ../../platform/i18n/src/use-domain-dict.ts
function useDomainDict(domain, locale) {
	const [dict, setDict] = useState(() => domain.getCached(locale) ?? domain.fallback);
	useEffect(() => {
		let cancelled = false;
		if (locale === domain.fallbackLocale) {
			setDict(domain.fallback);
			return;
		}
		const cached = domain.getCached(locale);
		if (cached) {
			setDict(cached);
			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";
	const cache = /* @__PURE__ */ new Map();
	return {
		fallback: config.fallback,
		fallbackLocale,
		getCached(locale) {
			if (locale === fallbackLocale) return config.fallback;
			return cache.get(locale) ?? null;
		},
		async load(locale) {
			if (locale === fallbackLocale) return config.fallback;
			const cached = cache.get(locale);
			if (cached) return cached;
			const loader = config.loaders?.[locale];
			if (!loader) return config.fallback;
			const target = await loader();
			const merged = {
				...config.fallback,
				...target
			};
			cache.set(locale, merged);
			return merged;
		}
	};
}
//#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 { useActiveLocale as a, LocaleProvider as i, createDomainTranslations as n, useDomainDict as r, createStaticDictAdapter as t };

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