import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { summarizeDiff } from "../utils/diff.js";
import { DiffDetailView, parseDiffDetailFiles } from "./DiffDetailView.js";
import { DiffFileList } from "./DiffFileList.js";
import { DiffReviewChrome } from "./DiffReviewChrome.js";
import { DiffSummaryHeader } from "./DiffSummaryHeader.js";
import { truncateCells } from "./design/textLayout.js";
const DEFAULT_MAX_LINES = 220;
const DEFAULT_WIDTH = 112;
export function DiffReviewPanel(props) {
    const width = props.width ?? DEFAULT_WIDTH;
    const model = diffReviewPanelModel({
        diff: props.diff,
        title: props.title,
        subtitle: props.subtitle,
        sourceLabel: props.sourceLabel,
        mode: props.mode,
        selectedFileIndex: props.selectedFileIndex,
    });
    return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Box, { flexDirection: "row", children: [_jsx(Text, { color: "cyan", bold: true, children: truncateCells(model.title, Math.max(20, width - 28)) }), _jsx(Box, { flexGrow: 1 }), _jsx(Text, { color: "gray", children: truncateCells(model.sourceLabel, 24) })] }), model.subtitle ? _jsx(Text, { color: "gray", children: truncateCells(model.subtitle, width) }) : null, _jsxs(Text, { color: "gray", children: [model.modeLabel, " | ", `${model.files} file${model.files === 1 ? "" : "s"}`, " | ", `${model.hunks} hunk${model.hunks === 1 ? "" : "s"}`, " | ", _jsx(Text, { color: "green", children: `+${model.added}` }), " ", _jsx(Text, { color: "red", children: `-${model.removed}` })] }), _jsx(Box, { marginTop: 1, children: _jsx(DiffReviewChrome, { sources: props.sources ?? [{ label: model.sourceLabel }], selectedSourceIndex: props.selectedSourceIndex, mode: model.modeLabel, width: width }) }), _jsx(Box, { marginTop: 1, flexDirection: "column", children: _jsx(DiffReviewContent, { diff: props.diff, model: model, maxLines: props.maxLines ?? DEFAULT_MAX_LINES, width: width }) }), _jsx(Text, { color: "gray", children: contentHint(model) })] }));
}
export function diffReviewPanelModel(input) {
    const summary = summarizeDiff(input.diff);
    const files = parseDiffDetailFiles(input.diff);
    const hunks = files.reduce((total, file) => total + file.hunks.length, 0);
    const selectedFileIndex = files.length > 0 ? clampIndex(input.selectedFileIndex ?? 0, files.length) : 0;
    const mode = input.mode ?? "review";
    return {
        title: input.title?.trim() || "Diff review",
        subtitle: input.subtitle?.trim() || "",
        sourceLabel: input.sourceLabel?.trim() || "workspace",
        files: files.length,
        added: summary.added,
        removed: summary.removed,
        hunks,
        modeLabel: mode,
        selectedFileIndex,
        selectedFilePath: files[selectedFileIndex]?.path ?? "",
        emptyMessage: files.length === 0 ? "Working tree is clean" : "",
    };
}
function DiffReviewContent(props) {
    if (props.model.files === 0) {
        return _jsx(Text, { color: "gray", children: props.model.emptyMessage });
    }
    if (props.model.modeLabel === "list") {
        return (_jsxs(Box, { flexDirection: "column", children: [_jsx(DiffSummaryHeader, { diff: props.diff }), _jsx(DiffFileList, { diff: props.diff, width: props.width, selectedIndex: props.model.selectedFileIndex })] }));
    }
    if (props.model.modeLabel === "detail") {
        return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: "gray", children: `selected ${props.model.selectedFileIndex + 1}/${props.model.files}: ${props.model.selectedFilePath}` }), _jsx(DiffDetailView, { diff: props.diff, selectedFileIndex: props.model.selectedFileIndex, maxLines: props.maxLines, width: props.width })] }));
    }
    return (_jsxs(Box, { flexDirection: "column", children: [_jsx(DiffSummaryHeader, { diff: props.diff }), _jsx(DiffFileList, { diff: props.diff, width: props.width, selectedIndex: props.model.selectedFileIndex }), _jsx(DiffDetailView, { diff: props.diff, maxLines: props.maxLines, width: props.width })] }));
}
function contentHint(model) {
    if (model.files === 0)
        return "preview: no changed files";
    if (model.modeLabel === "list")
        return "list: select a file to inspect";
    if (model.modeLabel === "detail")
        return "detail: selected file hunk view";
    return "review: file list + hunk detail";
}
function clampIndex(index, length) {
    if (length <= 0)
        return 0;
    return Math.min(length - 1, Math.max(0, Math.trunc(index)));
}
