import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
const MAX_VALUE_CHARS = 180;
export function ActionSummaryBlock(props) {
    const model = parseActionSummary(props.summary);
    if (!model || model.fields.length === 0)
        return null;
    return (_jsx(Box, { flexDirection: "column", children: model.fields.map((field) => (_jsx(ActionSummaryRow, { label: field.key, value: field.value }, field.key))) }));
}
export function parseActionSummary(summary) {
    const trimmed = summary.trim();
    if (!trimmed)
        return null;
    const [action = "", ...restParts] = trimmed.split(/\s+/);
    if (!action || !/^[a-zA-Z0-9_.:-]+$/.test(action))
        return null;
    const rest = restParts.join(" ");
    const matches = [...rest.matchAll(/(?:^|\s)([a-zA-Z][a-zA-Z0-9_]*?)=/g)];
    if (matches.length === 0)
        return { action, fields: [] };
    const fields = matches.map((match, index) => {
        const key = match[1] ?? "";
        const valueStart = (match.index ?? 0) + match[0].length;
        const next = matches[index + 1];
        const valueEnd = next?.index ?? rest.length;
        return {
            key,
            value: compactFieldValue(rest.slice(valueStart, valueEnd).trim()),
        };
    });
    return { action, fields };
}
function compactFieldValue(value) {
    const singleLine = value.replace(/\s+/g, " ").trim();
    return singleLine.length > MAX_VALUE_CHARS
        ? `${singleLine.slice(0, MAX_VALUE_CHARS - 3)}...`
        : singleLine;
}
function ActionSummaryRow(props) {
    return (_jsxs(Box, { flexDirection: "row", children: [_jsxs(Text, { color: "gray", children: [props.label.padEnd(9), " "] }), _jsx(Text, { color: "gray", children: props.value || "-" })] }));
}
