import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { StatusBadge } from "./design/StatusBadge.js";
import { truncateCells } from "./design/textLayout.js";
export function ValidationErrorsList(props) {
    const model = validationErrorsListModel(props.errors);
    if (model.count === 0)
        return null;
    const width = props.width ?? 96;
    const title = props.title ?? "validation details";
    return (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsxs(Box, { flexDirection: "row", children: [_jsxs(Text, { color: "gray", children: [title, " "] }), _jsx(StatusBadge, { label: `${model.count}`, tone: model.groups.some((group) => group.rows.some((row) => row.severity === "error")) ? "error" : "warning" })] }), model.groups.map((group) => (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsx(Text, { color: "cyan", children: truncateCells(group.file, Math.max(16, width - 4)) }), group.rows.map((row) => (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Box, { flexDirection: "row", children: [_jsx(Text, { color: "gray", children: "  " }), _jsx(StatusBadge, { label: row.severity, tone: row.tone }), _jsx(Text, { color: "gray", children: " " }), _jsx(Text, { color: "gray", children: truncateCells(row.path, 24) }), _jsx(Text, { color: row.severity === "error" ? "red" : "yellow", children: truncateCells(` ${row.message}`, Math.max(12, width - 38)) })] }), row.value ? (_jsx(Text, { color: "gray", children: truncateCells(`    value: ${row.value}`, Math.max(16, width - 4)) })) : null, row.suggestion ? (_jsx(Text, { color: "gray", children: truncateCells(`    fix: ${row.suggestion}`, Math.max(16, width - 4)) })) : null, row.docLink ? (_jsx(Text, { color: "gray", children: truncateCells(`    docs: ${row.docLink}`, Math.max(16, width - 4)) })) : null] }, row.key)))] }, group.file)))] }));
}
export function validationErrorsListModel(errors) {
    const groups = new Map();
    for (const error of errors) {
        const file = error.file?.trim() || "(unknown file)";
        const rows = groups.get(file) ?? [];
        rows.push({
            key: `${file}:${error.path ?? ""}:${error.message}`,
            path: error.path?.trim() || "(root)",
            message: error.message.trim() || "(no message)",
            value: formatInvalidValue(error.invalidValue),
            severity: error.severity ?? "error",
            tone: (error.severity ?? "error") === "error" ? "error" : "warning",
            suggestion: error.suggestion?.trim() ?? "",
            docLink: error.docLink?.trim() ?? "",
        });
        groups.set(file, rows);
    }
    return {
        count: errors.length,
        groups: [...groups.entries()]
            .sort(([a], [b]) => compareValidationPath(a, b))
            .map(([file, rows]) => ({
            file,
            rows: rows.sort((a, b) => compareValidationPath(a.path, b.path)),
        })),
    };
}
function compareValidationPath(a, b) {
    const lower = a.toLowerCase().localeCompare(b.toLowerCase());
    return lower !== 0 ? lower : a.localeCompare(b);
}
export function formatInvalidValue(value) {
    if (value === undefined)
        return "";
    if (value === null)
        return "null";
    if (typeof value === "string")
        return JSON.stringify(value);
    if (typeof value === "number" || typeof value === "boolean")
        return String(value);
    try {
        return JSON.stringify(value);
    }
    catch {
        return String(value);
    }
}
