import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { parseActionSummary } from "./ActionSummaryBlock.js";
import { truncateCells } from "./design/textLayout.js";
export function BrowserApprovalPreviewBlock(props) {
    const model = browserApprovalPreviewModel(props.summary);
    if (!model)
        return null;
    const width = props.width ?? 96;
    return (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsxs(Text, { children: [_jsx(Text, { color: riskColor(model.risk), children: model.title }), " ", _jsx(Text, { color: "gray", children: model.risk })] }), _jsx(PreviewRow, { label: "url", value: model.url, width: width }), _jsx(PreviewRow, { label: model.targetLabel, value: model.target, width: width }), _jsx(PreviewRow, { label: "note", value: model.note, width: width, color: "gray" })] }));
}
export function browserApprovalPreviewModel(summary) {
    const parsed = parseActionSummary(summary);
    if (!parsed || !parsed.action.startsWith("browser_"))
        return null;
    const fields = Object.fromEntries(parsed.fields.map((field) => [field.key, field.value]));
    const url = fields.url || "(unknown url)";
    if (parsed.action === "browser_screenshot") {
        return {
            action: parsed.action,
            title: "Browser screenshot",
            url,
            targetLabel: "path",
            target: fields.path || "(unknown path)",
            risk: "medium",
            note: fields.fullPage === "true" ? "captures a full-page screenshot artifact" : "captures the current viewport",
        };
    }
    if (parsed.action === "browser_click") {
        return {
            action: parsed.action,
            title: "Browser click",
            url,
            targetLabel: "selector",
            target: fields.selector || "(unknown selector)",
            risk: "high",
            note: "may trigger navigation, form actions, purchases, or other state changes",
        };
    }
    if (parsed.action === "browser_type") {
        return {
            action: parsed.action,
            title: "Browser typing",
            url,
            targetLabel: "selector",
            target: fields.selector || "(unknown selector)",
            risk: "high",
            note: `typed text is redacted from the gate summary; ${fields.textChars || "0"} chars`,
        };
    }
    if (parsed.action === "browser_session_start") {
        return {
            action: parsed.action,
            title: "Browser session",
            url,
            targetLabel: "visible",
            target: fields.visible || "true",
            risk: "medium",
            note: "opens or attaches a browser session",
        };
    }
    return null;
}
function PreviewRow(props) {
    return (_jsxs(Box, { flexDirection: "row", children: [_jsxs(Text, { color: "gray", children: [props.label.padEnd(9), " "] }), _jsx(Text, { color: props.color ?? "gray", children: truncateCells(props.value, Math.max(16, props.width - 12)) })] }));
}
function riskColor(risk) {
    if (risk === "high")
        return "yellow";
    if (risk === "low")
        return "green";
    return "cyan";
}
