function manifestKey(entry) {
  return entry.kind === "legacy" && entry.method ? `${entry.moduleId}\0${entry.method}\0${entry.path}` : `${entry.moduleId}\0${entry.path}`;
}
async function loadOpenApiExport(entry) {
  if (!entry) return void 0;
  try {
    const mod = await entry.load();
    const openApi = mod.openApi;
    return openApi && typeof openApi === "object" ? openApi : void 0;
  } catch {
    return void 0;
  }
}
async function attachOpenApiDocsToModules(modules, manifests) {
  const manifestsByKey = /* @__PURE__ */ new Map();
  for (const entry of manifests) {
    manifestsByKey.set(manifestKey(entry), entry);
  }
  return Promise.all(
    modules.map(async (mod) => {
      if (!Array.isArray(mod.apis) || !mod.apis.length) return mod;
      const apis = await Promise.all(
        mod.apis.map(async (api) => {
          if (api.docs) return api;
          if ("handlers" in api) {
            const entry2 = manifestsByKey.get(`${mod.id}\0${api.path}`);
            const docs2 = await loadOpenApiExport(entry2);
            if (!docs2) return api;
            return { ...api, docs: docs2 };
          }
          const entry = manifestsByKey.get(`${mod.id}\0${api.method}\0${api.path}`);
          const docs = await loadOpenApiExport(entry);
          if (!docs) return api;
          return { ...api, docs };
        })
      );
      return { ...mod, apis };
    })
  );
}
export {
  attachOpenApiDocsToModules
};
//# sourceMappingURL=attach-docs.js.map
