import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { readFileEditApprovalPreview } from "../services/approval/fileEditApprovalPreview.js";
import { diffLineColor } from "./StructuredDiff.js";
import { truncateCells } from "./design/textLayout.js";
const DEFAULT_MAX_LINES = 44;
const DEFAULT_WIDTH = 96;
export function FileEditDiffPreviewBlock(props) {
    const model = fileEditDiffPreviewModel(props.projectPath, props.gateId, props.maxLines ?? DEFAULT_MAX_LINES);
    if (!model)
        return null;
    const width = props.width ?? DEFAULT_WIDTH;
    return (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsxs(Text, { children: [_jsx(Text, { color: "cyan", children: model.title }), " ", _jsx(Text, { color: "gray", children: truncateCells(model.meta, Math.max(16, width - 14)) })] }), model.status === "unavailable" ? (_jsx(Text, { color: "yellow", children: `preview unavailable: ${model.unavailableReason}` })) : (_jsxs(Box, { flexDirection: "column", children: [model.lines.map((line, index) => (_jsx(Text, { color: diffLineColor(line), children: truncateCells(line || " ", width) }, `${index}:${line.slice(0, 12)}`))), model.clipped ? _jsx(Text, { color: "gray", children: "... diff preview clipped ..." }) : null] }))] }));
}
export function fileEditDiffPreviewModel(projectPath, gateId, maxLines = DEFAULT_MAX_LINES) {
    if (!projectPath)
        return null;
    const record = readFileEditApprovalPreview(projectPath, gateId);
    if (!record)
        return null;
    return fileEditDiffPreviewModelFromRecord(record, maxLines);
}
export function fileEditDiffPreviewModelFromRecord(record, maxLines = DEFAULT_MAX_LINES) {
    const lines = record.diffLines.slice(0, Math.max(0, maxLines));
    return {
        status: record.status,
        title: "Diff preview",
        meta: `${record.action} ${record.relativePath} | +${record.added} -${record.removed}`,
        lines,
        clipped: record.clipped || record.diffLines.length > lines.length,
        unavailableReason: record.unavailableReason ?? "unavailable",
    };
}
