import { bridgeToolToChatDefinitionWithNative, chatToolToBridgeDefinitionWithNative, mapBridgeToolsToChatWithNative, mapChatToolsToBridgeWithNative } from '../../router/virtual-router/engine-selection/native-shared-conversion-semantics.js';
export function stringifyArgs(args) {
    if (typeof args === 'string')
        return args;
    try {
        return JSON.stringify(args ?? {});
    }
    catch {
        return String(args);
    }
}
function assertToolMappingNativeAvailable() {
    if (typeof bridgeToolToChatDefinitionWithNative !== 'function' ||
        typeof chatToolToBridgeDefinitionWithNative !== 'function' ||
        typeof mapBridgeToolsToChatWithNative !== 'function' ||
        typeof mapChatToolsToBridgeWithNative !== 'function') {
        throw new Error('[tool-mapping] native bindings unavailable');
    }
}
function resolveSanitizeMode(options, fallback = 'responses') {
    const probe = options?.sanitizeName?.('shell_command') ?? options?.sanitizeName?.('Bash');
    if (typeof probe === 'string' && probe === 'shell_command') {
        return 'anthropic';
    }
    if (typeof probe === 'string' && probe === 'Bash') {
        return 'anthropic_denormalize';
    }
    return fallback;
}
export function bridgeToolToChatDefinition(rawTool, options) {
    if (!rawTool || typeof rawTool !== 'object') {
        return null;
    }
    assertToolMappingNativeAvailable();
    const sanitizeMode = resolveSanitizeMode(options, 'responses');
    const mapped = bridgeToolToChatDefinitionWithNative(rawTool, { sanitizeMode });
    return mapped ?? null;
}
export function mapBridgeToolsToChat(rawTools, options) {
    if (!Array.isArray(rawTools) || rawTools.length === 0) {
        return undefined;
    }
    assertToolMappingNativeAvailable();
    const sanitizeMode = resolveSanitizeMode(options, 'responses');
    const mapped = mapBridgeToolsToChatWithNative(rawTools, { sanitizeMode });
    return mapped.length ? mapped : undefined;
}
export function chatToolToBridgeDefinition(rawTool, options) {
    if (!rawTool || typeof rawTool !== 'object') {
        return null;
    }
    assertToolMappingNativeAvailable();
    const sanitizeMode = resolveSanitizeMode(options, 'responses');
    const mapped = chatToolToBridgeDefinitionWithNative(rawTool, { sanitizeMode });
    return mapped ?? null;
}
export function mapChatToolsToBridge(rawTools, options) {
    if (!Array.isArray(rawTools) || rawTools.length === 0) {
        return undefined;
    }
    assertToolMappingNativeAvailable();
    const sanitizeMode = resolveSanitizeMode(options, 'responses');
    const mapped = mapChatToolsToBridgeWithNative(rawTools, { sanitizeMode });
    return mapped.length ? mapped : undefined;
}
