"use client";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useMemo } from "react";
import { cn } from "@stigmer/theme";
import { resolveToolCategory, } from "./tool-categories";
import { FilePathLink } from "./FilePathLink";
import { McpArgsView, McpMetadataRow } from "./McpToolDetail";
import { useSandboxNormalize } from "./SandboxContext";
import { CollapsibleCode, FilePathIcon, formatJson, } from "./tool-rendering-primitives";
/**
 * Unified tool-arguments renderer used by both {@link ApprovalCard}
 * (pre-execution) and {@link ToolCallDetail} (post-execution).
 *
 * Resolves the tool category from `toolName` + `mcpServerSlug`,
 * extracts the relevant primary argument, and dispatches to the
 * appropriate category-specific view:
 *
 * - **Shell** — terminal-style command block
 * - **File (read/write/edit/delete)** — file icon + path + optional content
 * - **Search / List** — pattern display
 * - **MCP** — metadata row + scalar key-value grid + collapsible JSON
 * - **Generic / Unknown** — formatted JSON args
 *
 * @example
 * ```tsx
 * // In approval card (from argsPreview string):
 * const args = JSON.parse(pendingApproval.argsPreview);
 * <ToolArgsView toolName={toolName} args={args} mcpServerSlug={slug} />
 *
 * // In detail view (from ToolCall):
 * <ToolArgsView toolName={tc.name} args={tc.args} mcpServerSlug={tc.mcpServerSlug} />
 * ```
 */
export function ToolArgsView({ toolName, args, mcpServerSlug, className, }) {
    const categoryInfo = useMemo(() => resolveToolCategory(toolName, mcpServerSlug), [toolName, mcpServerSlug]);
    const primaryArg = useMemo(() => extractPrimaryArgValue(args, categoryInfo), [args, categoryInfo]);
    return (_jsx("div", { className: cn("text-xs", className), children: _jsx(CategoryArgsDispatch, { category: categoryInfo.category, toolName: toolName, args: args, primaryArg: primaryArg, mcpServerSlug: mcpServerSlug }) }));
}
// ---------------------------------------------------------------------------
// Dispatch
// ---------------------------------------------------------------------------
function CategoryArgsDispatch({ category, toolName, args, primaryArg, mcpServerSlug, }) {
    switch (category) {
        case "shell":
            return primaryArg ? _jsx(ShellArgsView, { command: primaryArg }) : null;
        case "read":
        case "write":
        case "edit":
        case "delete":
            return primaryArg ? (_jsx(FileArgsView, { path: primaryArg, category: category, args: args })) : null;
        case "search":
        case "list":
            return primaryArg ? _jsx(SearchArgsView, { pattern: primaryArg }) : null;
        case "mcp":
            return (_jsx(McpArgsPreview, { toolName: toolName, args: args, mcpServerSlug: mcpServerSlug ?? "" }));
        default:
            return args && Object.keys(args).length > 0 ? (_jsx(GenericArgsView, { args: args })) : null;
    }
}
// ---------------------------------------------------------------------------
// Category-specific views
// ---------------------------------------------------------------------------
function ShellArgsView({ command }) {
    const normalize = useSandboxNormalize();
    return (_jsx("div", { className: "rounded-md border border-border bg-[var(--stgm-terminal-bg,#1a1a2e)] p-2.5", children: _jsxs("pre", { className: "whitespace-pre-wrap break-words font-mono text-xs text-[var(--stgm-terminal-fg,#e0e0e0)]", children: [_jsxs("span", { className: "select-none text-[var(--stgm-terminal-prompt,#6b7280)]", children: ["$", " "] }), normalize(command)] }) }));
}
function FileArgsView({ path, category, args, }) {
    const writeContent = useMemo(() => {
        if (category !== "write" && category !== "edit")
            return null;
        if (!args)
            return null;
        return extractWriteContentFromArgs(args);
    }, [category, args]);
    return (_jsxs("div", { className: "space-y-1.5", children: [_jsxs("div", { className: "flex items-center gap-1.5 text-xs", children: [_jsx(FilePathIcon, {}), _jsx(FilePathLink, { path: path, className: "text-xs" })] }), writeContent && (_jsx(CollapsibleCode, { label: "Content", content: writeContent }))] }));
}
function SearchArgsView({ pattern }) {
    return (_jsxs("div", { className: "flex items-center gap-1.5 text-xs", children: [_jsx("span", { className: "text-muted-foreground", children: "Pattern:" }), _jsx("code", { className: "rounded bg-muted px-1.5 py-0.5 font-mono text-foreground", children: pattern })] }));
}
function McpArgsPreview({ toolName, args, mcpServerSlug, }) {
    return (_jsxs("div", { className: "space-y-2", children: [_jsx(McpMetadataRow, { mcpServerSlug: mcpServerSlug, toolName: toolName, duration: null }), args && Object.keys(args).length > 0 && _jsx(McpArgsView, { args: args })] }));
}
function GenericArgsView({ args }) {
    return (_jsx(CollapsibleCode, { label: "Arguments", content: formatJson(args) }));
}
// ---------------------------------------------------------------------------
// Utilities
// ---------------------------------------------------------------------------
const WRITE_CONTENT_FIELDS = [
    "contents",
    "content",
    "file_content",
    "new_text",
    "new_string",
    "replacement",
];
function extractWriteContentFromArgs(args) {
    for (const field of WRITE_CONTENT_FIELDS) {
        const val = args[field];
        if (typeof val === "string" && val.length > 0)
            return val;
    }
    return null;
}
function extractPrimaryArgValue(args, info) {
    if (!args)
        return null;
    const tryField = (field) => {
        const val = args[field];
        if (typeof val === "string" && val.length > 0)
            return val;
        return null;
    };
    if (info.primaryArgField) {
        const v = tryField(info.primaryArgField);
        if (v)
            return v;
    }
    for (const fb of info.fallbackArgFields) {
        const v = tryField(fb);
        if (v)
            return v;
    }
    if (info.category === "unknown" || info.category === "mcp") {
        const keys = Object.keys(args);
        if (keys.length > 0) {
            const val = args[keys[0]];
            if (typeof val === "string")
                return val;
        }
    }
    return null;
}
//# sourceMappingURL=ToolArgsView.js.map