import { execSync } from 'node:child_process';
import { ClaudeBridge } from './claude-bridge.js';
import { OpenCodeBridge } from './opencode-bridge.js';
import { getEngine } from '../config/engine-config.js';
function detectOpenCode() {
    try {
        execSync('which opencode', { encoding: 'utf-8', timeout: 3000 });
        return true;
    }
    catch {
        return false;
    }
}
function detectClaudeCode() {
    try {
        execSync('which claude', { encoding: 'utf-8', timeout: 3000 });
        return true;
    }
    catch {
        return false;
    }
}
export function createEngine(type) {
    const engine = type ?? getEngine();
    if (engine === 'claude-code') {
        console.log('[engine-factory] Using Claude Code bridge');
        return new ClaudeBridge({
            timeoutMs: parseInt(process.env.BRIDGE_TIMEOUT_MS ?? '300000', 10),
        });
    }
    if (engine === 'opencode') {
        console.log('[engine-factory] Using OpenCode bridge');
        return new OpenCodeBridge({
            baseUrl: process.env.OPENCODE_BASE_URL,
            timeoutMs: parseInt(process.env.BRIDGE_TIMEOUT_MS ?? '300000', 10),
        });
    }
    const hasOpenCode = detectOpenCode();
    const hasClaudeCode = detectClaudeCode();
    if (hasOpenCode) {
        console.log('[engine-factory] AUTO: OpenCode detected — using OpenCode bridge');
        return new OpenCodeBridge({
            baseUrl: process.env.OPENCODE_BASE_URL,
            timeoutMs: parseInt(process.env.BRIDGE_TIMEOUT_MS ?? '300000', 10),
        });
    }
    if (hasClaudeCode) {
        console.log('[engine-factory] AUTO: Claude Code detected — using Claude Code bridge');
        return new ClaudeBridge({
            timeoutMs: parseInt(process.env.BRIDGE_TIMEOUT_MS ?? '300000', 10),
        });
    }
    console.warn('[engine-factory] AUTO: No engine detected — falling back to Claude Code (will fail if not installed)');
    return new ClaudeBridge({
        timeoutMs: parseInt(process.env.BRIDGE_TIMEOUT_MS ?? '300000', 10),
    });
}
//# sourceMappingURL=factory.js.map