"use client";
import { jsx } from "react/jsx-runtime";
import { createContext, useContext, useMemo } from "react";
const I18N_CONTEXT_KEY = "__openMercatoI18nContext";
function getI18nContext() {
  const store = globalThis;
  if (!store[I18N_CONTEXT_KEY]) {
    store[I18N_CONTEXT_KEY] = createContext(null);
  }
  return store[I18N_CONTEXT_KEY];
}
const I18nContext = getI18nContext();
function format(template, params) {
  if (!params) return template;
  return template.replace(/\{\{(\w+)\}\}|\{(\w+)\}/g, (_, doubleKey, singleKey) => {
    const key = doubleKey ?? singleKey;
    if (!key) return _;
    const value = params[key];
    if (value === void 0) {
      return doubleKey ? `{{${key}}}` : `{${key}}`;
    }
    return String(value);
  });
}
function I18nProvider({ children, locale, dict, localeLocked = false }) {
  const value = useMemo(() => ({
    locale,
    localeLocked,
    t: (key, fallbackOrParams, params) => {
      let fallback;
      let resolvedParams;
      if (typeof fallbackOrParams === "string") {
        fallback = fallbackOrParams;
        resolvedParams = params;
      } else {
        resolvedParams = fallbackOrParams ?? params;
      }
      const template = dict[key] ?? fallback ?? key;
      return format(template, resolvedParams);
    }
  }), [locale, dict, localeLocked]);
  return /* @__PURE__ */ jsx(I18nContext.Provider, { value, children });
}
function useT() {
  const ctx = useContext(I18nContext);
  if (!ctx) throw new Error("useT must be used within I18nProvider");
  return ctx.t;
}
function useOptionalT() {
  const ctx = useContext(I18nContext);
  return ctx?.t;
}
function useLocale() {
  const ctx = useContext(I18nContext);
  if (!ctx) throw new Error("useLocale must be used within I18nProvider");
  return ctx.locale;
}
function useOptionalLocale() {
  const ctx = useContext(I18nContext);
  return ctx?.locale;
}
function useLocaleLocked() {
  const ctx = useContext(I18nContext);
  return ctx?.localeLocked ?? false;
}
export {
  I18nProvider,
  useLocale,
  useLocaleLocked,
  useOptionalLocale,
  useOptionalT,
  useT
};
//# sourceMappingURL=context.js.map
