import { applyModuleOverridesToModules } from "@open-mercato/shared/modules/overrides";
import { invalidateDictionaryCache, invalidateDictionaryCacheLocales } from "../i18n/dictionary-cache.js";
import { createLogger } from "../logger/index.js";
const logger = createLogger("shared").child({ component: "modules-registry" });
const GLOBAL_KEY = "__openMercatoModulesRegistry__";
function getGlobalModules() {
  return globalThis[GLOBAL_KEY] ?? null;
}
function setGlobalModules(modules) {
  ;
  globalThis[GLOBAL_KEY] = modules;
}
function hasRuntimeContracts(entry) {
  return Object.keys(entry).some((key) => key !== "id" && key !== "translations");
}
function isI18nOnlyRegistration(modules) {
  return modules.length > 0 && modules.every((entry) => !hasRuntimeContracts(entry));
}
function mergeI18nModules(existing, incoming) {
  const incomingById = new Map(incoming.map((entry) => [entry.id, entry]));
  const existingIds = new Set(existing.map((entry) => entry.id));
  const merged = existing.map((entry) => {
    const i18nModule = incomingById.get(entry.id);
    if (!i18nModule?.translations) return entry;
    return {
      ...entry,
      translations: {
        ...entry.translations ?? {},
        ...i18nModule.translations
      }
    };
  });
  for (const entry of incoming) {
    if (!existingIds.has(entry.id)) merged.push(entry);
  }
  return merged;
}
function getTranslationLocales(modules) {
  const locales = /* @__PURE__ */ new Set();
  for (const entry of modules) {
    for (const locale of Object.keys(entry.translations ?? {})) {
      locales.add(locale);
    }
  }
  return [...locales];
}
function preserveExistingTranslations(existing, incoming) {
  const existingById = new Map(existing.map((entry) => [entry.id, entry]));
  return incoming.map((entry) => {
    if (entry.translations) return entry;
    const translations = existingById.get(entry.id)?.translations;
    return translations ? { ...entry, translations } : entry;
  });
}
function registerModules(modules) {
  const existing = getGlobalModules();
  if (existing !== null && process.env.NODE_ENV === "development") {
    logger.debug("Modules re-registered (this may occur during HMR)");
  }
  const nextModules = applyModuleOverridesToModules(modules);
  const i18nOnlyRegistration = isI18nOnlyRegistration(nextModules);
  const shouldMergeI18nOnly = existing !== null && i18nOnlyRegistration;
  const registeredModules = shouldMergeI18nOnly ? mergeI18nModules(existing, nextModules) : preserveExistingTranslations(existing ?? [], nextModules);
  setGlobalModules(registeredModules);
  if (i18nOnlyRegistration) {
    invalidateDictionaryCacheLocales(getTranslationLocales(nextModules));
  } else {
    invalidateDictionaryCache();
  }
}
function getModules() {
  const modules = getGlobalModules();
  if (!modules) {
    throw new Error("[Bootstrap] Modules not registered. Call registerModules() at bootstrap.");
  }
  return modules;
}
function tryGetModules() {
  return getGlobalModules();
}
export {
  getModules,
  registerModules,
  tryGetModules
};
//# sourceMappingURL=registry.js.map
