import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { parseActionSummary } from "./ActionSummaryBlock.js";
export function FileEditPreviewBlock(props) {
    const model = fileEditPreviewModel(props.summary);
    if (!model)
        return null;
    return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Text, { children: [_jsx(Text, { color: riskColor(model.risk), children: model.title }), " ", _jsx(Text, { color: "gray", children: model.risk })] }), _jsx(PreviewRow, { label: "path", value: model.path }), _jsx(PreviewRow, { label: "operation", value: model.operation }), _jsx(PreviewRow, { label: "impact", value: model.impact }), model.detail ? _jsx(PreviewRow, { label: "detail", value: model.detail }) : null, model.change ? _jsx(PreviewRow, { label: "change", value: model.change, color: changeColor(model.change) }) : null, model.fingerprint ? _jsx(PreviewRow, { label: "sha", value: model.fingerprint }) : null, _jsx(PreviewRow, { label: "preview", value: model.hint, color: "gray" })] }));
}
export function fileEditPreviewModel(summary) {
    const parsed = parseActionSummary(summary);
    if (!parsed)
        return null;
    const fields = Object.fromEntries(parsed.fields.map((field) => [field.key, field.value]));
    if (parsed.action === "write_file") {
        const overwrite = fields.overwrite === "true";
        const chars = parseInteger(fields.chars);
        const lines = parseInteger(fields.lines);
        const change = projectedChange(fields);
        return {
            action: "write_file",
            title: overwrite ? "Overwrite file" : "Create file",
            path: fields.path || "(unknown)",
            operation: overwrite ? "replace file content" : "write new file",
            impact: chars === undefined ? "unknown size" : `${chars} chars`,
            detail: lines === undefined ? "" : `${lines} line${lines === 1 ? "" : "s"}`,
            change,
            fingerprint: fields.sha || "",
            risk: overwrite ? "high" : "medium",
            hint: overwrite ? "approve once, then review the diff" : "approve once to create, then review the diff",
        };
    }
    if (parsed.action === "apply_patch") {
        const edits = parseInteger(fields.edits);
        const searchChars = parseInteger(fields.searchChars);
        const replaceChars = parseInteger(fields.replaceChars);
        const change = projectedChange(fields);
        return {
            action: "apply_patch",
            title: "Apply patch",
            path: fields.path || "(unknown)",
            operation: "search/replace edits",
            impact: edits === undefined ? "unknown edits" : `${edits} edit${edits === 1 ? "" : "s"}`,
            detail: searchChars === undefined || replaceChars === undefined
                ? ""
                : `${searchChars} search chars -> ${replaceChars} replace chars`,
            change,
            fingerprint: fields.sha || "",
            risk: "high",
            hint: "approve once, then review the diff",
        };
    }
    return null;
}
function PreviewRow(props) {
    return (_jsxs(Box, { flexDirection: "row", children: [_jsxs(Text, { color: "gray", children: [props.label.padEnd(9), " "] }), _jsx(Text, { color: props.color ?? "gray", children: props.value })] }));
}
function parseInteger(value) {
    if (!value || !/^\d+$/.test(value))
        return undefined;
    return Number(value);
}
function projectedChange(fields) {
    const projected = fields.projected;
    if (!projected)
        return "";
    if (projected !== "ok")
        return `projection ${projected}`;
    const added = parseInteger(fields.added);
    const removed = parseInteger(fields.removed);
    const oldLines = parseInteger(fields.oldLines);
    const newLines = parseInteger(fields.newLines);
    const exists = fields.exists ? `exists ${fields.exists}` : "";
    const delta = added === undefined || removed === undefined ? "diff ready" : `+${added} -${removed}`;
    const size = oldLines === undefined || newLines === undefined ? "" : `${oldLines} -> ${newLines} lines`;
    return [delta, size, exists].filter(Boolean).join(" | ");
}
function riskColor(risk) {
    if (risk === "high")
        return "yellow";
    if (risk === "medium")
        return "cyan";
    return "green";
}
function changeColor(change) {
    if (/projection/.test(change))
        return "yellow";
    if (/\+\d+ -0/.test(change))
        return "green";
    return "gray";
}
