// 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;
}
export {
  parseDocsMarkdown
};
