"use strict";
/**
 * Codex CLI tool implementation
 * Provides integration with OpenAI's Codex CLI
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodexTool = 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);
/**
 * Codex CLI tool implementation
 * Manages Codex sessions using tmux
 */
class CodexTool extends base_1.BaseCLITool {
    id = 'codex';
    name = 'Codex CLI';
    command = 'codex';
    /**
     * Check if Codex 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 Codex session for a worktree
     *
     * @param worktreeId - Worktree ID
     * @param worktreePath - Worktree path
     */
    async startSession(worktreeId, worktreePath) {
        // Check if Codex is installed
        const codexAvailable = await this.isInstalled();
        if (!codexAvailable) {
            throw new Error('Codex 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(`Codex session ${sessionName} already exists`);
            return;
        }
        try {
            // Create tmux session with large history buffer for Codex output
            await (0, tmux_1.createSession)({
                sessionName,
                workingDirectory: worktreePath,
                historyLimit: 50000,
            });
            // Wait a moment for the session to be created
            await new Promise((resolve) => setTimeout(resolve, 100));
            // Start Codex CLI in interactive mode
            await (0, tmux_1.sendKeys)(sessionName, 'codex', true);
            // Wait for Codex to initialize (and potentially show update notification)
            await new Promise((resolve) => setTimeout(resolve, 3000));
            // Auto-skip update notification if present (select option 2: Skip)
            await (0, tmux_1.sendKeys)(sessionName, '2', true);
            // Wait a moment for the selection to process
            await new Promise((resolve) => setTimeout(resolve, 500));
            console.log(`✓ Started Codex session: ${sessionName}`);
        }
        catch (error) {
            const errorMessage = error instanceof Error ? error.message : String(error);
            throw new Error(`Failed to start Codex session: ${errorMessage}`);
        }
    }
    /**
     * Send a message to Codex session
     *
     * @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(`Codex session ${sessionName} does not exist. Start the session first.`);
        }
        try {
            // Send message to Codex (without Enter)
            await (0, tmux_1.sendKeys)(sessionName, message, false);
            // Wait a moment for the text to be typed
            await new Promise((resolve) => setTimeout(resolve, 100));
            // Send Enter key separately
            await execAsync(`tmux send-keys -t "${sessionName}" C-m`);
            // Wait a moment for the message to be processed
            await new Promise((resolve) => setTimeout(resolve, 200));
            console.log(`✓ Sent message to Codex session: ${sessionName}`);
        }
        catch (error) {
            const errorMessage = error instanceof Error ? error.message : String(error);
            throw new Error(`Failed to send message to Codex: ${errorMessage}`);
        }
    }
    /**
     * Kill Codex session
     *
     * @param worktreeId - Worktree ID
     */
    async killSession(worktreeId) {
        const sessionName = this.getSessionName(worktreeId);
        try {
            // Send Ctrl+D to exit Codex 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 Codex 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 Codex session: ${sessionName}`);
            }
        }
        catch (error) {
            const errorMessage = error instanceof Error ? error.message : String(error);
            console.error(`Error stopping Codex session: ${errorMessage}`);
            throw error;
        }
    }
}
exports.CodexTool = CodexTool;
