import { WEB_SEARCH_TOOL_NAME } from "../adapter/activation/tool-set.js";
import { registerApplyPatchTool } from "../tools/apply-patch/tool.js";
import { registerExecCommandTool } from "../tools/exec/command-tool.js";
import { registerWriteStdinTool } from "../tools/exec/write-stdin-tool.js";
import { registerImageGenerationTool } from "../tools/imagegen/tool.js";
import { registerViewImageTool } from "../tools/view-image/tool.js";
import { registerWebSearchTool } from "../tools/web-run/tool.js";
export function isExplicitlyConfiguredToolProvider(model, config) {
    const provider = model?.provider?.trim().toLowerCase();
    return Boolean(provider && config.scope.additionalProviders.some((entry) => entry.trim().toLowerCase() === provider));
}
export function registerCodexTools(pi, runtime) {
    const renderOptions = (config) => ({ customRendering: config.ui.toolRenaming });
    const promptOptions = (config) => ({ promptSnippet: config.mode === "path" });
    const registerCore = (config) => {
        registerApplyPatchTool(pi, { ...promptOptions(config), showDiffWhenCollapsed: config.mode === "normal" && !config.ui.compactTools });
        registerExecCommandTool(pi, runtime.tracker, runtime.sessions, {
            describeImagesForTextModels: config.tools.viewImageFallback,
            ...renderOptions(config),
            ...promptOptions(config),
            showOutputWhenCollapsed: config.mode === "normal",
            compactTools: config.ui.compactTools,
        });
        registerWriteStdinTool(pi, runtime.sessions, { describeImagesForTextModels: config.tools.viewImageFallback, ...promptOptions(config) });
        registerViewImageTool(pi, { describeForTextModels: config.tools.viewImageFallback, ...renderOptions(config), ...promptOptions(config) });
    };
    const ensureOptionalTools = (config = runtime.state.config) => {
        const allowConfiguredProvider = (model) => isExplicitlyConfiguredToolProvider(model, config);
        if (config.tools.webRun || config.tools.webRunOnly) {
            registerWebSearchTool(pi, WEB_SEARCH_TOOL_NAME, {
                getRecentInput: () => runtime.latestRecentWebSearchInput,
                model: () => runtime.state.config.openai.webSearchModel,
                allowConfiguredProvider,
                ...renderOptions(config),
                ...promptOptions(config),
            });
            runtime.registeredNativeWebSearchTools.add(WEB_SEARCH_TOOL_NAME);
        }
        if (config.tools.imageGeneration || config.tools.imageGenerationOnly) {
            registerImageGenerationTool(pi, { allowConfiguredProvider, ...renderOptions(config), ...promptOptions(config) });
        }
    };
    registerCore(runtime.state.config);
    ensureOptionalTools();
    return {
        ensureOptionalTools,
        applyConfig(config) {
            registerCore(config);
            ensureOptionalTools(config);
            runtime.sessions.setBaseEnv(runtime.bundledPathToolsEnv(config));
        },
    };
}
