import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
const MAX_ITEMS = 8;
export function ValidationResultBlock(props) {
    const model = parseValidationResultMessage(props.message);
    if (!model)
        return null;
    const items = model.status === "ok" ? model.checks : model.errors;
    const visible = items.slice(0, MAX_ITEMS);
    const clipped = Math.max(0, items.length - visible.length);
    return (_jsxs(Box, { flexDirection: "column", children: [_jsx(ValidationRow, { label: "valid", value: model.status, color: model.status === "ok" ? "green" : "red" }), _jsx(ValidationRow, { label: "kind", value: model.kind }), _jsx(ValidationRow, { label: "bytes", value: String(model.bytes) }), visible.map((item, index) => (_jsx(ValidationRow, { label: model.status === "ok" ? "check" : "error", value: item, color: model.status === "ok" ? "gray" : "red" }, `${index}:${item.slice(0, 24)}`))), clipped > 0 ? _jsx(Text, { color: "gray", children: `... ${clipped} more validation items ...` }) : null] }));
}
export function parseValidationResultMessage(message) {
    const ok = message.match(/^ok:\s*([^,]+),\s*(\d+)\s+bytes,\s*checks:\s*(.+)$/i);
    if (ok) {
        return {
            status: "ok",
            kind: ok[1]?.trim() ?? "unknown",
            bytes: Number(ok[2] ?? 0),
            checks: splitItems(ok[3] ?? ""),
            errors: [],
        };
    }
    const failed = message.match(/^failed:\s*([^,]+),\s*(\d+)\s+bytes,\s*errors:\s*(.+)$/i);
    if (failed) {
        return {
            status: "failed",
            kind: failed[1]?.trim() ?? "unknown",
            bytes: Number(failed[2] ?? 0),
            checks: [],
            errors: splitItems(failed[3] ?? ""),
        };
    }
    return null;
}
function splitItems(value) {
    return value
        .split(/[,;]\s*/)
        .map((item) => item.trim())
        .filter(Boolean);
}
function ValidationRow(props) {
    return (_jsxs(Box, { flexDirection: "row", children: [_jsxs(Text, { color: "gray", children: [props.label.padEnd(7), " "] }), _jsx(Text, { color: props.color ?? "gray", children: props.value })] }));
}
