import { jsx as _jsx } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { diffLineColor } from "./StructuredDiff.js";
import { MarkdownTable, parseMarkdownTableBlock } from "./MarkdownTable.js";
import { HighlightedCode } from "./HighlightedCode.js";
import { OrderedList } from "./OrderedList.js";
export function Markdown(props) {
    const lines = props.children.split(/\r?\n/);
    const elements = [];
    for (let index = 0; index < lines.length; index += 1) {
        const line = lines[index] ?? "";
        const trimmed = line.trim();
        const table = parseMarkdownTableBlock(lines, index, 96);
        if (table) {
            elements.push(_jsx(MarkdownTable, { model: table.model, width: 96 }, `table-${index}`));
            index = table.endIndex;
            continue;
        }
        if (trimmed.startsWith("```")) {
            const fenceLanguage = trimmed.replace(/^```/, "").trim().toLowerCase();
            const codeLines = [];
            let endIndex = index + 1;
            while (endIndex < lines.length) {
                const candidate = lines[endIndex] ?? "";
                if (candidate.trim().startsWith("```"))
                    break;
                codeLines.push(candidate);
                endIndex += 1;
            }
            elements.push(_jsx(HighlightedCode, { code: codeLines.join("\n"), filePath: fenceLanguage || "text", width: 96, dim: props.dimColor }, `code-${index}`));
            index = endIndex < lines.length ? endIndex : lines.length - 1;
            continue;
        }
        if (isDiffLine(line)) {
            elements.push(_jsx(Text, { color: diffLineColor(line), children: line || " " }, lineKey(index, line)));
            continue;
        }
        if (trimmed.startsWith("#")) {
            elements.push(_jsx(Text, { color: "cyan", bold: true, children: trimmed.replace(/^#+\s*/, "") }, lineKey(index, line)));
            continue;
        }
        if (/^[-*]\s+/.test(trimmed)) {
            elements.push(_jsx(Text, { color: props.dimColor ? "gray" : undefined, children: `  - ${trimmed.replace(/^[-*]\s+/, "")}` }, lineKey(index, line)));
            continue;
        }
        if (/^\d+\.\s+/.test(trimmed)) {
            const block = parseOrderedListBlock(lines, index);
            elements.push(_jsx(OrderedList, { children: block.items.map((item, itemIndex) => (_jsx(OrderedList.Item, { children: _jsx(Text, { color: props.dimColor ? "gray" : undefined, children: item }) }, `${itemIndex}-${item.slice(0, 20)}`))) }, `ordered-${index}`));
            index = block.endIndex;
            continue;
        }
        if (trimmed.startsWith(">")) {
            elements.push(_jsx(Text, { color: "gray", children: trimmed }, lineKey(index, line)));
            continue;
        }
        elements.push(_jsx(Text, { color: props.dimColor ? "gray" : undefined, children: line || " " }, lineKey(index, line)));
    }
    return (_jsx(Box, { flexDirection: "column", children: elements }));
}
function lineKey(index, line) {
    return `${index}:${line.slice(0, 24)}`;
}
function isDiffLine(line) {
    return line.startsWith("@@") ||
        line.startsWith("+++") ||
        line.startsWith("---") ||
        /^\+[^\s+]/.test(line) ||
        /^-[^\s-]/.test(line);
}
export function parseOrderedListBlock(lines, startIndex) {
    const items = [];
    let index = startIndex;
    while (index < lines.length) {
        const trimmed = (lines[index] ?? "").trim();
        const match = /^\d+\.\s+(.+)$/.exec(trimmed);
        if (!match)
            break;
        items.push(match[1] ?? "");
        index += 1;
    }
    return {
        items,
        endIndex: Math.max(startIndex, index - 1),
    };
}
