"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExecutorFactory = void 0;
const majk_executor_1 = require("./majk.executor");
const claude_code_executor_1 = require("./claude-code.executor");
const majk_chat_claude_code_1 = require("@majkapp/majk-chat-claude-code");
const child_process_1 = require("child_process");
class ExecutorFactory {
    static create(type, claudePath) {
        const executorType = type || process.env.MAJK_AGENT || 'majk';
        switch (executorType) {
            case 'claude-code':
            case 'claude':
                // Use SDK-based executor (new default)
                return new majk_chat_claude_code_1.ClaudeCodeExecutor();
            case 'claude-code-spawn':
                // Legacy CLI spawning executor
                return new claude_code_executor_1.ClaudeCodeExecutor(claudePath);
            case 'majk':
            default:
                return new majk_executor_1.MajkExecutor();
        }
    }
    static async isClaudeCodeAvailable(claudePath = 'claude') {
        return new Promise((resolve) => {
            try {
                const child = (0, child_process_1.spawn)(claudePath, ['--version'], {
                    stdio: 'pipe'
                });
                child.stdin?.end();
                let hasOutput = false;
                child.stdout.on('data', () => {
                    hasOutput = true;
                });
                child.on('close', (code) => {
                    resolve(code === 0 || hasOutput);
                });
                child.on('error', () => {
                    resolve(false);
                });
                setTimeout(() => {
                    if (!child.killed) {
                        child.kill();
                        resolve(false);
                    }
                }, 3000);
            }
            catch {
                resolve(false);
            }
        });
    }
    static async validateExecutorChoice(type, claudePath) {
        if (type === 'claude-code' || type === 'claude') {
            // SDK-based executor - check if package is available
            try {
                require('@majkapp/majk-chat-claude-code');
            }
            catch (error) {
                throw new Error(`Claude Code SDK package not found. This should not happen in a proper installation.\n` +
                    `Please reinstall: npm install`);
            }
        }
        else if (type === 'claude-code-spawn') {
            // Legacy CLI spawning - check if CLI is available
            const available = await ExecutorFactory.isClaudeCodeAvailable(claudePath);
            if (!available) {
                throw new Error(`Claude Code CLI not found. Please ensure 'claude' is installed and available in PATH.\n` +
                    `Visit https://docs.anthropic.com/claude/docs/claude-code for installation instructions.`);
            }
        }
    }
}
exports.ExecutorFactory = ExecutorFactory;
//# sourceMappingURL=executor-factory.js.map