import { applyResponseEnricherOverridesToEntries } from "../../modules/overrides.js";
const GLOBAL_ENRICHERS_KEY = "__openMercatoResponseEnrichers__";
let _enricherEntries = null;
function readGlobalEnrichers() {
  try {
    const value = globalThis[GLOBAL_ENRICHERS_KEY];
    return Array.isArray(value) ? value : null;
  } catch {
    return null;
  }
}
function writeGlobalEnrichers(entries) {
  try {
    ;
    globalThis[GLOBAL_ENRICHERS_KEY] = entries;
  } catch {
  }
}
function registerResponseEnrichers(entries) {
  const finalEntries = applyResponseEnricherOverridesToEntries(entries);
  const flat = [];
  for (const entry of finalEntries) {
    for (const enricher of entry.enrichers) {
      flat.push({ moduleId: entry.moduleId, enricher });
    }
  }
  flat.sort((a, b) => (b.enricher.priority ?? 0) - (a.enricher.priority ?? 0));
  _enricherEntries = flat;
  writeGlobalEnrichers(flat);
}
function getResponseEnrichers() {
  const globalEntries = readGlobalEnrichers();
  if (globalEntries) return globalEntries;
  if (!_enricherEntries) {
    return [];
  }
  return _enricherEntries;
}
function getEnrichersForEntity(targetEntity, selector) {
  const entityEntries = getResponseEnrichers().filter(
    (entry) => entry.enricher.targetEntity === targetEntity
  );
  if (!selector || selector.surface === "api-response") {
    return entityEntries;
  }
  return entityEntries.filter((entry) => {
    const qeConfig = entry.enricher.queryEngine;
    if (!qeConfig || !qeConfig.enabled) return false;
    if (selector.engine && qeConfig.engines && qeConfig.engines.length > 0) {
      return qeConfig.engines.includes(selector.engine);
    }
    return true;
  });
}
export {
  getEnrichersForEntity,
  getResponseEnrichers,
  registerResponseEnrichers
};
//# sourceMappingURL=enricher-registry.js.map
