// src/components/docs/generated/docs-index.generated.ts
var DOCS_INDEX_MANIFEST = "docs-index.manifest.json";

// src/components/docs/generated/loader.ts
import { readFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
var baseDir = dirname(fileURLToPath(import.meta.url));
var manifestPromise = null;
var docsPromise = null;
var chunkCache = new Map;
function chunkKeyForId(id) {
  if (!id)
    return "_common";
  if (id.includes("/")) {
    const [prefix] = id.split("/");
    return prefix || "_common";
  }
  return "_common";
}
async function loadManifest() {
  if (!manifestPromise) {
    manifestPromise = readFile(join(baseDir, DOCS_INDEX_MANIFEST), "utf8").then((content) => JSON.parse(content));
  }
  return manifestPromise;
}
function resolveContentRoot(manifest) {
  const root = manifest.contentRoot ?? ".";
  return join(baseDir, root);
}
async function loadChunk(fileName) {
  const cached = chunkCache.get(fileName);
  if (cached)
    return cached;
  const load = readFile(join(baseDir, fileName), "utf8").then((content) => JSON.parse(content));
  chunkCache.set(fileName, load);
  return load;
}
async function listGeneratedDocs() {
  if (!docsPromise) {
    docsPromise = (async () => {
      const manifest = await loadManifest();
      const chunks = await Promise.all(manifest.chunks.map((chunk) => loadChunk(chunk.file)));
      return chunks.flat();
    })();
  }
  return docsPromise;
}
async function getDocsIndexManifest() {
  return loadManifest();
}
async function getGeneratedDocById(id) {
  const manifest = await loadManifest();
  const key = chunkKeyForId(id);
  const chunk = manifest.chunks.find((item) => item.key === key);
  if (!chunk)
    return null;
  const entries = await loadChunk(chunk.file);
  const entry = entries.find((doc) => doc.id === id);
  if (!entry || !entry.contentPath)
    return null;
  const contentPath = join(resolveContentRoot(manifest), entry.contentPath);
  try {
    const content = await readFile(contentPath, "utf8");
    return { entry, content };
  } catch {
    return null;
  }
}
export {
  listGeneratedDocs,
  getGeneratedDocById,
  getDocsIndexManifest
};
