import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { ApprovalResultBlock, parseApprovalResultMessage } from "./ApprovalResultBlock.js";
import { FileEditResultBlock } from "./FileEditResultBlock.js";
import { fileEditResultModel } from "./FileEditResultBlock.js";
import { FileToolResultBlock } from "./FileToolResultBlock.js";
import { parseFileToolResultMessage } from "./FileToolResultBlock.js";
import { Markdown } from "./Markdown.js";
import { ShellResultBlock } from "./ShellResultBlock.js";
import { parseShellResultMessage } from "./ShellResultBlock.js";
import { StructuredDiff, isUnifiedDiff } from "./StructuredDiff.js";
import { toneColor } from "./design/terminalTheme.js";
const KNOWN_STATUSES = new Set([
    "approved",
    "blocked",
    "cancelled",
    "canceled",
    "completed",
    "denied",
    "done",
    "error",
    "failed",
    "ok",
    "paused",
    "rejected",
    "skipped",
    "started",
    "succeeded",
    "warning",
]);
export function ToolResultDetail(props) {
    if (!props.body)
        return null;
    if (isUnifiedDiff(props.body))
        return _jsx(StructuredDiff, { diff: props.body, maxLines: 120 });
    if (parseApprovalResultMessage(props.body))
        return _jsx(ApprovalResultBlock, { message: props.body });
    if (isQuestionAwaitingUserMessage(props.body)) {
        return _jsx(Markdown, { dimColor: true, children: formatQuestionAwaitingUserMessage(props.body) });
    }
    const detail = parseToolResultDetail(props.title, props.body);
    if (!detail)
        return _jsx(Markdown, { dimColor: true, children: props.body });
    return (_jsxs(Box, { flexDirection: "column", children: [_jsx(ToolDetailRow, { label: "action", value: detail.action }), _jsx(ToolDetailRow, { label: "status", value: detail.status, color: toneColor(props.tone) ?? "gray" }), detail.target ? _jsx(ToolDetailRow, { label: "target", value: detail.target }) : null, renderToolMessage(detail, props.tone)] }));
}
export function parseToolResultDetail(title, body = "") {
    const parts = title.trim().split(/\s+/).filter(Boolean);
    if (parts.length < 2)
        return null;
    const [action, rawStatus, ...targetParts] = parts;
    const status = rawStatus.toLowerCase();
    if (!/^[a-zA-Z0-9_.:-]+$/.test(action))
        return null;
    if (!KNOWN_STATUSES.has(status))
        return null;
    const target = normalizeToolResultTarget(action, targetParts.join(" ").trim());
    const message = normalizeToolResultMessage(action, body.trim());
    return {
        action,
        status,
        ...(target ? { target } : {}),
        ...(message ? { message } : {}),
    };
}
function ToolDetailRow(props) {
    return (_jsxs(Box, { flexDirection: "row", children: [_jsxs(Text, { color: "gray", children: [props.label.padEnd(7), " "] }), _jsx(Text, { color: props.color, children: props.value })] }));
}
function renderToolMessage(detail, tone) {
    if (parseApprovalResultMessage(detail.message ?? "")) {
        return _jsx(ApprovalResultBlock, { message: detail.message ?? "" });
    }
    if (fileEditResultModel(detail.action, detail.message ?? "")) {
        return _jsx(FileEditResultBlock, { action: detail.action, message: detail.message });
    }
    if (detail.action === "run_command" || detail.action === "ssh_run") {
        if (parseShellResultMessage(detail.message ?? "")) {
            return _jsx(ShellResultBlock, { message: detail.message ?? "", tone: tone });
        }
    }
    if (parseFileToolResultMessage(detail.action, detail.message ?? "")) {
        return _jsx(FileToolResultBlock, { action: detail.action, message: detail.message ?? "" });
    }
    if (!detail.message)
        return null;
    return _jsx(Markdown, { dimColor: true, children: detail.message ?? "" });
}
export function isQuestionAwaitingUserMessage(message) {
    return /^Question awaiting user answer\./i.test(message.trim());
}
export function formatQuestionAwaitingUserMessage(message) {
    const lines = message
        .split(/\r?\n/)
        .map((line) => line.trimEnd())
        .filter((line) => !/^Question awaiting user answer\./i.test(line.trim()))
        .filter((line) => !/^pending question$/i.test(line.trim()));
    return lines.join("\n").trim() || "Waiting for your answer in the permission panel.";
}
function normalizeToolResultTarget(action, target) {
    if ((action === "EnterPlanMode" || action === "ExitPlanMode") && isInternalPlanPath(target))
        return "";
    return target;
}
function normalizeToolResultMessage(action, message) {
    if (action !== "EnterPlanMode" && action !== "ExitPlanMode")
        return message;
    return message
        .split(/\r?\n/)
        .filter((line) => !/^plan=\.deepseekcode[\\/]+plans[\\/]+/i.test(line.trim()))
        .join("\n")
        .trim();
}
function isInternalPlanPath(value) {
    return /^\.deepseekcode[\\/]+plans[\\/]+run_[^\\/]+\.md$/i.test(value.trim());
}
