import { I18N_CONFIG } from "./config";
import { getSafeLocale } from "./i18n-utils";
const loadedNamespaces = /* @__PURE__ */ new Map();
const PAGE_NAMESPACE_MAP = {
  // Dashboard pages
  "/dashboard": ["common", "dashboard"],
  "/dashboard/settings": ["common", "settings"],
  "/dashboard/settings/profile": ["common", "settings"],
  "/dashboard/settings/password": ["common", "settings"],
  "/dashboard/settings/security": ["common", "settings"],
  "/dashboard/settings/notifications": ["common", "settings"],
  "/dashboard/settings/billing": ["common", "settings"],
  // Public pages
  "/": ["common", "public"],
  "/features": ["common", "public"],
  "/pricing": ["common", "public"],
  "/docs": ["common", "public"],
  "/support": ["common", "public"],
  // Auth pages
  "/login": ["common", "auth"],
  "/signup": ["common", "auth"],
  "/forgot-password": ["common", "auth"],
  "/reset-password": ["common", "auth"],
  "/verify-email": ["common", "auth"]
};
function getCacheKey(locale, namespace) {
  return `${locale}:${namespace}`;
}
async function loadNamespace(locale, namespace) {
  const cacheKey = getCacheKey(locale, namespace);
  if (loadedNamespaces.has(cacheKey)) {
    return loadedNamespaces.get(cacheKey);
  }
  try {
    const messages = await import(`../messages/${locale}/${namespace}.json`);
    const namespaceData = messages.default;
    loadedNamespaces.set(cacheKey, namespaceData);
    return namespaceData;
  } catch {
    const fallbackLocale = I18N_CONFIG.defaultLocale;
    if (locale !== fallbackLocale) {
      const fallbackCacheKey = getCacheKey(fallbackLocale, namespace);
      if (loadedNamespaces.has(fallbackCacheKey)) {
        return loadedNamespaces.get(fallbackCacheKey);
      }
      try {
        const fallbackMessages = await import(`../messages/${fallbackLocale}/${namespace}.json`);
        const fallbackData = fallbackMessages.default;
        loadedNamespaces.set(fallbackCacheKey, fallbackData);
        return fallbackData;
      } catch {
        console.warn(`[namespace-loader] Namespace ${namespace} not available for ${locale} or ${fallbackLocale}`);
      }
    }
    return {};
  }
}
async function loadNamespaces(locale, namespaces) {
  const safeLocale = getSafeLocale(locale);
  const namespacePromises = namespaces.map(async (namespace) => {
    const data = await loadNamespace(safeLocale, namespace);
    return [namespace, data];
  });
  const results = await Promise.all(namespacePromises);
  return Object.fromEntries(results);
}
function getPageNamespaces(pathname) {
  if (PAGE_NAMESPACE_MAP[pathname]) {
    return PAGE_NAMESPACE_MAP[pathname];
  }
  if (pathname.startsWith("/dashboard/settings/")) {
    return ["common", "settings"];
  }
  if (pathname.startsWith("/dashboard")) {
    return ["common", "dashboard"];
  }
  if (pathname.startsWith("/auth") || pathname === "/login" || pathname === "/signup") {
    return ["common", "auth"];
  }
  return ["common", "public"];
}
async function loadPageMessages(locale, pathname) {
  const safeLocale = getSafeLocale(locale);
  if (!pathname) {
    return loadAllNamespaces(safeLocale);
  }
  const requiredNamespaces = getPageNamespaces(pathname);
  const namespaceData = await loadNamespaces(safeLocale, requiredNamespaces);
  return namespaceData;
}
async function loadAllNamespaces(locale) {
  try {
    const messages = await import(`../messages/${locale}/index.ts`);
    return messages.default;
  } catch {
    if (locale !== I18N_CONFIG.defaultLocale) {
      try {
        const fallbackMessages = await import(`../messages/${I18N_CONFIG.defaultLocale}/index.ts`);
        return fallbackMessages.default;
      } catch {
        console.warn(`[namespace-loader] All namespaces not available for ${locale} or ${I18N_CONFIG.defaultLocale}`);
      }
    }
    return {};
  }
}
async function preloadCriticalNamespaces(locale) {
  const criticalNamespaces = I18N_CONFIG.performance.preloadCriticalNamespaces || ["common"];
  Promise.all(
    criticalNamespaces.map(
      (namespace) => loadNamespace(locale, namespace).catch(
        (err) => console.warn(`Failed to preload ${namespace}:`, err)
      )
    )
  );
}
function clearNamespaceCache() {
  loadedNamespaces.clear();
}
function getCacheStats() {
  return {
    size: loadedNamespaces.size,
    keys: Array.from(loadedNamespaces.keys())
  };
}
export {
  clearNamespaceCache,
  getCacheStats,
  getPageNamespaces,
  loadNamespace,
  loadNamespaces,
  loadPageMessages,
  preloadCriticalNamespaces
};
