import { getRequestConfig } from "next-intl/server";
import { I18N_CONFIG } from "./lib/config/index.js";
import { headers } from "next/headers";
import { loadMergedTranslations } from "./lib/translations/registry.js";
import { getUserLocale } from "./lib/locale.js";
const DEBUG_I18N = process.env.NEXTSPARK_DEBUG_I18N === "true";
const NAMESPACE_GROUPS = {
  // Páginas públicas iniciales: incluye auth para login/signup
  PUBLIC_INITIAL: ["common", "public", "auth"],
  // Dashboard: incluye public para navegación pero no auth (ya autenticado)
  DASHBOARD_AUTHENTICATED: ["common", "dashboard", "settings", "public", "teams"],
  // Solo autenticación: para páginas específicas de auth
  AUTH_ONLY: ["common", "auth", "validation"],
  // Fallback completo para casos edge
  ALL: ["common", "dashboard", "settings", "auth", "public", "validation", "teams"]
};
function getPageNamespaces(pathname) {
  if (DEBUG_I18N) {
    console.log(`[i18n] Analyzing pathname: "${pathname}"`);
  }
  if (pathname.startsWith("/dashboard")) {
    if (DEBUG_I18N) {
      console.log(`[i18n] Dashboard detected \u2192 Loading authenticated user namespaces`);
    }
    return NAMESPACE_GROUPS.DASHBOARD_AUTHENTICATED;
  }
  const isAuthPage = pathname.startsWith("/auth") || pathname === "/login" || pathname === "/signup" || pathname === "/forgot-password" || pathname === "/reset-password" || pathname === "/verify-email" || pathname.includes("login") || pathname.includes("signup") || pathname.includes("auth");
  if (isAuthPage) {
    if (DEBUG_I18N) {
      console.log(`[i18n] Auth page detected \u2192 Loading auth-only namespaces`);
    }
    return NAMESPACE_GROUPS.AUTH_ONLY;
  }
  if (pathname === "/" || pathname.startsWith("/pricing") || pathname.startsWith("/docs") || pathname.startsWith("/support") || pathname.startsWith("/features")) {
    if (DEBUG_I18N) {
      console.log(`[i18n] Public page detected \u2192 Loading public + auth namespaces`);
    }
    return NAMESPACE_GROUPS.PUBLIC_INITIAL;
  }
  if (!pathname || pathname === "") {
    if (DEBUG_I18N) {
      console.log(`[i18n] Empty pathname \u2192 Using context-aware fallback`);
    }
    if (typeof window !== "undefined") {
      const currentPath = window.location.pathname;
      if (DEBUG_I18N) {
        console.log(`[i18n] Client-side pathname detected: "${currentPath}"`);
      }
      if (currentPath.startsWith("/dashboard")) {
        return NAMESPACE_GROUPS.DASHBOARD_AUTHENTICATED;
      }
      if (currentPath.includes("login") || currentPath.includes("signup") || currentPath.includes("auth")) {
        return NAMESPACE_GROUPS.AUTH_ONLY;
      }
      if (currentPath === "/" || currentPath.startsWith("/pricing") || currentPath.startsWith("/features")) {
        return NAMESPACE_GROUPS.PUBLIC_INITIAL;
      }
    }
    return NAMESPACE_GROUPS.PUBLIC_INITIAL;
  }
  return NAMESPACE_GROUPS.PUBLIC_INITIAL;
}
async function getServerLocale() {
  try {
    return await getUserLocale();
  } catch {
    return I18N_CONFIG.defaultLocale;
  }
}
var i18n_default = getRequestConfig(async () => {
  let locale;
  try {
    locale = await getServerLocale();
  } catch {
    locale = I18N_CONFIG.defaultLocale;
  }
  try {
    let pathname = "";
    try {
      const headersList = await headers();
      pathname = headersList.get("x-pathname") || headersList.get("x-url") || "";
      if (!pathname) {
        const referer = headersList.get("referer") || "";
        if (referer) {
          try {
            const url = new URL(referer);
            pathname = url.pathname;
            if (DEBUG_I18N) {
              console.log(`[i18n] Extracted pathname from referer: "${pathname}"`);
            }
          } catch {
          }
        }
      }
    } catch {
    }
    const messages = await loadMergedTranslations(locale);
    if (DEBUG_I18N) {
      console.log(`[i18n] Loaded merged translations for ${locale} with ${Object.keys(messages).length} namespaces`);
    }
    return {
      locale,
      messages
    };
  } catch (error) {
    console.error(`[i18n] Failed to load translations for ${locale}:`, error);
    return {
      locale: I18N_CONFIG.defaultLocale,
      messages: {}
    };
  }
});
import { loadOptimizedTranslations } from "./lib/translations/i18n-integration.js";
export {
  NAMESPACE_GROUPS,
  i18n_default as default,
  getPageNamespaces,
  loadOptimizedTranslations
};
