import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { summarizeDiff } from "../utils/diff.js";
const MAX_FILE_LABELS = 4;
export function DiffSummaryHeader(props) {
    const model = diffSummaryHeaderModel(props.diff, props.maxFiles ?? MAX_FILE_LABELS);
    return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Text, { color: "gray", children: ["diff ", _jsx(Text, { color: "green", children: `+${model.added}` }), " ", _jsx(Text, { color: "red", children: `-${model.removed}` }), ` | files ${model.files.length + model.clippedFiles} | hunks ${model.hunks}`] }), model.files.length > 0 ? (_jsxs(Text, { color: "gray", children: [model.files.join(", "), model.clippedFiles > 0 ? `, ... ${model.clippedFiles} more` : ""] })) : null] }));
}
export function diffSummaryHeaderModel(diff, maxFiles = MAX_FILE_LABELS) {
    const summary = summarizeDiff(diff);
    const files = [];
    let hunks = 0;
    for (const line of diff.split(/\r?\n/)) {
        if (line.startsWith("@@")) {
            hunks += 1;
            continue;
        }
        const gitMatch = line.match(/^diff --git\s+a\/(.+?)\s+b\/(.+)$/);
        if (gitMatch) {
            pushUnique(files, cleanDiffPath(gitMatch[2] ?? gitMatch[1] ?? ""));
            continue;
        }
        const plusMatch = line.match(/^\+\+\+\s+(.+)$/);
        if (plusMatch) {
            const filePath = cleanDiffPath(plusMatch[1] ?? "");
            if (filePath && filePath !== "/dev/null")
                pushUnique(files, filePath);
        }
    }
    const visibleFiles = files.slice(0, maxFiles);
    return {
        added: summary.added,
        removed: summary.removed,
        hunks,
        files: visibleFiles,
        clippedFiles: Math.max(0, files.length - visibleFiles.length),
    };
}
function pushUnique(values, value) {
    if (value && !values.includes(value))
        values.push(value);
}
function cleanDiffPath(value) {
    const trimmed = value.trim().replace(/^"|"$/g, "");
    if (trimmed === "/dev/null")
        return trimmed;
    return trimmed.replace(/^[ab]\//, "");
}
