import path from "node:path";
import { ToolRegistry, createReadFileTool, createWriteFileTool, createEditFileTool, createSearchTool, createListFilesTool, createRunCommandTool, } from "@sean.holung/minicode-sdk";
import { createFindReferencesTool } from "./find-references.js";
import { createGetDependenciesTool } from "./get-dependencies.js";
import { createReadSymbolTool } from "./read-symbol.js";
import { createFindPathTool } from "./find-path.js";
import { createSearchCodeMapTool } from "./search-code-map.js";
import { buildPostEditDiagnostic } from "./post-edit-diagnostics.js";
export { ToolRegistry };
async function reindexBestEffort(projectIndex, relPath, content) {
    try {
        await projectIndex.reindexFile(relPath, content);
    }
    catch {
        // Best effort only: the file mutation already succeeded.
    }
}
/**
 * Create a ToolRegistry with the SDK's core tools plus indexer-specific tools
 * when a ProjectIndex is available.
 *
 * Set `MINICODE_TOOL_PROFILE=file-search-only` (or pass it via the env to a
 * benchmark run) to omit the five graph-aware tools (`read_symbol`,
 * `find_references`, `get_dependencies`, `find_path`, `search_code_map`).
 * The code map remains in the system prompt — only the interactive tools
 * are gated. This is the right shape for an `all-tools` vs.
 * `file-search-only` ablation per `benchmarks/STRATEGY.md`.
 */
export function createToolRegistry(config, projectIndex) {
    const hooks = projectIndex
        ? {
            afterWrite: (relPath, content) => reindexBestEffort(projectIndex, relPath, content),
            afterEdit: async (absPath, content) => {
                const relPath = path.relative(config.workspaceRoot, absPath);
                await reindexBestEffort(projectIndex, relPath, content);
                return await buildPostEditDiagnostic(absPath, config.workspaceRoot);
            },
            afterCommand: async () => {
                await projectIndex.refreshFromWorkspace();
            },
        }
        : undefined;
    const tools = [
        createReadFileTool(config),
        createWriteFileTool(config, hooks ? { afterWrite: hooks.afterWrite } : undefined),
        createEditFileTool(config, hooks ? { afterEdit: hooks.afterEdit } : undefined),
        createSearchTool(config),
        createListFilesTool(config),
        createRunCommandTool(config, hooks ? { afterCommand: hooks.afterCommand } : undefined),
    ];
    const includeGraphTools = projectIndex !== undefined &&
        process.env.MINICODE_TOOL_PROFILE !== "file-search-only";
    if (includeGraphTools && projectIndex) {
        tools.splice(1, 0, createReadSymbolTool(config, projectIndex));
        tools.splice(2, 0, createFindReferencesTool(projectIndex));
        tools.splice(3, 0, createGetDependenciesTool(projectIndex));
        tools.splice(4, 0, createFindPathTool(projectIndex));
        tools.splice(5, 0, createSearchCodeMapTool(projectIndex));
    }
    return new ToolRegistry(tools);
}
