import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
import { Box, Text } from 'ink';
import React from 'react';
import { useTerminalWidth } from '../hooks/useTerminalWidth.js';
import { useTheme } from '../hooks/useTheme.js';
import { calculateTokens } from '../utils/token-calculator.js';
const MAX_VALUE_PREVIEW = 80;
function previewValue(value) {
    if (value === null || value === undefined)
        return '';
    if (typeof value === 'string') {
        return value.length > MAX_VALUE_PREVIEW
            ? `${value.slice(0, MAX_VALUE_PREVIEW)}…`
            : value;
    }
    if (Array.isArray(value))
        return `[${value.length} items]`;
    if (typeof value === 'object')
        return '[object]';
    return String(value);
}
function CustomToolFormatterComponent({ toolName, args, result, }) {
    const boxWidth = useTerminalWidth();
    const { colors } = useTheme();
    const argEntries = Object.entries(args ?? {}).filter(([, v]) => v !== undefined && v !== null && v !== '');
    const tokens = result ? calculateTokens(result) : 0;
    return (_jsxs(Box, { flexDirection: "column", marginBottom: 1, width: boxWidth, children: [_jsxs(Text, { color: colors.tool, children: ["\u2692 ", toolName] }), argEntries.length > 0 && (_jsx(Box, { flexDirection: "column", children: argEntries.map(([k, v]) => (_jsxs(Box, { children: [_jsxs(Text, { color: colors.secondary, children: [k, ": "] }), _jsx(Box, { marginLeft: 1, flexShrink: 1, children: _jsx(Text, { wrap: "truncate-end", color: colors.text, children: previewValue(v) }) })] }, k))) })), result !== undefined && (_jsxs(Box, { children: [_jsx(Text, { color: colors.secondary, children: "Output: " }), _jsxs(Text, { color: colors.text, children: ["~", tokens, " tokens"] })] }))] }));
}
/**
 * Default formatter shared by all shell-bodied custom tools.
 * Renders the tool name, the provided args, and (after execution) a token
 * estimate of the output. Matches the visual style of `webSearchFormatter`.
 */
export function makeCustomToolFormatter(toolName) {
    return (args, result) => React.createElement(CustomToolFormatterComponent, {
        toolName,
        args: args ?? {},
        result,
    });
}
//# sourceMappingURL=formatter.js.map