"use strict";
/**
 * Base implementation for CLI tools
 * Provides common functionality for all CLI tool implementations
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseCLITool = void 0;
const child_process_1 = require("child_process");
const util_1 = require("util");
const validation_1 = require("./validation");
const tmux_1 = require("../tmux/tmux");
const execAsync = (0, util_1.promisify)(child_process_1.exec);
/**
 * Abstract base class for CLI tools
 * Implements common functionality and defines abstract methods for specific implementations
 */
class BaseCLITool {
    /**
     * Check if CLI tool is installed
     * Uses 'which' command to check if the tool is available in PATH
     */
    async isInstalled() {
        try {
            await execAsync(`which ${this.command}`, { timeout: 5000 });
            return true;
        }
        catch {
            return false;
        }
    }
    /**
     * Generate session name for a worktree
     * Format: mcbd-{cli_tool_id}-{worktree_id}
     *
     * T2.3: Added validation to prevent command injection (MF4-001)
     *
     * @param worktreeId - Worktree ID
     * @returns Session name
     * @throws Error if the resulting session name is invalid
     */
    getSessionName(worktreeId) {
        const sessionName = `mcbd-${this.id}-${worktreeId}`;
        (0, validation_1.validateSessionName)(sessionName);
        return sessionName;
    }
    /**
     * Interrupt processing by sending Escape key
     * Default implementation: send Escape key to tmux session
     *
     * @param worktreeId - Worktree ID
     */
    async interrupt(worktreeId) {
        const sessionName = this.getSessionName(worktreeId);
        await (0, tmux_1.sendSpecialKey)(sessionName, 'Escape');
    }
}
exports.BaseCLITool = BaseCLITool;
