import { isShellLikeToolNameTokenWithNative } from '../../../../../../../router/virtual-router/engine-selection/native-hub-pipeline-req-inbound-semantics.js';
const SHELL_LIKE_TOOL_NAMES = new Set(['exec_command', 'shell_command', 'shell', 'bash', 'terminal']);
function readTrimmedString(value) {
    if (typeof value !== 'string') {
        return undefined;
    }
    const trimmed = value.trim();
    return trimmed.length ? trimmed : undefined;
}
function isRecord(value) {
    return !!value && typeof value === 'object' && !Array.isArray(value);
}
function readStringArrayCommand(value) {
    if (!Array.isArray(value)) {
        return undefined;
    }
    const tokens = value
        .map((item) => (item == null ? '' : String(item).trim()))
        .filter((item) => item.length > 0);
    return tokens.length ? tokens.join(' ') : undefined;
}
function parseJsonRecord(value) {
    if (isRecord(value)) {
        return value;
    }
    if (typeof value !== 'string') {
        return undefined;
    }
    const trimmed = value.trim();
    if (!trimmed) {
        return {};
    }
    try {
        const parsed = JSON.parse(trimmed);
        return isRecord(parsed) ? parsed : undefined;
    }
    catch {
        return undefined;
    }
}
function readCommandFromArgs(args) {
    const input = isRecord(args.input) ? args.input : undefined;
    const direct = readTrimmedString(args.cmd) ??
        readTrimmedString(args.command) ??
        readTrimmedString(args.script) ??
        readTrimmedString(args.toon) ??
        readStringArrayCommand(args.cmd) ??
        readStringArrayCommand(args.command);
    if (direct) {
        return direct;
    }
    if (!input) {
        return undefined;
    }
    return (readTrimmedString(input.cmd) ??
        readTrimmedString(input.command) ??
        readTrimmedString(input.script) ??
        readStringArrayCommand(input.cmd) ??
        readStringArrayCommand(input.command));
}
function readWorkdirFromArgs(args) {
    const input = isRecord(args.input) ? args.input : undefined;
    return (readTrimmedString(args.workdir) ??
        readTrimmedString(args.cwd) ??
        readTrimmedString(args.workDir) ??
        readTrimmedString(input?.workdir) ??
        readTrimmedString(input?.cwd));
}
function collectRequestedToolNames(payload) {
    const names = new Set();
    const root = payload;
    const tools = Array.isArray(root.tools) ? root.tools : [];
    for (const tool of tools) {
        if (!isRecord(tool))
            continue;
        const fn = isRecord(tool.function) ? tool.function : undefined;
        const name = readTrimmedString(fn?.name) ?? readTrimmedString(tool.name);
        if (name) {
            names.add(name);
        }
    }
    return names;
}
function resolveShellLikeToolName(rawName, requestedToolNames) {
    if (requestedToolNames.size === 0) {
        return rawName;
    }
    if (requestedToolNames.has(rawName)) {
        return rawName;
    }
    if (requestedToolNames.has('exec_command')) {
        return 'exec_command';
    }
    if (requestedToolNames.has('shell_command')) {
        return 'shell_command';
    }
    return rawName;
}
export function normalizeShellLikeToolCallsBeforeGovernanceFallback(payload) {
    const root = payload;
    const messages = Array.isArray(root.messages) ? root.messages : [];
    if (!messages.length) {
        return;
    }
    const requestedToolNames = collectRequestedToolNames(payload);
    for (const message of messages) {
        if (!isRecord(message))
            continue;
        const role = readTrimmedString(message.role)?.toLowerCase();
        if (role !== 'assistant')
            continue;
        const toolCalls = Array.isArray(message.tool_calls) ? message.tool_calls : [];
        if (!toolCalls.length)
            continue;
        for (const call of toolCalls) {
            if (!isRecord(call))
                continue;
            const fn = isRecord(call.function) ? call.function : undefined;
            if (!fn)
                continue;
            const rawName = readTrimmedString(fn.name);
            if (!rawName)
                continue;
            const isShellLike = isShellLikeToolNameTokenWithNative(rawName);
            if (!isShellLike)
                continue;
            const resolvedName = resolveShellLikeToolName(rawName, requestedToolNames);
            if (resolvedName !== rawName) {
                fn.name = resolvedName;
            }
            const parsedArgs = parseJsonRecord(fn.arguments);
            const args = parsedArgs ?? {};
            const cmd = readCommandFromArgs(args);
            if (!cmd) {
                continue;
            }
            const nextArgs = {
                ...args,
                cmd,
                command: cmd
            };
            const workdir = readWorkdirFromArgs(args);
            if (workdir) {
                nextArgs.workdir = workdir;
            }
            if (Object.prototype.hasOwnProperty.call(nextArgs, 'toon')) {
                delete nextArgs.toon;
            }
            try {
                fn.arguments = JSON.stringify(nextArgs);
            }
            catch {
                fn.arguments = JSON.stringify({ cmd, command: cmd, ...(workdir ? { workdir } : {}) });
            }
        }
    }
}
