/**
 * Minimal markdown-to-HTML converter for spec rendering.
 * No external dependencies — pure regex-based transformation.
 */
// eslint-disable-next-line max-lines-per-function, complexity -- markdown parser with many patterns
export function renderMarkdown(md) {
    const lines = md.split('\n');
    const output = [];
    let inCodeBlock = false;
    let codeLanguage = '';
    let codeLines = [];
    let inList = false;
    let inTable = false;
    let tableHeaderDone = false;
    for (const line of lines) {
        // Code block fences
        if (line.startsWith('```')) {
            if (inCodeBlock) {
                const code = codeLines.join('\n');
                if (codeLanguage === 'mermaid') {
                    output.push(`<div class="mermaid">${code}</div>`);
                }
                else {
                    const langClass = codeLanguage ? ` class="language-${escapeHtml(codeLanguage)}"` : '';
                    output.push(`<pre><code${langClass}>${escapeHtml(code)}</code></pre>`);
                }
                inCodeBlock = false;
                codeLines = [];
                codeLanguage = '';
            }
            else {
                closeList(output, inList);
                inList = false;
                closeTable(output, inTable);
                inTable = false;
                tableHeaderDone = false;
                inCodeBlock = true;
                codeLanguage = line.slice(3).trim();
            }
            continue;
        }
        if (inCodeBlock) {
            codeLines.push(line);
            continue;
        }
        // Table rows
        if (line.includes('|') && line.trim().startsWith('|')) {
            const cells = parseTableRow(line);
            if (cells.length > 0) {
                // Separator row (e.g. |---|---|)
                if (cells.every((c) => /^[-:]+$/.test(c.trim()))) {
                    tableHeaderDone = true;
                    continue;
                }
                if (!inTable) {
                    closeList(output, inList);
                    inList = false;
                    output.push('<table>');
                    inTable = true;
                    tableHeaderDone = false;
                }
                const tag = !tableHeaderDone ? 'th' : 'td';
                const rowHtml = cells.map((c) => `<${tag}>${renderInline(c.trim())}</${tag}>`).join('');
                output.push(`<tr>${rowHtml}</tr>`);
                continue;
            }
        }
        else if (inTable) {
            closeTable(output, inTable);
            inTable = false;
            tableHeaderDone = false;
        }
        // Empty line
        if (line.trim() === '') {
            closeList(output, inList);
            inList = false;
            continue;
        }
        // Headings
        const headingMatch = /^(#{1,6})\s+(.+)$/.exec(line);
        if (headingMatch) {
            closeList(output, inList);
            inList = false;
            const level = headingMatch[1]?.length ?? 1;
            const text = headingMatch[2] ?? '';
            output.push(`<h${String(level)}>${renderInline(text)}</h${String(level)}>`);
            continue;
        }
        // Blockquote
        if (line.startsWith('> ')) {
            closeList(output, inList);
            inList = false;
            output.push(`<blockquote>${renderInline(line.slice(2))}</blockquote>`);
            continue;
        }
        // Checkbox items
        const checkboxMatch = /^[-*]\s+\[([ xX])\]\s+(.+)$/.exec(line.trim());
        if (checkboxMatch) {
            closeList(output, inList);
            inList = false;
            const checked = checkboxMatch[1] !== ' ';
            const text = checkboxMatch[2] ?? '';
            const checkClass = checked ? 'criteria-check checked' : 'criteria-check';
            const checkmark = checked ? '&#10003;' : '';
            output.push(`<div class="criteria-item"><div class="${checkClass}">${checkmark}</div><span>${renderInline(text)}</span></div>`);
            continue;
        }
        // Unordered list items
        const listMatch = /^[-*]\s+(.+)$/.exec(line.trim());
        if (listMatch) {
            if (!inList) {
                output.push('<ul>');
                inList = true;
            }
            output.push(`<li>${renderInline(listMatch[1] ?? '')}</li>`);
            continue;
        }
        // Paragraph
        closeList(output, inList);
        inList = false;
        output.push(`<p>${renderInline(line)}</p>`);
    }
    // Close any unclosed blocks
    if (inCodeBlock) {
        const code = codeLines.join('\n');
        const langClass = codeLanguage ? ` class="language-${escapeHtml(codeLanguage)}"` : '';
        output.push(`<pre><code${langClass}>${escapeHtml(code)}</code></pre>`);
    }
    closeList(output, inList);
    closeTable(output, inTable);
    return output.join('\n');
}
function renderInline(text) {
    let result = escapeHtml(text);
    // Bold: **text**
    result = result.replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>');
    // Italic: *text* (but not inside bold)
    result = result.replace(/(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)/g, '<em>$1</em>');
    // Inline code: `text`
    result = result.replace(/`([^`]+)`/g, '<code>$1</code>');
    // Links: [text](url)
    result = result.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');
    return result;
}
function escapeHtml(text) {
    return text
        .replace(/&/g, '&amp;')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;')
        .replace(/"/g, '&quot;');
}
function closeList(output, inList) {
    if (inList) {
        output.push('</ul>');
    }
}
function closeTable(output, inTable) {
    if (inTable) {
        output.push('</table>');
    }
}
function parseTableRow(line) {
    const trimmed = line.trim();
    const inner = trimmed.startsWith('|') ? trimmed.slice(1) : trimmed;
    const withoutEnd = inner.endsWith('|') ? inner.slice(0, -1) : inner;
    return withoutEnd.split('|');
}
//# sourceMappingURL=markdown-renderer.js.map