import { join } from 'node:path';
import { ToolRegistry } from './registry.js';
import { FileStateCache } from '../state/file-cache.js';
import { ProjectIndex } from '../state/project-index.js';
import { ModeManager } from '../state/mode-manager.js';
import { HookDispatcher } from '../hooks/dispatcher.js';
import { FileUndoManager } from '../state/file-undo.js';
import { ReadTool } from './read/index.js';
import { createWriteTool } from './write/index.js';
import { createEditTool } from './edit/index.js';
import { createFindTool } from './find/index.js';
import { createExecTool } from './exec/index.js';
import { createAnalyzeTool } from './analyze/index.js';
import { InspectTool } from './inspect/index.js';
import { createAgentTool } from './agent/index.js';
import { createFetchTool } from './fetch/index.js';
import { createStateTool } from './state/index.js';
import { createWorkflowServices, createWorkflowTool } from './workflow/index.js';
import { createRegistryTool } from './registry-tool/index.js';
import { KVState } from '../state/kv-state.js';
import { createTaskTool } from './task/index.js';
import { createTeamTool } from './team/index.js';
import { createWorklistTool } from './worklist/index.js';
import { createMcpTool } from './mcp/index.js';
import { createPacketTool } from './packet/index.js';
import { createQueryTool } from './query/index.js';
import { createRemoteTool } from './remote-trigger/index.js';
import { createReplTool } from './repl/index.js';
import { controlTool } from './control/index.js';
import { createChannelTool } from './channel/index.js';
import { createWebSearchTool } from './web-search/index.js';
import { ProcessManager } from './shared/process-manager.js';
import { AgentMessageBus } from '../agents/message-bus.js';
import { CrossSessionTaskRegistry } from '../sessions/orchestration/index.js';
import { OverflowHandler } from './shared/overflow.js';
import { createGoodVibesContextTool, createGoodVibesSettingsTool, } from './goodvibes-runtime/index.js';
export { ToolRegistry } from './registry.js';
export { ProcessManager } from './shared/process-manager.js';
export { AGENT_TEMPLATES, AgentManager } from './agent/index.js';
export { AutoHealer, } from './shared/auto-heal.js';
export { appendSchemaFingerprint, computeSchemaFingerprint, computeSchemaFingerprintSync, getSchemaShapeId, SCHEMA_SHAPE_IDS, } from './shared/schema-fingerprint.js';
export { OverflowHandler, createSpillBackend } from './shared/overflow.js';
export { DiagnosticsBackend, FileBackend, LedgerBackend, overflowCleanup, } from './shared/overflow.js';
export { WORKFLOW_DEFINITIONS, TriggerManager, WorkflowManager, ScheduleManager, createWorkflowServices, createWorkflowTool, parseInterval, } from './workflow/index.js';
export { loadSkillByTrigger } from './registry-tool/skill-loader.js';
export { ReadTool } from './read/index.js';
export { createWriteTool } from './write/index.js';
export { createEditTool } from './edit/index.js';
export { createFindTool } from './find/index.js';
export { createExecTool } from './exec/index.js';
export { formatDenialResponse, guardExecCommand } from './exec/ast-guard.js';
export { createAnalyzeTool } from './analyze/index.js';
export { InspectTool } from './inspect/index.js';
export { createAgentTool } from './agent/index.js';
export { createChannelTool } from './channel/index.js';
export { registerChannelAgentTools } from './channel/agent-tools.js';
export { controlTool } from './control/index.js';
export { createFetchTool } from './fetch/index.js';
export { applySanitizer, resolveSanitizeMode } from './fetch/sanitizer.js';
export { TRUST_TIER_EVENTS, classifyHostTrustTier, extractHostname, } from './fetch/trust-tiers.js';
export { repairToolCall } from './auto-repair.js';
export { createMcpTool } from './mcp/index.js';
export { createPacketTool } from './packet/index.js';
export { createQueryTool } from './query/index.js';
export { createRegistryTool } from './registry-tool/index.js';
export { createRemoteTool } from './remote-trigger/index.js';
export { createReplTool } from './repl/index.js';
export { createStateTool } from './state/index.js';
export { createTaskTool } from './task/index.js';
export { createTeamTool } from './team/index.js';
export { createWebSearchTool } from './web-search/index.js';
export { createWorklistTool } from './worklist/index.js';
export function registerToolWithContractGate(registry, tool, featureFlags) {
    const verifyContracts = featureFlags?.isEnabled('tool-contract-verification') ?? true;
    if (!verifyContracts) {
        registry.register(tool);
        return;
    }
    registry.registerWithContract(tool, {
        strictIdempotency: false,
        strictPermissionClass: false,
    });
}
/**
 * Register all built-in tools into the given registry.
 * Creates shared FileStateCache and ProjectIndex instances so read/write/edit
 * tools share cache state within a session.
 */
export function registerAllTools(registry, deps) {
    const fileCache = deps?.fileCache ?? new FileStateCache();
    if (!deps?.fileUndoManager || !deps?.modeManager || !deps?.processManager || !deps?.agentMessageBus || !deps?.workflowServices) {
        throw new Error('registerAllTools requires explicit fileUndoManager, modeManager, processManager, agentMessageBus, and workflowServices ownership.');
    }
    const fileUndoManager = deps.fileUndoManager;
    const modeManager = deps.modeManager;
    const processManager = deps.processManager;
    const agentManager = deps?.agentManager
        ?? (deps?.remoteRunnerRegistry
            ? deps.remoteRunnerRegistry.agentManager ?? null
            : null);
    if (!agentManager) {
        throw new Error('registerAllTools requires agentManager');
    }
    const agentMessageBus = deps.agentMessageBus;
    const wrfcController = deps?.wrfcController;
    const archetypeLoader = deps?.archetypeLoader;
    const webSearchService = deps?.webSearchService;
    const channelRegistry = deps?.channelRegistry ?? null;
    const remoteRunnerRegistry = deps?.remoteRunnerRegistry;
    const workflowServices = deps.workflowServices;
    const mcpRegistry = deps?.mcpRegistry;
    if (!deps?.configManager || !deps?.providerRegistry || !deps?.toolLLM) {
        throw new Error('registerAllTools requires configManager, providerRegistry, and toolLLM');
    }
    if (!deps?.sandboxSessionRegistry) {
        throw new Error('registerAllTools requires sandboxSessionRegistry');
    }
    if (!deps?.sessionOrchestration) {
        throw new Error('registerAllTools requires sessionOrchestration');
    }
    const sessionOrchestration = deps.sessionOrchestration;
    const workingDirectory = deps?.workingDirectory;
    if (!workingDirectory) {
        throw new Error('registerAllTools requires workingDirectory');
    }
    if (!deps?.surfaceRoot || deps.surfaceRoot.trim().length === 0) {
        throw new Error('registerAllTools requires surfaceRoot');
    }
    const projectIndex = deps?.projectIndex ?? new ProjectIndex(workingDirectory);
    const registerTool = (tool) => {
        registerToolWithContractGate(registry, tool, deps.featureFlags);
    };
    registerTool(createGoodVibesContextTool({
        configManager: deps.configManager,
        providerRegistry: deps.providerRegistry,
        toolRegistry: registry,
        channelRegistry,
        serviceRegistry: deps.serviceRegistry,
        secretsManager: deps.secretsManager ?? null,
        workingDirectory,
        homeDirectory: deps.configManager.getHomeDirectory() ?? undefined,
        surfaceRoot: deps.surfaceRoot,
    }));
    registerTool(createGoodVibesSettingsTool({
        configManager: deps.configManager,
    }));
    registerTool(new ReadTool(projectIndex, fileCache));
    registerTool(createWriteTool({
        projectRoot: workingDirectory,
        fileCache,
        projectIndex,
        fileUndoManager,
        configManager: deps.configManager,
        toolLLM: deps.toolLLM,
        changeTracker: deps?.changeTracker,
    }));
    registerTool(createEditTool(fileCache, {
        fileUndoManager,
        configManager: deps.configManager,
        toolLLM: deps.toolLLM,
        changeTracker: deps?.changeTracker,
    }));
    registerTool(createFindTool(workingDirectory, deps.featureFlags));
    registerTool(createExecTool(processManager, {
        featureFlags: deps.featureFlags,
        overflowHandler: deps.overflowHandler,
    }));
    registerTool(createAnalyzeTool(deps.toolLLM, deps.featureFlags, workingDirectory));
    registerTool(new InspectTool(deps.featureFlags, workingDirectory));
    registerTool(createAgentTool({
        manager: agentManager,
        messageBus: agentMessageBus,
        configManager: deps.configManager,
        ...(archetypeLoader ? { archetypeLoader } : {}),
        ...(wrfcController ? { wrfcController } : {}),
    }));
    const kvState = new KVState({ stateDir: join(workingDirectory, '.goodvibes', 'state') });
    const hookDispatcher = new HookDispatcher();
    registerTool(createStateTool(kvState, projectIndex, {
        memoryDir: join(workingDirectory, '.goodvibes', 'memory'),
        hookDispatcher,
        modeManager,
    }));
    registerTool(createWorkflowTool(workflowServices));
    registerTool(createFetchTool({
        serviceRegistry: deps.serviceRegistry,
        featureFlags: deps.featureFlags,
    }));
    if (webSearchService) {
        registerTool(createWebSearchTool(webSearchService));
    }
    registerTool(createRegistryTool(registry, {
        workingDirectory,
        homeDirectory: deps.configManager.getHomeDirectory() ?? undefined,
    }));
    registerTool(createTaskTool(sessionOrchestration));
    registerTool(createTeamTool({ surfaceRoot: deps.surfaceRoot }));
    registerTool(createWorklistTool({ surfaceRoot: deps.surfaceRoot }));
    if (mcpRegistry) {
        registerTool(createMcpTool(mcpRegistry));
    }
    registerTool(createPacketTool({ workingDirectory, surfaceRoot: deps.surfaceRoot }));
    registerTool(createQueryTool({ workingDirectory, surfaceRoot: deps.surfaceRoot }));
    if (remoteRunnerRegistry) {
        registerTool(createRemoteTool(remoteRunnerRegistry));
    }
    registerTool(createReplTool(deps.configManager, deps.sandboxSessionRegistry, {
        surfaceRoot: deps.surfaceRoot,
    }));
    registerTool(controlTool);
    registerTool(createChannelTool(channelRegistry));
    return { fileCache, projectIndex };
}
