import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { ValidationResultBlock } from "./ValidationResultBlock.js";
const FILE_ACTIONS = new Set([
    "apply_patch",
    "browser_screenshot",
    "glob_files",
    "grep_files",
    "list_files",
    "read_file",
    "ssh_read_file",
    "ssh_write_file",
    "validate_artifact",
    "write_file",
]);
const MAX_DETAIL_LINES = 80;
const MAX_DETAIL_LINE_CHARS = 240;
export function FileToolResultBlock(props) {
    if (props.action === "validate_artifact") {
        const validation = _jsx(ValidationResultBlock, { message: props.message });
        if (validation)
            return validation;
    }
    const summary = parseFileToolResultMessage(props.action, props.message);
    if (!summary)
        return null;
    return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Box, { flexDirection: "row", children: [_jsxs(Text, { color: "gray", children: [summary.metricLabel.padEnd(7), " "] }), _jsx(Text, { color: "gray", children: summary.metricValue })] }), summary.lines.map((line, index) => (_jsx(Text, { color: "gray", children: line || " " }, `${index}:${line.slice(0, 24)}`)))] }));
}
export function parseFileToolResultMessage(action, message) {
    if (!FILE_ACTIONS.has(action))
        return null;
    const trimmed = message.trim();
    if (!trimmed)
        return null;
    const simple = parseSimpleMetric(trimmed);
    if (simple)
        return simple;
    if (trimmed === "no matches") {
        return { metricLabel: "matches", metricValue: "0", lines: [] };
    }
    const lines = clipLines(trimmed);
    if (action === "glob_files" || action === "grep_files") {
        return {
            metricLabel: "matches",
            metricValue: String(trimmed.split(/\r?\n/).filter(Boolean).length),
            lines,
        };
    }
    if (action === "validate_artifact") {
        const errors = lines.filter((line) => /\berror\b/i.test(line)).length;
        const warnings = lines.filter((line) => /\bwarning\b/i.test(line)).length;
        return {
            metricLabel: "issues",
            metricValue: `${errors} error / ${warnings} warning`,
            lines,
        };
    }
    return {
        metricLabel: "detail",
        metricValue: `${lines.length} line${lines.length === 1 ? "" : "s"}`,
        lines,
    };
}
function parseSimpleMetric(message) {
    const match = message.match(/^(\d+)\s+(chars|entries|edits|bytes)(?:\s+.*)?$/i);
    if (!match)
        return null;
    return {
        metricLabel: match[2].toLowerCase(),
        metricValue: match[1],
        lines: message.includes(" from ") || message.includes(" to ") ? [message] : [],
    };
}
function clipLines(message) {
    const lines = message.split(/\r?\n/);
    const clipped = lines.length > MAX_DETAIL_LINES;
    const visible = lines.slice(0, MAX_DETAIL_LINES).map((line) => line.length > MAX_DETAIL_LINE_CHARS
        ? `${line.slice(0, MAX_DETAIL_LINE_CHARS - 3)}...`
        : line);
    if (clipped)
        visible.push(`... ${lines.length - MAX_DETAIL_LINES} more lines ...`);
    return visible;
}
