"use strict";
/**
 * Gemini CLI tool implementation
 * Provides integration with Google's Gemini CLI
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.GeminiTool = void 0;
const base_1 = require("./base");
const tmux_1 = require("../tmux");
const child_process_1 = require("child_process");
const util_1 = require("util");
const execAsync = (0, util_1.promisify)(child_process_1.exec);
/**
 * Gemini CLI tool implementation
 * Manages Gemini sessions using tmux
 */
class GeminiTool extends base_1.BaseCLITool {
    id = 'gemini';
    name = 'Gemini CLI';
    command = 'gemini';
    /**
     * Check if Gemini session is running for a worktree
     *
     * @param worktreeId - Worktree ID
     * @returns True if session is running
     */
    async isRunning(worktreeId) {
        const sessionName = this.getSessionName(worktreeId);
        return await (0, tmux_1.hasSession)(sessionName);
    }
    /**
     * Start a new Gemini session for a worktree
     * Note: Gemini uses non-interactive mode, so we just create a tmux session
     * for running one-shot commands
     *
     * @param worktreeId - Worktree ID
     * @param worktreePath - Worktree path
     */
    async startSession(worktreeId, worktreePath) {
        // Check if Gemini is installed
        const geminiAvailable = await this.isInstalled();
        if (!geminiAvailable) {
            throw new Error('Gemini CLI is not installed or not in PATH');
        }
        const sessionName = this.getSessionName(worktreeId);
        // Check if session already exists
        const exists = await (0, tmux_1.hasSession)(sessionName);
        if (exists) {
            console.log(`Gemini session ${sessionName} already exists`);
            return;
        }
        try {
            // Create tmux session for running Gemini commands
            await (0, tmux_1.createSession)({
                sessionName,
                workingDirectory: worktreePath,
                historyLimit: 50000,
            });
            console.log(`✓ Started Gemini session: ${sessionName}`);
        }
        catch (error) {
            const errorMessage = error instanceof Error ? error.message : String(error);
            throw new Error(`Failed to start Gemini session: ${errorMessage}`);
        }
    }
    /**
     * Send a message to Gemini session (non-interactive mode)
     * Executes a one-shot Gemini command and captures the output
     *
     * @param worktreeId - Worktree ID
     * @param message - Message to send
     */
    async sendMessage(worktreeId, message) {
        const sessionName = this.getSessionName(worktreeId);
        // Check if session exists
        const exists = await (0, tmux_1.hasSession)(sessionName);
        if (!exists) {
            throw new Error(`Gemini session ${sessionName} does not exist. Start the session first.`);
        }
        try {
            // Escape the message for shell execution
            const escapedMessage = message.replace(/'/g, "'\\''");
            // Execute Gemini in non-interactive mode using stdin piping
            // This approach bypasses the TUI and executes in one-shot mode
            await (0, tmux_1.sendKeys)(sessionName, `echo '${escapedMessage}' | gemini`, true);
            console.log(`✓ Sent message to Gemini session: ${sessionName}`);
        }
        catch (error) {
            const errorMessage = error instanceof Error ? error.message : String(error);
            throw new Error(`Failed to send message to Gemini: ${errorMessage}`);
        }
    }
    /**
     * Kill Gemini session
     *
     * @param worktreeId - Worktree ID
     */
    async killSession(worktreeId) {
        const sessionName = this.getSessionName(worktreeId);
        try {
            // Send Ctrl+D to exit Gemini gracefully
            const exists = await (0, tmux_1.hasSession)(sessionName);
            if (exists) {
                // Send Ctrl+D (ASCII 4)
                await execAsync(`tmux send-keys -t "${sessionName}" C-d`);
                // Wait a moment for Gemini to exit
                await new Promise((resolve) => setTimeout(resolve, 500));
            }
            // Kill the tmux session
            const killed = await (0, tmux_1.killSession)(sessionName);
            if (killed) {
                console.log(`✓ Stopped Gemini session: ${sessionName}`);
            }
        }
        catch (error) {
            const errorMessage = error instanceof Error ? error.message : String(error);
            console.error(`Error stopping Gemini session: ${errorMessage}`);
            throw error;
        }
    }
}
exports.GeminiTool = GeminiTool;
