import { TranslationService } from "../services/translation.service.js";
const DEBUG_I18N = process.env.NEXTSPARK_DEBUG_I18N === "true";
const globalCache = globalThis;
function getTranslationCache() {
  if (!globalCache.__nextspark_translations_cache) {
    globalCache.__nextspark_translations_cache = /* @__PURE__ */ new Map();
  }
  return globalCache.__nextspark_translations_cache;
}
function deepMergeMessages(target, source) {
  const result = { ...target };
  for (const key of Object.keys(source)) {
    const sourceValue = source[key];
    const targetValue = result[key];
    if (sourceValue !== null && typeof sourceValue === "object" && !Array.isArray(sourceValue) && targetValue !== null && typeof targetValue === "object" && !Array.isArray(targetValue)) {
      result[key] = deepMergeMessages(
        targetValue,
        sourceValue
      );
    } else if (sourceValue !== void 0) {
      result[key] = sourceValue;
    }
  }
  return result;
}
function getLocaleFallbackChain(locale) {
  const chain = [locale];
  if (locale.includes("-")) {
    chain.push(locale.split("-")[0]);
  }
  if (!chain.includes("en")) {
    chain.push("en");
  }
  return chain;
}
async function loadCoreTranslations(locale) {
  try {
    const coreMessages = await import(`../../messages/${locale}/index.ts`);
    return coreMessages.default || coreMessages;
  } catch {
    return {};
  }
}
let registeredThemeMessages = {};
function registerThemeMessages(locale, messages) {
  registeredThemeMessages[locale] = messages;
}
async function loadThemeTranslations(locale) {
  const activeTheme = process.env.NEXT_PUBLIC_ACTIVE_THEME;
  if (activeTheme) {
    const registryMessages = await TranslationService.load(activeTheme, locale);
    if (Object.keys(registryMessages).length > 0) {
      return registryMessages;
    }
  }
  if (registeredThemeMessages[locale]) {
    return registeredThemeMessages[locale];
  }
  return {};
}
async function loadEntityTranslationsFromRegistry(locale) {
  const activeTheme = process.env.NEXT_PUBLIC_ACTIVE_THEME || "default";
  try {
    const entityTranslations = await TranslationService.loadAllEntities(activeTheme, locale);
    return entityTranslations;
  } catch (error) {
    if (DEBUG_I18N) {
      console.warn(`[translations] Failed to load entity translations for ${locale}:`, error);
    }
    return {};
  }
}
async function loadMergedTranslations(locale) {
  const activeTheme = process.env.NEXT_PUBLIC_ACTIVE_THEME || "default";
  const cacheKey = `${locale}-${activeTheme}`;
  const cache = getTranslationCache();
  if (cache.has(cacheKey)) {
    if (DEBUG_I18N) {
      console.log(`[i18n] Cache hit for ${cacheKey}`);
    }
    return cache.get(cacheKey);
  }
  if (DEBUG_I18N) {
    console.log(`[i18n] Loading translations for ${cacheKey}`);
  }
  const fallbackChain = getLocaleFallbackChain(locale);
  let mergedMessages = {};
  for (const fallbackLocale of [...fallbackChain].reverse()) {
    try {
      const coreMessages = await loadCoreTranslations(fallbackLocale);
      mergedMessages = deepMergeMessages(mergedMessages, coreMessages);
    } catch {
    }
  }
  try {
    const themeMessages = await loadThemeTranslations(locale);
    mergedMessages = deepMergeMessages(mergedMessages, themeMessages);
  } catch {
    if (DEBUG_I18N) {
      console.warn(`[translations] No theme messages for ${locale}`);
    }
  }
  try {
    const entityMessages = await loadEntityTranslationsFromRegistry(locale);
    if (Object.keys(entityMessages).length > 0) {
      mergedMessages = deepMergeMessages(mergedMessages, entityMessages);
    }
  } catch {
  }
  cache.set(cacheKey, mergedMessages);
  if (DEBUG_I18N) {
    console.log(`[i18n] Cached translations for ${cacheKey} with ${Object.keys(mergedMessages).length} namespaces`);
  }
  return mergedMessages;
}
export {
  deepMergeMessages,
  loadMergedTranslations,
  registerThemeMessages
};
