"use strict";
/**
 * Claude launch argument helpers for third-party WebSearch.
 *
 * Uses the same prompt injection mode as the user to avoid mixing
 * `--append-system-prompt` and `--append-system-prompt-file` in one request.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.appendThirdPartyWebSearchToolArgs = exports.THIRD_PARTY_WEBSEARCH_STEERING_PROMPT = void 0;
const claude_tool_args_1 = require("../claude-tool-args");
const prompt_injection_strategy_1 = require("../prompt-injection-strategy");
const claude_subcommand_detector_1 = require("../claude-subcommand-detector");
const NATIVE_WEBSEARCH_TOOL = 'WebSearch';
const DISALLOWED_TOOLS_FLAG = '--disallowedTools';
exports.THIRD_PARTY_WEBSEARCH_STEERING_PROMPT = {
    name: 'ccs-prompt-websearch-tool',
    content: 'For web lookup or current-information requests, prefer the CCS MCP tool WebSearch instead of Bash/curl/http fetches. If the user explicitly wants shell commands, or WebSearch is unavailable or fails, you may fall back to Bash/network tools.',
};
function parseToolValue(rawValue) {
    return rawValue
        .split(',')
        .map((value) => value.trim())
        .filter((value) => value.length > 0);
}
function mergeToolValues(rawValues, toolName) {
    const merged = rawValues.flatMap(parseToolValue);
    if (!merged.includes(toolName)) {
        merged.push(toolName);
    }
    return merged.join(',');
}
function hasToolInFlag(args, flag, toolName) {
    for (let index = 0; index < args.length; index += 1) {
        const arg = args[index];
        if (arg === flag) {
            const value = (0, claude_tool_args_1.getImmediateFlagValue)(args, index);
            if (value && parseToolValue(value).includes(toolName)) {
                return true;
            }
            continue;
        }
        if (!arg.startsWith(`${flag}=`)) {
            continue;
        }
        const rawValue = arg.slice(flag.length + 1);
        if (parseToolValue(rawValue).includes(toolName)) {
            return true;
        }
    }
    return false;
}
function ensureDisallowedNativeWebSearchTool(args) {
    const { optionArgs, trailingArgs } = (0, claude_tool_args_1.splitArgsAtTerminator)(args);
    if (hasToolInFlag(optionArgs, DISALLOWED_TOOLS_FLAG, NATIVE_WEBSEARCH_TOOL)) {
        return args;
    }
    for (let index = 0; index < optionArgs.length; index += 1) {
        const arg = optionArgs[index];
        if (arg === DISALLOWED_TOOLS_FLAG) {
            const currentValue = (0, claude_tool_args_1.getImmediateFlagValue)(optionArgs, index);
            const mergedValue = mergeToolValues(currentValue ? [currentValue] : [], NATIVE_WEBSEARCH_TOOL);
            return [
                ...optionArgs.slice(0, index + 1),
                mergedValue,
                ...optionArgs.slice(currentValue === null ? index + 1 : index + 2),
                ...trailingArgs,
            ];
        }
        if (arg.startsWith(`${DISALLOWED_TOOLS_FLAG}=`)) {
            const rawValue = arg.slice(DISALLOWED_TOOLS_FLAG.length + 1);
            return [
                ...optionArgs.slice(0, index),
                `${DISALLOWED_TOOLS_FLAG}=${mergeToolValues([rawValue], NATIVE_WEBSEARCH_TOOL)}`,
                ...optionArgs.slice(index + 1),
                ...trailingArgs,
            ];
        }
    }
    return [...optionArgs, DISALLOWED_TOOLS_FLAG, NATIVE_WEBSEARCH_TOOL, ...trailingArgs];
}
function ensureWebSearchSteeringPrompt(args) {
    const { optionArgs, trailingArgs } = (0, claude_tool_args_1.splitArgsAtTerminator)(args);
    if ((0, claude_tool_args_1.hasExactFlagValue)(optionArgs, prompt_injection_strategy_1.PROMPT_FLAG_INLINE, exports.THIRD_PARTY_WEBSEARCH_STEERING_PROMPT.content)) {
        return args;
    }
    if ((0, prompt_injection_strategy_1.hasManagedPromptFileArg)({
        args: optionArgs,
        promptName: exports.THIRD_PARTY_WEBSEARCH_STEERING_PROMPT.name,
    })) {
        return args;
    }
    const steeringArgs = (0, prompt_injection_strategy_1.buildSteeringArg)({
        args: optionArgs,
        promptName: exports.THIRD_PARTY_WEBSEARCH_STEERING_PROMPT.name,
        promptContent: exports.THIRD_PARTY_WEBSEARCH_STEERING_PROMPT.content,
    });
    return [...optionArgs, ...steeringArgs, ...trailingArgs];
}
function appendThirdPartyWebSearchToolArgs(args) {
    // Claude subcommands (agents, doctor, mcp, ...) reject top-level session flags
    // like `--append-system-prompt` and `--disallowedTools`. Issue #1218.
    if ((0, claude_subcommand_detector_1.isClaudeSubcommandInvocation)(args))
        return args;
    return ensureWebSearchSteeringPrompt(ensureDisallowedNativeWebSearchTool(args));
}
exports.appendThirdPartyWebSearchToolArgs = appendThirdPartyWebSearchToolArgs;
//# sourceMappingURL=claude-tool-args.js.map