import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { HookResultBlock, parseHookResultMessage } from "./HookResultBlock.js";
import { MessageResponse } from "./MessageResponse.js";
import { Markdown } from "./Markdown.js";
export function SystemTextMessage(props) {
    if (parseHookResultMessage(props.text)) {
        return _jsx(HookResultBlock, { message: props.text });
    }
    const kind = classifySystemText(props.text);
    const text = formatSystemText(props.text);
    if (shouldPreserveSystemTextLayout(props.text)) {
        return (_jsx(Box, { flexDirection: "column", children: text.split(/\r?\n/).map((line, index) => (_jsx(Text, { dimColor: true, children: line || " " }, `${index}-${line.slice(0, 12)}`))) }));
    }
    if (kind === "info") {
        return _jsx(Text, { dimColor: true, children: text || " " });
    }
    return (_jsx(MessageResponse, { tone: systemTextTone(kind), children: _jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: systemTextColor(kind), children: systemTextTitle(kind) }), _jsx(Markdown, { dimColor: true, children: text })] }) }));
}
export function classifySystemText(text) {
    const normalized = formatSystemText(text);
    if (/^usage:/i.test(normalized))
        return "usage";
    if (/^unknown command\b/i.test(normalized))
        return "unknown-command";
    if (/\b(provider missing|approval required|permission|paused|blocked)\b/i.test(normalized))
        return "warning";
    if (/\b(error|failed|missing api key|invalid api key|denied)\b/i.test(normalized))
        return "error";
    return "info";
}
export function formatSystemText(text) {
    if (shouldPreserveSystemTextLayout(text))
        return text.trimEnd();
    return text.replace(/\s+/g, " ").trim();
}
function shouldPreserveSystemTextLayout(text) {
    return text.includes("OpenClaw 微信登录二维码") ||
        text.includes("OpenClaw WeChat login QR") ||
        text.includes("OpenClaw WeChat browser login") ||
        text.includes("OpenClaw WeChat QR image login") ||
        text.includes("liteapp.weixin.qq.com/q/") ||
        text.includes("qrcode=");
}
function systemTextTitle(kind) {
    if (kind === "usage")
        return "usage";
    if (kind === "unknown-command")
        return "unknown command";
    if (kind === "warning")
        return "attention";
    if (kind === "error")
        return "error";
    return "system";
}
function systemTextTone(kind) {
    if (kind === "error")
        return "error";
    if (kind === "warning" || kind === "usage" || kind === "unknown-command")
        return "warning";
    return "default";
}
function systemTextColor(kind) {
    if (kind === "error")
        return "red";
    if (kind === "warning" || kind === "usage" || kind === "unknown-command")
        return "yellow";
    return "gray";
}
