import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { fileEditPreviewModel } from "./FileEditPreviewBlock.js";
import { fileEditDiffPreviewModel, } from "./FileEditDiffPreviewBlock.js";
import { diffLineColor } from "./StructuredDiff.js";
import { SelectList } from "./design/SelectList.js";
import { StatusBadge } from "./design/StatusBadge.js";
import { Tabs } from "./design/Tabs.js";
import { truncateCells } from "./design/textLayout.js";
export function FileEditReviewPanel(props) {
    const model = fileEditReviewPanelModel({
        summary: props.summary,
        projectPath: props.projectPath,
        gateId: props.gateId,
        maxLines: props.maxLines,
    });
    if (!model)
        return null;
    const width = props.width ?? 96;
    return (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsxs(Box, { flexDirection: "row", justifyContent: "space-between", children: [_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: "cyan", children: model.title }), _jsx(Text, { color: "gray", children: truncateCells(model.subtitle, Math.max(24, width - 20)) })] }), _jsx(StatusBadge, { label: model.risk, tone: model.riskTone })] }), _jsx(Box, { marginTop: 1, children: _jsx(Tabs, { tabs: model.tabs, selectedId: "summary", title: "review", width: width }) }), _jsx(Box, { flexDirection: "column", marginTop: 1, children: model.summaryRows.map((row) => (_jsx(ReviewRow, { row: row, width: width }, row.key))) }), model.diff ? _jsx(DiffPreview, { model: model.diff, width: width }) : null, _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: "gray", children: "decision" }) }), _jsx(SelectList, { options: model.decisionOptions, selectedIndex: 0, visibleCount: 4, width: width })] }));
}
export function fileEditReviewPanelModel(input) {
    const preview = fileEditPreviewModel(input.summary);
    if (!preview)
        return null;
    const diff = fileEditDiffPreviewModel(input.projectPath, input.gateId, input.maxLines ?? 28);
    const diffCount = diff?.status === "ok" ? diff.lines.length : 0;
    return {
        title: preview.title,
        subtitle: preview.path,
        risk: preview.risk,
        riskTone: riskTone(preview.risk),
        tabs: [
            { id: "summary", title: "summary", tone: "brand" },
            {
                id: "diff",
                title: diff?.status === "unavailable" ? "diff n/a" : "diff",
                count: diffCount,
                tone: diff?.status === "unavailable" ? "warning" : diff ? "success" : "muted",
            },
            { id: "decision", title: "decision", tone: "muted" },
        ],
        summaryRows: [
            row("path", "path", "target", "brand", preview.path),
            row("operation", "operation", operationStatus(preview), riskTone(preview.risk), preview.operation),
            row("impact", "impact", preview.impact, impactTone(preview.change), preview.detail || "size metadata unavailable"),
            row("change", "change", preview.change ? "projected" : "unknown", preview.change ? "success" : "muted", preview.change || "no projected diff metadata"),
            row("fingerprint", "fingerprint", preview.fingerprint ? "sha" : "none", preview.fingerprint ? "muted" : "warning", preview.fingerprint || "approval fingerprint unavailable"),
        ],
        decisionOptions: decisionOptions(input.gateId, preview),
        diff,
    };
}
function ReviewRow(props) {
    const detailWidth = Math.max(18, props.width - 34);
    return (_jsxs(Box, { flexDirection: "row", children: [_jsx(StatusBadge, { label: props.row.status, tone: props.row.tone }), _jsx(Text, { children: " " }), _jsx(Text, { color: "cyan", children: truncateCells(props.row.label.padEnd(12), 12) }), _jsx(Text, { color: "gray", children: truncateCells(props.row.detail, detailWidth) })] }));
}
function DiffPreview(props) {
    return (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsxs(Text, { children: [_jsx(Text, { color: "cyan", children: props.model.title }), " ", _jsx(Text, { color: "gray", children: truncateCells(props.model.meta, Math.max(18, props.width - 16)) })] }), props.model.status === "unavailable" ? (_jsx(Text, { color: "yellow", children: `preview unavailable: ${props.model.unavailableReason}` })) : (_jsxs(Box, { flexDirection: "column", children: [props.model.lines.map((line, index) => (_jsx(Text, { color: diffLineColor(line), children: truncateCells(line || " ", props.width) }, `${index}:${line.slice(0, 16)}`))), props.model.clipped ? _jsx(Text, { color: "gray", children: "... diff preview clipped ..." }) : null] }))] }));
}
function decisionOptions(gateId, preview) {
    void gateId;
    return [
        {
            id: "inspect",
            label: "inspect",
            detail: "D",
            description: "review workspace diff after the edit is retried",
            tone: "brand",
        },
        {
            id: "approve",
            label: "approve once",
            detail: "Enter / Y",
            description: "allow this exact file edit fingerprint",
            tone: "success",
        },
        {
            id: "reject",
            label: "reject",
            detail: "N",
            description: "block this edit and send feedback",
            tone: "error",
        },
    ];
}
function row(key, label, status, tone, detail) {
    return { key, label, status, tone, detail };
}
function riskTone(risk) {
    if (risk === "high")
        return "warning";
    if (risk === "medium")
        return "brand";
    return "success";
}
function impactTone(change) {
    if (!change)
        return "muted";
    if (/projection/.test(change))
        return "warning";
    return "success";
}
function operationStatus(preview) {
    if (preview.action === "apply_patch")
        return "patch";
    return preview.operation.includes("new") ? "create" : "overwrite";
}
