"use strict";
/**
 * CLI Tool Manager
 * Singleton class to manage multiple CLI tools (Claude, Codex, Gemini, Vibe Local, OpenCode)
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.CLIToolManager = void 0;
const claude_1 = require("./claude");
const codex_1 = require("./codex");
const gemini_1 = require("./gemini");
const vibe_local_1 = require("./vibe-local");
const opencode_1 = require("./opencode");
const response_poller_1 = require("../polling/response-poller");
/**
 * CLI Tool Manager (Singleton)
 * Provides centralized access to all CLI tools (Issue #368: includes Vibe Local, Issue #379: includes OpenCode)
 */
class CLIToolManager {
    static instance;
    tools;
    /**
     * Private constructor for Singleton pattern
     */
    constructor() {
        this.tools = new Map();
        // Initialize all tools
        this.tools.set('claude', new claude_1.ClaudeTool());
        this.tools.set('codex', new codex_1.CodexTool());
        this.tools.set('gemini', new gemini_1.GeminiTool());
        this.tools.set('vibe-local', new vibe_local_1.VibeLocalTool());
        this.tools.set('opencode', new opencode_1.OpenCodeTool());
    }
    /**
     * Get singleton instance
     *
     * @returns CLIToolManager instance
     */
    static getInstance() {
        if (!CLIToolManager.instance) {
            CLIToolManager.instance = new CLIToolManager();
        }
        return CLIToolManager.instance;
    }
    /**
     * Get a specific CLI tool by type
     *
     * @param type - CLI tool type
     * @returns CLI tool instance
     *
     * @example
     * ```typescript
     * const manager = CLIToolManager.getInstance();
     * const claude = manager.getTool('claude');
     * await claude.startSession('my-worktree', '/path/to/worktree');
     * ```
     */
    getTool(type) {
        const tool = this.tools.get(type);
        if (!tool) {
            throw new Error(`CLI tool '${type}' not found`);
        }
        return tool;
    }
    /**
     * Get all CLI tools
     *
     * @returns Array of all CLI tool instances
     *
     * @example
     * ```typescript
     * const manager = CLIToolManager.getInstance();
     * const allTools = manager.getAllTools();
     * console.log(allTools.map(t => t.name)); // ['Claude Code', 'Codex CLI', 'Gemini CLI', 'Vibe Local', 'OpenCode']
     * ```
     */
    getAllTools() {
        return Array.from(this.tools.values());
    }
    /**
     * Get information about a specific tool including installation status
     *
     * @param type - CLI tool type
     * @returns Tool information with installation status
     *
     * @example
     * ```typescript
     * const manager = CLIToolManager.getInstance();
     * const info = await manager.getToolInfo('claude');
     * if (info.installed) {
     *   console.log(`${info.name} is installed`);
     * }
     * ```
     */
    async getToolInfo(type) {
        const tool = this.getTool(type);
        const installed = await tool.isInstalled();
        return {
            id: tool.id,
            name: tool.name,
            command: tool.command,
            installed,
        };
    }
    /**
     * Get information about all tools including installation status
     *
     * @returns Array of tool information for all tools
     *
     * @example
     * ```typescript
     * const manager = CLIToolManager.getInstance();
     * const allInfo = await manager.getAllToolsInfo();
     * allInfo.forEach(info => {
     *   console.log(`${info.name}: ${info.installed ? 'installed' : 'not installed'}`);
     * });
     * ```
     */
    async getAllToolsInfo() {
        const tools = this.getAllTools();
        const infoPromises = tools.map(async (tool) => {
            const installed = await tool.isInstalled();
            return {
                id: tool.id,
                name: tool.name,
                command: tool.command,
                installed,
            };
        });
        return Promise.all(infoPromises);
    }
    /**
     * Get only installed tools
     *
     * @returns Array of tool information for installed tools only
     *
     * @example
     * ```typescript
     * const manager = CLIToolManager.getInstance();
     * const installed = await manager.getInstalledTools();
     * console.log(`${installed.length} tools installed`);
     * ```
     */
    async getInstalledTools() {
        const allInfo = await this.getAllToolsInfo();
        return allInfo.filter(info => info.installed);
    }
    /**
     * Stop pollers for a specific worktree and CLI tool
     * T2.4: Abstraction for poller stopping (MF1-001 DIP compliance)
     *
     * This method abstracts the poller stopping logic so API layer
     * doesn't need to know about specific poller implementations.
     *
     * @param worktreeId - Worktree ID
     * @param cliToolId - CLI tool ID
     *
     * @example
     * ```typescript
     * const manager = CLIToolManager.getInstance();
     * manager.stopPollers('my-worktree', 'claude');
     * ```
     */
    stopPollers(worktreeId, cliToolId) {
        // Stop response-poller for all tools
        (0, response_poller_1.stopPolling)(worktreeId, cliToolId);
    }
}
exports.CLIToolManager = CLIToolManager;
