import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { Box, Text, useInput } from 'ink';
import SelectInput from 'ink-select-input';
import React from 'react';
import { RenderErrorBoundary } from '../components/render-error-boundary.js';
import { useTerminalWidth } from '../hooks/useTerminalWidth.js';
import { useTheme } from '../hooks/useTheme.js';
import { getToolManager } from '../message-handler.js';
import { formatError } from '../utils/error-formatter.js';
import { getLogger } from '../utils/logging/index.js';
import { getToolJsonSchema, validateArgsAgainstSchema, } from '../utils/schema-validate.js';
import { parseToolArguments } from '../utils/tool-args-parser.js';
import { formatValidationError } from '../utils/tool-validation.js';
export default function ToolConfirmation({ toolCall, onConfirm, onCancel, }) {
    const boxWidth = useTerminalWidth();
    const { colors } = useTheme();
    const [formatterPreview, setFormatterPreview] = React.useState(null);
    const [isLoadingPreview, setIsLoadingPreview] = React.useState(false);
    const [hasFormatterError, setHasFormatterError] = React.useState(false);
    const [hasValidationError, setHasValidationError] = React.useState(false);
    const [_validationError, setValidationError] = React.useState(null);
    // Get MCP tool info for display
    const toolManager = getToolManager();
    const mcpInfo = toolManager?.getMCPToolInfo(toolCall.function.name) || {
        isMCPTool: false,
    };
    // Load formatter preview
    React.useEffect(() => {
        const loadPreview = async () => {
            if (!toolManager)
                return;
            let parsedArgs = {};
            try {
                parsedArgs = parseToolArguments(toolCall.function.arguments);
            }
            catch {
                // Leave as {} — the schema/validator will surface the real problem.
            }
            // 1. Schema type-check — the same check the executed handler runs, so a
            // wrong-typed argument shows red here and is fed back to the model.
            const schema = getToolJsonSchema(toolManager.getToolEntry(toolCall.function.name)?.tool);
            const typeErrors = schema
                ? validateArgsAgainstSchema(parsedArgs, schema)
                : [];
            if (typeErrors.length > 0) {
                const msg = formatValidationError('one or more arguments have the wrong type — fix the types and call the tool again', typeErrors);
                setValidationError(msg);
                setHasValidationError(true);
                setFormatterPreview(_jsx(Text, { color: colors.error, children: msg }));
                return;
            }
            // 2. Per-tool validator.
            const validator = toolManager.getToolValidator(toolCall.function.name);
            if (validator) {
                try {
                    const validationResult = await validator(parsedArgs);
                    if (!validationResult.valid) {
                        setValidationError(validationResult.error);
                        setHasValidationError(true);
                        setFormatterPreview(_jsx(Text, { color: colors.error, children: validationResult.error }));
                        return;
                    }
                }
                catch (error) {
                    const logger = getLogger();
                    logger.error({ error: formatError(error) }, 'Error running validator');
                    const errorMsg = `Validation error: ${formatError(error)}`;
                    setValidationError(errorMsg);
                    setHasValidationError(true);
                    setFormatterPreview(_jsx(Text, { color: colors.error, children: errorMsg }));
                    return;
                }
            }
            // 3. Formatter preview.
            const formatter = toolManager.getToolFormatter(toolCall.function.name);
            if (formatter) {
                setIsLoadingPreview(true);
                try {
                    const preview = await formatter(parsedArgs);
                    setFormatterPreview(preview);
                }
                catch (error) {
                    const logger = getLogger();
                    logger.error({ error: formatError(error) }, 'Error loading formatter preview');
                    setHasFormatterError(true);
                    setFormatterPreview(_jsxs(Text, { color: colors.error, children: ["Error: ", String(error)] }));
                }
                finally {
                    setIsLoadingPreview(false);
                }
            }
        };
        void loadPreview();
    }, [toolCall, toolManager, colors.error]);
    // Handle escape key to cancel
    useInput((_inputChar, key) => {
        if (key.escape) {
            onCancel();
        }
    });
    // Auto-handle errors without user interaction
    React.useEffect(() => {
        if (hasFormatterError && !hasValidationError) {
            // Automatically cancel the tool execution only for formatter crashes
            onConfirm(false);
        }
        if (hasValidationError) {
            // Automatically proceed to execution phase where the validator
            // will fail again and pass the error back to the model to correct
            onConfirm(true);
        }
    }, [hasFormatterError, hasValidationError, onConfirm]);
    const options = [
        { label: '✓ Yes, execute this tool', value: true },
        { label: '✗ No, cancel execution', value: false },
    ];
    const handleSelect = (item) => {
        onConfirm(item.value);
    };
    return (_jsx(Box, { width: boxWidth, marginBottom: 1, children: _jsxs(Box, { flexDirection: "column", children: [isLoadingPreview && (_jsx(Box, { marginBottom: 1, children: _jsx(Text, { color: colors.secondary, children: "Loading preview..." }) })), formatterPreview && !isLoadingPreview && (_jsx(Box, { marginBottom: 1, flexDirection: "column", children: _jsx(Box, { children: React.isValidElement(formatterPreview) ? (_jsx(RenderErrorBoundary, { label: toolCall.function.name, children: formatterPreview })) : (_jsx(Text, { color: colors.text, children: String(formatterPreview) })) }) })), !hasFormatterError && !hasValidationError && (_jsxs(_Fragment, { children: [_jsx(Box, { marginBottom: 1, children: _jsx(Text, { color: colors.tool, children: `Do you want to execute ${mcpInfo.isMCPTool
                                    ? `MCP tool "${toolCall.function.name}" from server "${mcpInfo.serverName}"`
                                    : `tool "${toolCall.function.name}"`}?` }) }), _jsx(SelectInput, { items: options, onSelect: handleSelect }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: colors.secondary, children: "Press Escape to cancel" }) })] })), hasFormatterError && !hasValidationError && (_jsxs(Box, { marginTop: 1, children: [_jsx(Text, { color: colors.error, children: "Tool execution cancelled due to formatter error." }), _jsx(Text, { color: colors.secondary, children: "Press Escape to continue" })] }))] }) }));
}
//# sourceMappingURL=tool-confirmation.js.map