/**
 * Simple markdown renderer for terminal output
 *
 * Converts markdown to terminal-formatted output with colors and indentation.
 * Handles: headers, code blocks, inline code, bold, lists, tables, and links.
 */
import pc from "picocolors";
export function renderMarkdown(markdown) {
    const lines = markdown.split("\n");
    const output = [];
    let inCodeBlock = false;
    let codeBlockLang = "";
    let inTable = false;
    for (let i = 0; i < lines.length; i++) {
        let line = lines[i];
        // Code blocks
        if (line.startsWith("```")) {
            if (!inCodeBlock) {
                inCodeBlock = true;
                codeBlockLang = line.slice(3).trim();
                output.push("");
                if (codeBlockLang) {
                    output.push(pc.dim(`  [${codeBlockLang}]`));
                }
            }
            else {
                inCodeBlock = false;
                codeBlockLang = "";
                output.push("");
            }
            continue;
        }
        if (inCodeBlock) {
            // Render code with cyan color and indentation
            output.push(pc.cyan("  " + line));
            continue;
        }
        // Headers
        if (line.startsWith("# ")) {
            output.push("");
            output.push(pc.bold(pc.underline(line.slice(2))));
            output.push("");
            continue;
        }
        if (line.startsWith("## ")) {
            output.push("");
            output.push(pc.bold(line.slice(3)));
            output.push("");
            continue;
        }
        if (line.startsWith("### ")) {
            output.push("");
            output.push(pc.bold(pc.dim(line.slice(4))));
            continue;
        }
        if (line.startsWith("#### ")) {
            output.push(pc.dim(line.slice(5)));
            continue;
        }
        // Horizontal rules
        if (line.match(/^-{3,}$/) || line.match(/^\*{3,}$/) || line.match(/^_{3,}$/)) {
            output.push(pc.dim("─".repeat(60)));
            continue;
        }
        // Tables
        if (line.startsWith("|")) {
            if (!inTable) {
                inTable = true;
                output.push("");
            }
            // Skip separator lines
            if (line.match(/^\|[-:\s|]+\|$/)) {
                continue;
            }
            // Parse table row
            const cells = line
                .split("|")
                .slice(1, -1)
                .map((c) => c.trim());
            // Check if this is likely a header row (first row after starting table)
            const prevLine = lines[i - 1];
            const isHeader = prevLine && !prevLine.startsWith("|");
            if (isHeader || (prevLine && prevLine.match(/^\|[-:\s|]+\|$/))) {
                // This is a header row or first data row after separator
                const formattedCells = cells.map((c) => isHeader ? pc.bold(c.padEnd(20)) : c.padEnd(20));
                output.push("  " + formattedCells.join("  "));
                if (isHeader) {
                    output.push("  " + pc.dim("─".repeat(cells.length * 22)));
                }
            }
            else {
                const formattedCells = cells.map((c) => c.padEnd(20));
                output.push("  " + formattedCells.join("  "));
            }
            continue;
        }
        else if (inTable) {
            inTable = false;
            output.push("");
        }
        // Unordered lists
        if (line.match(/^(\s*)[-*]\s/)) {
            const match = line.match(/^(\s*)[-*]\s(.*)$/);
            if (match) {
                const indent = match[1];
                const content = formatInline(match[2]);
                output.push(`${indent}  ${pc.cyan("•")} ${content}`);
            }
            continue;
        }
        // Ordered lists
        if (line.match(/^(\s*)\d+\.\s/)) {
            const match = line.match(/^(\s*)(\d+)\.\s(.*)$/);
            if (match) {
                const indent = match[1];
                const num = match[2];
                const content = formatInline(match[3]);
                output.push(`${indent}  ${pc.cyan(num + ".")} ${content}`);
            }
            continue;
        }
        // Blockquotes
        if (line.startsWith(">")) {
            const content = formatInline(line.slice(1).trim());
            output.push(pc.dim("  │ ") + pc.italic(content));
            continue;
        }
        // Regular paragraph
        output.push(formatInline(line));
    }
    return output.join("\n");
}
/**
 * Format inline markdown elements: bold, italic, code, links
 */
function formatInline(text) {
    // Inline code `code`
    text = text.replace(/`([^`]+)`/g, (_, code) => pc.cyan(code));
    // Bold **text** or __text__
    text = text.replace(/\*\*([^*]+)\*\*/g, (_, content) => pc.bold(content));
    text = text.replace(/__([^_]+)__/g, (_, content) => pc.bold(content));
    // Italic *text* or _text_ (be careful not to match list markers)
    text = text.replace(/(?<!\*)\*([^*]+)\*(?!\*)/g, (_, content) => pc.italic(content));
    text = text.replace(/(?<!_)_([^_]+)_(?!_)/g, (_, content) => pc.italic(content));
    // Links [text](url) - show text in blue, url in dim
    text = text.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (_, linkText, url) => {
        return `${pc.blue(linkText)} ${pc.dim(`(${url})`)}`;
    });
    return text;
}
//# sourceMappingURL=markdown-renderer.js.map