import { jsx } from "react/jsx-runtime";
import ApiDocsExplorer from "./Explorer.js";
import { resolveApiDocsBaseUrl } from "@open-mercato/core/modules/api_docs/lib/resources";
import { APP_VERSION } from "@open-mercato/shared/lib/version";
function collectOperations(doc) {
  const operations = [];
  const paths = Object.keys(doc.paths ?? {}).sort((a, b) => a.localeCompare(b));
  for (const path of paths) {
    const methodEntries = Object.entries(doc.paths[path] ?? {});
    for (const [method, operation] of methodEntries) {
      const methodUpper = method.toUpperCase();
      const op = operation ?? {};
      const summary = typeof op.summary === "string" ? op.summary : void 0;
      const description = typeof op.description === "string" ? op.description : void 0;
      const tag = Array.isArray(op.tags) && typeof op.tags[0] === "string" ? op.tags[0] : "General";
      operations.push({
        id: `${methodUpper}-${path.replace(/[^\w]+/g, "-")}`,
        path,
        method: methodUpper,
        tag,
        summary,
        description,
        operation
      });
    }
  }
  return operations;
}
function buildTagOrder(doc, operations) {
  const fromDoc = Array.isArray(doc.tags) ? doc.tags.map((tag) => tag?.name).filter(Boolean) : [];
  const fromOps = Array.from(new Set(operations.map((operation) => operation.tag)));
  const order = [];
  for (const tag of [...fromDoc, ...fromOps]) {
    if (typeof tag !== "string") continue;
    if (!order.includes(tag)) order.push(tag);
  }
  return order;
}
async function ApiDocsViewerPage() {
  const baseUrl = resolveApiDocsBaseUrl();
  const response = await fetch(`${baseUrl}/docs/openapi`, { cache: "no-store" });
  const doc = response.ok ? await response.json() : {
    openapi: "3.1.0",
    info: {
      title: "Open Mercato API",
      version: APP_VERSION,
      description: "Auto-generated OpenAPI definition for all enabled modules."
    },
    servers: [{ url: baseUrl, description: "Default environment" }],
    paths: {}
  };
  const operations = collectOperations(doc);
  const tagOrder = buildTagOrder(doc, operations);
  return /* @__PURE__ */ jsx(
    ApiDocsExplorer,
    {
      title: doc.info?.title ?? "Open Mercato API",
      version: doc.info?.version ?? APP_VERSION,
      description: doc.info?.description,
      operations,
      tagOrder,
      servers: doc.servers ?? [],
      docsUrl: "https://docs.openmercato.com",
      jsonSpecUrl: "/api/docs/openapi",
      markdownSpecUrl: "/api/docs/markdown"
    }
  );
}
export {
  ApiDocsViewerPage as default
};
//# sourceMappingURL=page.js.map
