// src/components/docs/reference/docsMarkdownParser.ts
var METADATA_SECTION = /^##\s+Metadata[\s\S]*?(?=^##\s|^#\s|Z)/m;
function stripMetadata(content) {
  const withoutTitle = content.replace(/^#\s+.*\n+/m, "");
  return withoutTitle.replace(METADATA_SECTION, "");
}
function isBlockStart(line) {
  return /^#{1,4}\s+/.test(line) || /^```/.test(line) || /^\s*[-*]\s+/.test(line) || /^\s*\d+\.\s+/.test(line) || /^>\s+/.test(line);
}
function parseDocsMarkdown(content) {
  const lines = stripMetadata(content).split(/\r?\n/);
  const blocks = [];
  let i = 0;
  while (i < lines.length) {
    const line = lines[i] ?? "";
    if (line.startsWith("<!-- @generated")) {
      i += 1;
      continue;
    }
    const trimmed = line.trim();
    if (!trimmed) {
      i += 1;
      continue;
    }
    if (trimmed.startsWith("```")) {
      const language = trimmed.slice(3).trim();
      const codeLines = [];
      i += 1;
      while (i < lines.length && !(lines[i] ?? "").trim().startsWith("```")) {
        codeLines.push(lines[i] ?? "");
        i += 1;
      }
      i += 1;
      blocks.push({
        type: "code",
        language: language || "text",
        code: codeLines.join(`
`)
      });
      continue;
    }
    if (/^#{1,4}\s+/.test(trimmed)) {
      const level = trimmed.match(/^#{1,4}/)?.[0].length ?? 2;
      blocks.push({
        type: "heading",
        level,
        text: trimmed.replace(/^#{1,4}\s+/, "")
      });
      i += 1;
      continue;
    }
    if (/^>\s+/.test(trimmed)) {
      const quoteLines = [];
      while (i < lines.length && /^>\s+/.test((lines[i] ?? "").trim())) {
        quoteLines.push((lines[i] ?? "").trim().replace(/^>\s+/, ""));
        i += 1;
      }
      blocks.push({ type: "quote", text: quoteLines.join(" ") });
      continue;
    }
    if (/^[-*]\s+/.test(trimmed) || /^\d+\.\s+/.test(trimmed)) {
      const ordered = /^\d+\.\s+/.test(trimmed);
      const items = [];
      while (i < lines.length && isBlockStart((lines[i] ?? "").trim())) {
        const itemLine = (lines[i] ?? "").trim();
        if (ordered && /^\d+\.\s+/.test(itemLine)) {
          items.push(itemLine.replace(/^\d+\.\s+/, ""));
        } else if (!ordered && /^[-*]\s+/.test(itemLine)) {
          items.push(itemLine.replace(/^[-*]\s+/, ""));
        } else {
          break;
        }
        i += 1;
      }
      blocks.push({ type: "list", ordered, items });
      continue;
    }
    const paragraphLines = [];
    while (i < lines.length && !isBlockStart((lines[i] ?? "").trim())) {
      const paragraphLine = (lines[i] ?? "").trim();
      if (!paragraphLine)
        break;
      paragraphLines.push(paragraphLine);
      i += 1;
    }
    blocks.push({ type: "paragraph", text: paragraphLines.join(" ") });
  }
  return blocks;
}

// src/components/docs/reference/DocsMarkdownContent.tsx
import { CodeBlock } from "@contractspec/lib.design-system";
import { VStack, HStack } from "@contractspec/lib.ui-kit-web/ui/stack";
import {
  BlockQuote,
  H2,
  H3,
  H4,
  Muted,
  P
} from "@contractspec/lib.ui-kit-web/ui/typography";
import { jsx, jsxs } from "react/jsx-runtime";
"use client";
var CODE_LANGUAGES = new Set([
  "typescript",
  "tsx",
  "javascript",
  "jsx",
  "bash",
  "shell",
  "json",
  "yaml",
  "graphql",
  "sql",
  "python",
  "go",
  "rust",
  "css",
  "scss",
  "html",
  "markdown",
  "text"
]);
function toCodeLanguage(language) {
  if (!language)
    return "text";
  const normalized = language.toLowerCase();
  return CODE_LANGUAGES.has(normalized) ? normalized : "text";
}
function renderHeading(level, text) {
  if (level <= 2)
    return /* @__PURE__ */ jsx(H2, {
      children: text
    });
  if (level === 3)
    return /* @__PURE__ */ jsx(H3, {
      children: text
    });
  return /* @__PURE__ */ jsx(H4, {
    children: text
  });
}
function DocsMarkdownContent({ content }) {
  const blocks = parseDocsMarkdown(content);
  if (!blocks.length) {
    return /* @__PURE__ */ jsx(Muted, {
      children: "No reference content available."
    });
  }
  return /* @__PURE__ */ jsx(VStack, {
    gap: "md",
    children: blocks.map((block, index) => {
      switch (block.type) {
        case "heading":
          return /* @__PURE__ */ jsx(VStack, {
            children: renderHeading(block.level, block.text)
          }, index);
        case "paragraph":
          return /* @__PURE__ */ jsx(P, {
            children: block.text
          }, index);
        case "quote":
          return /* @__PURE__ */ jsx(BlockQuote, {
            children: block.text
          }, index);
        case "code":
          return /* @__PURE__ */ jsx(CodeBlock, {
            language: toCodeLanguage(block.language),
            code: block.code
          }, index);
        case "list":
          return /* @__PURE__ */ jsx(VStack, {
            gap: "xs",
            children: block.items.map((item, itemIndex) => /* @__PURE__ */ jsxs(HStack, {
              gap: "sm",
              align: "start",
              children: [
                /* @__PURE__ */ jsx(Muted, {
                  children: block.ordered ? `${itemIndex + 1}.` : "•"
                }),
                /* @__PURE__ */ jsx(P, {
                  className: "leading-relaxed",
                  children: item
                })
              ]
            }, itemIndex))
          }, index);
        default:
          return null;
      }
    })
  });
}

// src/components/docs/reference/DocsReferenceContent.tsx
import { DataViewRenderer } from "@contractspec/lib.design-system";
import { VStack as VStack2 } from "@contractspec/lib.ui-kit-web/ui/stack";
import { ContractReferenceDataView } from "@contractspec/lib.contracts-spec/docs";
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
"use client";
function extractMeta(content, label) {
  const match = content.match(new RegExp(`- \\*\\*${label}\\*\\*: (.+)`, "i"));
  return match?.[1]?.trim();
}
function cleanSummary(summary) {
  if (!summary)
    return;
  return summary.replace(/<!--.*?-->/g, "").replace(/[ \t]+/g, " ").replace(/\n{3,}/g, `

`).trim();
}
function DocsReferenceContent({
  entry,
  content
}) {
  const summary = cleanSummary(entry.summary);
  const type = extractMeta(content, "Type");
  const version = extractMeta(content, "Version") ?? entry.version;
  const tags = extractMeta(content, "Tags")?.split(",").map((tag) => tag.trim()).filter(Boolean);
  const owners = extractMeta(content, "Owners")?.split(",").map((owner) => owner.trim()).filter(Boolean);
  const stability = extractMeta(content, "Stability") ?? entry.visibility;
  const referenceItem = {
    reference: {
      key: entry.title ?? entry.id,
      version,
      type,
      title: entry.title ?? entry.id,
      description: summary,
      tags: tags ?? entry.tags,
      owners: owners ?? entry.owners,
      stability
    }
  };
  const detailSpec = {
    ...ContractReferenceDataView,
    meta: {
      ...ContractReferenceDataView.meta,
      title: entry.title ?? ContractReferenceDataView.meta.title,
      description: summary ?? ContractReferenceDataView.meta.description
    }
  };
  return /* @__PURE__ */ jsxs2(VStack2, {
    gap: "xl",
    children: [
      /* @__PURE__ */ jsx2(DataViewRenderer, {
        spec: detailSpec,
        item: referenceItem
      }),
      /* @__PURE__ */ jsx2(DocsMarkdownContent, {
        content
      })
    ]
  });
}
export {
  DocsReferenceContent
};
