import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React from "react";
import { Box, Text } from "ink";
import { Byline, joinBylineItems } from "./design/Byline.js";
import { truncateCells } from "./design/textLayout.js";
const MODES = ["list", "detail", "review"];
export function DiffReviewChrome(props) {
    const model = diffReviewChromeModel(props);
    const width = props.width ?? 112;
    return (_jsxs(Box, { flexDirection: "column", children: [model.sources.length > 1 ? (_jsxs(Box, { flexDirection: "row", children: [_jsx(Text, { color: "gray", children: "source " }), model.sources.map((source, index) => (_jsxs(React.Fragment, { children: [index > 0 ? _jsx(Text, { color: "gray", children: " | " }) : null, _jsx(Text, { color: source.active ? "cyan" : "gray", bold: source.active, children: source.active ? `[${truncateCells(source.label, 18)}]` : truncateCells(source.label, 18) })] }, `${index}:${source.label}`)))] })) : null, _jsxs(Box, { flexDirection: "row", children: [_jsx(Text, { color: "gray", children: "mode   " }), model.modes.map((mode, index) => (_jsxs(React.Fragment, { children: [index > 0 ? _jsx(Text, { color: "gray", children: " | " }) : null, _jsx(Text, { color: mode.active ? "cyan" : "gray", bold: mode.active, children: mode.active ? `[${mode.label}]` : mode.label })] }, mode.label)))] }), _jsx(Byline, { children: model.guide.split(" | ").map((item) => (_jsx(Text, { color: "gray", children: truncateCells(item, Math.max(12, Math.floor(width / 3))) }, item))) })] }));
}
export function diffReviewChromeModel(input) {
    const rawSources = input.sources?.length ? input.sources : [{ label: "workspace" }];
    const selected = clampIndex(input.selectedSourceIndex ?? 0, rawSources.length);
    const mode = input.mode ?? "review";
    const guide = mode === "detail"
        ? joinBylineItems(["Esc list", "q close", "Up/Down hunk"])
        : joinBylineItems([
            rawSources.length > 1 ? "Left/Right source" : "",
            "Tab mode",
            "Enter detail",
            "Esc close",
        ]);
    return {
        sources: rawSources.map((source, index) => ({
            label: source.detail ? `${source.label} ${source.detail}` : source.label,
            active: index === selected,
        })),
        modes: MODES.map((label) => ({
            label,
            active: label === mode,
        })),
        guide,
    };
}
function clampIndex(index, length) {
    if (length <= 0)
        return 0;
    return Math.min(length - 1, Math.max(0, Math.trunc(index)));
}
