import { isJsonObject } from '../types/json.js';
import { buildAnthropicToolAliasMapWithNative } from '../../../router/virtual-router/engine-selection/native-chat-process-governance-semantics.js';
export function applyAnthropicToolAliasSemantics(request, entryEndpoint) {
    try {
        const endpoint = typeof entryEndpoint === 'string' ? entryEndpoint.trim().toLowerCase() : '';
        if (!endpoint.includes('/v1/messages')) {
            return request;
        }
        request.semantics = asObjectRecord(request.semantics);
        const semantics = request.semantics;
        const semanticsTools = asObjectRecord(semantics.tools);
        semantics.tools = semanticsTools;
        const hasAlias = isJsonObject(semanticsTools.toolNameAliasMap) ||
            isJsonObject(semanticsTools.toolAliasMap);
        if (hasAlias) {
            return request;
        }
        const sourceTools = Array.isArray(semanticsTools.clientToolsRaw) && semanticsTools.clientToolsRaw.length
            ? semanticsTools.clientToolsRaw
            : request.tools;
        const aliasMap = buildAnthropicToolAliasMapWithNative(sourceTools);
        if (aliasMap) {
            semanticsTools.toolNameAliasMap = aliasMap;
        }
    }
    catch {
        // best-effort: alias-map propagation must never block request handling
    }
    return request;
}
function asObjectRecord(value) {
    if (value && typeof value === 'object' && !Array.isArray(value)) {
        return value;
    }
    return {};
}
