import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { Pane } from "./design/Pane.js";
import { StatusBadge } from "./design/StatusBadge.js";
import { truncateCells } from "./design/textLayout.js";
export function KeybindingWarnings(props) {
    const model = keybindingWarningsModel(props);
    if (!model.visible)
        return null;
    const width = props.width ?? 96;
    return (_jsx(Box, { flexDirection: "column", marginTop: 1, marginBottom: 1, children: _jsxs(Pane, { width: width, title: "keybindings", tone: model.tone, paddingX: 1, children: [_jsxs(Box, { flexDirection: "row", justifyContent: "space-between", children: [_jsx(Text, { color: model.tone === "error" ? "red" : "yellow", bold: true, children: model.title }), _jsx(StatusBadge, { label: model.statusLabel, tone: model.tone })] }), _jsx(Text, { color: "gray", children: truncateCells(model.path, Math.max(16, width - 4)) }), _jsx(Box, { flexDirection: "column", marginTop: 1, children: model.rows.map((row) => (_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsxs(Text, { children: [_jsx(StatusBadge, { label: row.label, tone: row.tone }), _jsx(Text, { color: "gray", children: ` ${truncateCells(row.message, Math.max(16, width - 14))}` })] }), row.suggestion ? (_jsx(Text, { color: "gray", children: truncateCells(`  -> ${row.suggestion}`, Math.max(16, width - 4)) })) : null] }, row.key))) })] }) }));
}
export function keybindingWarningsModel(input) {
    const enabled = input.enabled ?? true;
    const rows = input.warnings.map((warning, index) => ({
        key: `${warning.severity}-${index}`,
        severity: warning.severity,
        label: warning.severity === "error" ? "Error" : "Warning",
        message: flattenWarningText(warning.message),
        suggestion: flattenWarningText(warning.suggestion ?? ""),
        tone: warning.severity === "error" ? "error" : "warning",
    }));
    const errorCount = rows.filter((row) => row.severity === "error").length;
    return {
        visible: enabled && rows.length > 0,
        title: "Keybinding configuration issues",
        path: input.path,
        rows,
        statusLabel: errorCount > 0 ? `${errorCount} error` : `${rows.length} warning`,
        tone: errorCount > 0 ? "error" : "warning",
    };
}
function flattenWarningText(value) {
    return value.replace(/\s+/g, " ").trim();
}
