import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { MessageResponse } from "./MessageResponse.js";
import { StatusIcon } from "./design/StatusIcon.js";
import { toneColor } from "./design/terminalTheme.js";
export function HookResultBlock(props) {
    const model = parseHookResultMessage(props.message);
    if (!model)
        return null;
    return (_jsx(MessageResponse, { tone: model.tone, children: _jsxs(Box, { flexDirection: "column", children: [_jsxs(Box, { flexDirection: "row", children: [_jsx(StatusIcon, { state: model.state, withSpace: true }), _jsx(Text, { color: toneColor(model.tone), children: model.title })] }), model.rows.map((row) => (_jsxs(Box, { flexDirection: "column", children: [_jsx(HookResultLine, { row: row }), row.notes.slice(0, 2).map((note) => (_jsx(Text, { color: "gray", children: `  ${note}` }, `${row.id}-${note}`)))] }, `${row.id}-${row.status}`)))] }) }));
}
export function parseHookResultMessage(message) {
    const rows = parseHookRows(message);
    if (rows.length === 0)
        return null;
    const failed = rows.filter((row) => row.status === "failed").length;
    const skipped = rows.filter((row) => row.status === "skipped").length;
    const succeeded = rows.filter((row) => row.status === "succeeded").length;
    const tone = failed > 0 ? "error" : skipped > 0 ? "warning" : "success";
    const state = failed > 0 ? "error" : skipped > 0 ? "warning" : "success";
    return {
        rows,
        tone,
        state,
        title: `hooks ${rows.length} (${[
            succeeded > 0 ? `ok ${succeeded}` : "",
            failed > 0 ? `failed ${failed}` : "",
            skipped > 0 ? `skipped ${skipped}` : "",
        ].filter(Boolean).join(" / ")})`,
    };
}
export function parseHookRows(message) {
    const rows = [];
    let current = null;
    for (const rawLine of message.split(/\r?\n/)) {
        const line = rawLine.trimEnd();
        const match = /^(succeeded|failed|skipped)\s+([^\s]+)\s+exit=([^\s]+)(\s+timed_out)?$/i.exec(line.trim());
        if (match) {
            current = {
                status: match[1].toLowerCase(),
                id: match[2],
                exitCode: match[3],
                timedOut: Boolean(match[4]),
                notes: [],
            };
            rows.push(current);
            continue;
        }
        if (!current || !/^\s+/.test(rawLine))
            continue;
        const note = line.trim();
        if (note)
            current.notes.push(note);
    }
    return rows;
}
function HookResultLine(props) {
    const state = props.row.status === "succeeded" ? "success" : props.row.status === "failed" ? "error" : "warning";
    return (_jsxs(Box, { flexDirection: "row", children: [_jsx(StatusIcon, { state: state, withSpace: true }), _jsx(Text, { children: props.row.id }), _jsx(Text, { color: "gray", children: ` ${props.row.status} exit=${props.row.exitCode}${props.row.timedOut ? " timed out" : ""}` })] }));
}
