import { applyApiInterceptorOverridesToEntries } from "../../modules/overrides.js";
import { createLogger } from "../logger/index.js";
const logger = createLogger("shared").child({ component: "umes" });
let _interceptorEntries = null;
const GLOBAL_INTERCEPTOR_KEY = "__openMercatoApiInterceptors__";
function readGlobalInterceptors() {
  try {
    const value = globalThis[GLOBAL_INTERCEPTOR_KEY];
    return Array.isArray(value) ? value : null;
  } catch {
    return null;
  }
}
function writeGlobalInterceptors(entries) {
  try {
    ;
    globalThis[GLOBAL_INTERCEPTOR_KEY] = entries;
  } catch {
  }
}
function registerApiInterceptors(entries) {
  const finalEntries = applyApiInterceptorOverridesToEntries(entries);
  const flat = [];
  finalEntries.forEach((entry, moduleOrder) => {
    entry.interceptors.forEach((interceptor, interceptorOrder) => {
      flat.push({
        moduleId: entry.moduleId,
        interceptor,
        moduleOrder,
        interceptorOrder
      });
    });
  });
  _interceptorEntries = flat;
  writeGlobalInterceptors(flat);
}
function getAllApiInterceptors() {
  const globalEntries = readGlobalInterceptors();
  if (globalEntries) return globalEntries;
  if (!_interceptorEntries) return [];
  return _interceptorEntries;
}
function routeMatches(targetRoute, routePath) {
  if (targetRoute === "*") return true;
  if (targetRoute.endsWith("/*")) {
    const prefix = targetRoute.slice(0, -2);
    return routePath === prefix || routePath.startsWith(`${prefix}/`);
  }
  return targetRoute === routePath;
}
const collisionWarnings = /* @__PURE__ */ new Set();
function getApiInterceptorsForRoute(routePath, method) {
  const matching = getAllApiInterceptors().filter((entry) => {
    const methods = entry.interceptor.methods ?? [];
    return methods.includes(method) && routeMatches(entry.interceptor.targetRoute, routePath);
  });
  const sorted = matching.sort((a, b) => {
    const byPriority = (b.interceptor.priority ?? 0) - (a.interceptor.priority ?? 0);
    if (byPriority !== 0) return byPriority;
    const byModule = a.moduleOrder - b.moduleOrder;
    if (byModule !== 0) return byModule;
    return a.interceptorOrder - b.interceptorOrder;
  });
  if (process.env.NODE_ENV !== "production") {
    for (let i = 1; i < sorted.length; i++) {
      const prev = sorted[i - 1];
      const current = sorted[i];
      const prevPriority = prev.interceptor.priority ?? 0;
      const currentPriority = current.interceptor.priority ?? 0;
      if (prevPriority !== currentPriority) continue;
      const warningKey = `${routePath}:${method}:${prev.interceptor.id}:${current.interceptor.id}:${currentPriority}`;
      if (collisionWarnings.has(warningKey)) continue;
      collisionWarnings.add(warningKey);
      logger.warn("Interceptors share the same priority for route \u2014 execution order is based on module registration order", { firstInterceptorId: prev.interceptor.id, secondInterceptorId: current.interceptor.id, priority: currentPriority, routePath });
    }
  }
  return sorted;
}
export {
  getAllApiInterceptors,
  getApiInterceptorsForRoute,
  registerApiInterceptors
};
//# sourceMappingURL=interceptor-registry.js.map
