import { TranslationService } from "../services/translation.service.js";
import { loadMergedTranslations } from "./registry.js";
import { getAllEntityConfigs, ensureInitialized } from "../entities/registry.js";
async function loadAllI18nTranslations(locale) {
  try {
    await ensureEntitiesInitialized();
    const entities = getAllEntityConfigs().filter((entity) => entity.enabled);
    console.log(`[i18n-integration] Loading translations for ${entities.length} entities:`, entities.map((e) => e.slug));
    return await loadMergedTranslations(locale);
  } catch (error) {
    console.error(`[i18n-integration] Failed to load translations for ${locale}:`, error);
    return {};
  }
}
async function ensureEntitiesInitialized() {
  try {
    await ensureInitialized();
  } catch (error) {
    console.warn("[i18n-integration] Failed to initialize entities:", error);
  }
}
function getCoreNamespaces() {
  try {
    const { getCoreNamespaces: getCoreNamespaces2 } = require("@nextsparkjs/registries/namespace-registry");
    return getCoreNamespaces2();
  } catch (error) {
    console.warn("Failed to load core namespaces from registry:", error);
    return ["common", "dashboard", "settings", "auth", "public", "validation", "admin", "teams"];
  }
}
function getOptimizedNamespaces(pathname) {
  console.log(`[i18n-integration] \u{1F50D} Analyzing pathname: "${pathname}"`);
  try {
    const { detectRouteStrategy, getOptimizedNamespaces: getOptimizedNamespaces2 } = require("@nextsparkjs/registries/namespace-registry");
    const strategy = detectRouteStrategy(pathname);
    const namespaceConfig = getOptimizedNamespaces2(strategy);
    console.log(`[i18n-integration] \u{1F4CB} Registry strategy: ${strategy}`);
    return namespaceConfig;
  } catch (error) {
    console.warn("Failed to load optimized namespaces from registry:", error);
    if (pathname.startsWith("/dashboard")) {
      console.log(`[i18n-integration] \u{1F4F1} Dashboard detected \u2192 Loading authenticated user namespaces`);
      return {
        core: ["common", "dashboard", "settings", "public", "validation", "teams", "admin"],
        entities: [],
        // Entities will be loaded dynamically
        strategy: "DASHBOARD_AUTHENTICATED"
      };
    }
    const isAuthPage = pathname.startsWith("/auth") || pathname === "/login" || pathname === "/signup" || pathname === "/forgot-password" || pathname === "/reset-password" || pathname === "/verify-email";
    if (isAuthPage) {
      console.log(`[i18n-integration] \u{1F510} Auth page detected \u2192 Loading auth-only namespaces`);
      return {
        core: ["common", "auth", "validation"],
        entities: [],
        strategy: "AUTH_ONLY"
      };
    }
    if (pathname === "/" || pathname.startsWith("/pricing") || pathname.startsWith("/docs") || pathname.startsWith("/support") || pathname.startsWith("/features") || pathname.startsWith("/products")) {
      console.log(`[i18n-integration] \u{1F310} Public page detected \u2192 Loading public + auth namespaces`);
      return {
        core: ["common", "public", "auth"],
        entities: [],
        // Some public pages might show entities
        strategy: "PUBLIC_INITIAL"
      };
    }
    if (pathname.startsWith("/superadmin")) {
      console.log(`[i18n-integration] \u{1F6E0}\uFE0F Superadmin panel detected \u2192 Loading superadmin namespaces`);
      return {
        core: ["common", "superadmin", "dashboard"],
        entities: [],
        strategy: "SUPERADMIN"
      };
    }
    console.log(`[i18n-integration] \u{1F504} Unknown route \u2192 Loading public as fallback`);
    return {
      core: ["common", "public"],
      entities: [],
      strategy: "UNKNOWN_FALLBACK"
    };
  }
}
async function loadOptimizedTranslations(locale, pathname = "") {
  const { core, strategy } = getOptimizedNamespaces(pathname);
  try {
    const allTranslations = await loadMergedTranslations(locale);
    const optimizedTranslations = {};
    for (const namespace of core) {
      if (allTranslations[namespace]) {
        optimizedTranslations[namespace] = allTranslations[namespace];
      }
    }
    if (strategy === "DASHBOARD_AUTHENTICATED" || pathname.includes("[entity]")) {
      const activeTheme = process.env.NEXT_PUBLIC_ACTIVE_THEME || "default";
      const entityTranslations = await TranslationService.loadAllEntities(activeTheme, locale);
      for (const [entityName, translations] of Object.entries(entityTranslations)) {
        if (Object.keys(translations).length > 0) {
          optimizedTranslations[entityName] = translations;
        }
      }
    }
    console.log(`[i18n-integration] Loaded optimized translations for ${locale} (${strategy}):`, {
      namespaces: Object.keys(optimizedTranslations)
    });
    return optimizedTranslations;
  } catch (error) {
    console.error(`[i18n-integration] Failed to load optimized translations for ${locale}:`, error);
    return {};
  }
}
export {
  getCoreNamespaces,
  getOptimizedNamespaces,
  loadAllI18nTranslations,
  loadOptimizedTranslations
};
