import { createHash } from "node:crypto";
import { getBackendRouteManifests } from "../../modules/registry.js";
import { getModules } from "./registry.js";
const UNKNOWN_FINGERPRINT = "unknown";
let cachedFingerprint = null;
let cachedManifestsRef = null;
let cachedModulesRef = null;
function readModulesRef() {
  try {
    return getModules();
  } catch {
    return null;
  }
}
function byCodeUnit(a, b) {
  return a < b ? -1 : a > b ? 1 : 0;
}
function describeModules(modules) {
  if (!modules) return [];
  return modules.map((mod) => {
    const features = (Array.isArray(mod.features) ? mod.features : []).filter((feature) => feature && typeof feature.id === "string" && feature.id.length > 0).map((feature) => `${feature.id}:${feature.module || mod.id}`).sort(byCodeUnit);
    return JSON.stringify({ id: mod.id, features });
  }).sort(byCodeUnit);
}
function getModuleSurfaceFingerprint() {
  const manifests = getBackendRouteManifests();
  const modulesRef = readModulesRef();
  if (cachedFingerprint !== null && cachedManifestsRef === manifests && cachedModulesRef === modulesRef) {
    return cachedFingerprint;
  }
  let fingerprint;
  try {
    const modules = describeModules(modulesRef);
    const routes = manifests.map((route) => JSON.stringify(route)).sort(byCodeUnit);
    fingerprint = createHash("sha1").update(JSON.stringify({ modules, routes })).digest("hex").slice(0, 12);
  } catch {
    fingerprint = UNKNOWN_FINGERPRINT;
  }
  cachedManifestsRef = manifests;
  cachedModulesRef = modulesRef;
  cachedFingerprint = fingerprint;
  return fingerprint;
}
export {
  getModuleSurfaceFingerprint
};
//# sourceMappingURL=surfaceFingerprint.js.map
