// @bun
// 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 "fs/promises";
import { dirname, join } from "path";
import { fileURLToPath } from "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;
  }
}

// src/components/docs/reference/DocsReferenceIndexClient.tsx
import { useMemo, useState } from "react";
import {
  DataViewList,
  formRenderer,
  StatusChip
} from "@contractspec/lib.design-system";
import { VStack, HStack } from "@contractspec/lib.ui-kit-web/ui/stack";
import { H1, Muted } from "@contractspec/lib.ui-kit-web/ui/typography";
import {
  DocsIndexDataView,
  DocsSearchForm
} from "@contractspec/lib.contracts-spec/docs";
import { jsx, jsxs } from "react/jsx-runtime";
"use client";
function cleanSummary(summary) {
  if (!summary)
    return;
  return summary.replace(/<!--.*?-->/g, "").replace(/[ \t]+/g, " ").replace(/\n{3,}/g, `

`).trim();
}
function normalizeEntry(entry) {
  const summary = cleanSummary(entry.summary);
  const kind = entry.kind ?? (entry.source === "generated" ? "reference" : undefined);
  const visibility = entry.visibility ?? (entry.source === "generated" ? "public" : undefined);
  return {
    ...entry,
    summary,
    kind,
    visibility
  };
}
function matchesQuery(entry, query) {
  if (!query)
    return true;
  const haystack = [entry.title, entry.summary, entry.id, ...entry.tags ?? []].filter(Boolean).join(" ").toLowerCase();
  return haystack.includes(query.toLowerCase());
}
function DocsReferenceIndexClient({
  entries
}) {
  const [filters, setFilters] = useState({});
  const normalizedEntries = useMemo(() => entries.map(normalizeEntry), [entries]);
  const filteredEntries = useMemo(() => {
    const query = filters.query?.trim() ?? "";
    return normalizedEntries.filter((entry) => {
      if (filters.kind && entry.kind !== filters.kind)
        return false;
      if (filters.visibility && entry.visibility !== filters.visibility)
        return false;
      return matchesQuery(entry, query);
    });
  }, [filters, normalizedEntries]);
  const searchForm = formRenderer.render(DocsSearchForm, {
    defaultValues: {
      query: filters.query ?? "",
      visibility: filters.visibility ?? "",
      kind: filters.kind ?? ""
    },
    overrides: {
      onSubmitOverride: (values) => {
        const submitted = values;
        setFilters({
          query: submitted.query ?? "",
          visibility: submitted.visibility ?? "",
          kind: submitted.kind ?? ""
        });
      }
    }
  });
  return /* @__PURE__ */ jsxs(VStack, {
    gap: "xl",
    children: [
      /* @__PURE__ */ jsxs(VStack, {
        gap: "sm",
        children: [
          /* @__PURE__ */ jsx(H1, {
            children: "Contract Reference"
          }),
          /* @__PURE__ */ jsx(Muted, {
            children: "Auto-generated reference docs for operations, events, forms, data views, and presentations."
          })
        ]
      }),
      /* @__PURE__ */ jsx(VStack, {
        gap: "sm",
        className: "max-w-2xl",
        children: searchForm
      }),
      /* @__PURE__ */ jsx(DataViewList, {
        spec: DocsIndexDataView,
        items: filteredEntries,
        emptyState: /* @__PURE__ */ jsx(Muted, {
          children: "No reference docs match your filters."
        }),
        onSelect: (entry) => {
          if (entry.route) {
            window.location.assign(entry.route);
          }
        },
        renderActions: (entry) => {
          return /* @__PURE__ */ jsxs(HStack, {
            gap: "xs",
            justify: "end",
            children: [
              entry.kind ? /* @__PURE__ */ jsx(StatusChip, {
                size: "sm",
                label: entry.kind
              }) : null,
              entry.visibility ? /* @__PURE__ */ jsx(StatusChip, {
                size: "sm",
                label: entry.visibility
              }) : null,
              entry.version ? /* @__PURE__ */ jsx(StatusChip, {
                size: "sm",
                label: `v${entry.version}`
              }) : null
            ]
          });
        }
      })
    ]
  });
}

// src/components/docs/reference/DocsReferenceIndexPage.tsx
import { jsx as jsx2 } from "react/jsx-runtime";
async function DocsReferenceIndexPage() {
  const entries = await listGeneratedDocs();
  return /* @__PURE__ */ jsx2(DocsReferenceIndexClient, {
    entries
  });
}
export {
  DocsReferenceIndexPage
};
