// @bun
// 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
            ]
          });
        }
      })
    ]
  });
}
export {
  DocsReferenceIndexClient
};
