// engine/ai-integration/detector.ts — AI tool detector for SPEC-268
import { execSync } from 'node:child_process';
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import { homedir } from 'node:os';
const HOME = homedir();
function which(cmd) {
    /* v8 ignore start */
    try {
        execSync(`which ${cmd}`, { stdio: 'ignore' });
        return true;
    }
    catch {
        return false;
    }
    /* v8 ignore stop */
}
function detectClaudeCode(_projectPath) {
    const settingsPath = join(HOME, '.claude', 'settings.json');
    const detected = existsSync(settingsPath) || which('claude');
    return {
        name: 'claude-code',
        detected,
        configPath: detected ? settingsPath : undefined,
        configExists: existsSync(settingsPath),
    };
}
function detectCodexCli(_projectPath) {
    const configPath = join(HOME, '.codex', 'config.toml');
    const detected = existsSync(configPath) || which('codex');
    return {
        name: 'codex-cli',
        detected,
        configPath: detected ? configPath : undefined,
        configExists: existsSync(configPath),
    };
}
function detectGeminiCli(_projectPath) {
    const settingsPath = join(HOME, '.gemini', 'settings.json');
    const detected = existsSync(settingsPath) || which('gemini');
    return {
        name: 'gemini-cli',
        detected,
        configPath: detected ? settingsPath : undefined,
        configExists: existsSync(settingsPath),
    };
}
function detectCursor(projectPath) {
    const cursorHome = join(HOME, '.cursor');
    const rulesPath = join(projectPath, '.cursorrules');
    const detected = existsSync(cursorHome) || existsSync(rulesPath);
    return {
        name: 'cursor',
        detected,
        configPath: detected ? rulesPath : undefined,
        configExists: existsSync(rulesPath),
    };
}
function detectWindsurf(projectPath) {
    const windsurfHome = join(HOME, '.windsurf');
    const rulesPath = join(projectPath, '.windsurfrules');
    const detected = existsSync(windsurfHome) || existsSync(rulesPath);
    return {
        name: 'windsurf',
        detected,
        configPath: detected ? rulesPath : undefined,
        configExists: existsSync(rulesPath),
    };
}
function detectGithubCopilot(_projectPath) {
    const copilotDir = join(HOME, '.config', 'github-copilot');
    const detected = existsSync(copilotDir);
    return {
        name: 'github-copilot',
        detected,
        configPath: detected ? copilotDir : undefined,
        configExists: detected,
    };
}
function detectContinueDev(_projectPath) {
    const continueDir = join(HOME, '.continue');
    const detected = existsSync(continueDir);
    return {
        name: 'continue-dev',
        detected,
        configPath: detected ? continueDir : undefined,
        configExists: detected,
    };
}
function detectAider(projectPath) {
    const confPath = join(projectPath, '.aider.conf.yml');
    const detected = which('aider') || existsSync(confPath);
    return {
        name: 'aider',
        detected,
        configPath: detected ? confPath : undefined,
        configExists: existsSync(confPath),
    };
}
const DETECTORS = {
    'claude-code': detectClaudeCode,
    'codex-cli': detectCodexCli,
    'gemini-cli': detectGeminiCli,
    cursor: detectCursor,
    windsurf: detectWindsurf,
    'github-copilot': detectGithubCopilot,
    'continue-dev': detectContinueDev,
    aider: detectAider,
};
export function detectAiTools(projectPath) {
    const tools = Object.values(DETECTORS).map((detect) => detect(projectPath));
    return {
        tools,
        scannedAt: new Date().toISOString(),
        projectPath,
    };
}
export function isToolDetected(tool, projectPath) {
    const detect = DETECTORS[tool];
    return detect(projectPath).detected;
}
export function getDetectedTools(projectPath) {
    return detectAiTools(projectPath)
        .tools.filter((t) => t.detected)
        .map((t) => t.name);
}
//# sourceMappingURL=detector.js.map