// Agent platform detection functions
import { readFile, access } from 'node:fs/promises';
import { join } from 'node:path';
// === Agent Platform Config ===
let platformsCache = null;
export async function loadPlatforms() {
    if (platformsCache) {
        return platformsCache;
    }
    try {
        const configPath = new URL('../../config/agent-platforms.json', import.meta.url);
        const content = await readFile(configPath, 'utf-8');
        platformsCache = JSON.parse(content);
        return platformsCache;
    }
    catch {
        /* v8 ignore next -- defensive catch for missing config file */
        return { platforms: {} };
    }
}
/**
 * Get the agent configuration for a platform.
 */
export async function getAgentConfig(platform) {
    const platforms = await loadPlatforms();
    const config = platforms.platforms[platform];
    if (!config) {
        return {
            platform,
            rulesFile: '',
            format: 'markdown',
            capabilities: [],
            mcpSupport: false,
        };
    }
    return {
        platform,
        rulesFile: config.rulesFile,
        skillsDir: config.skillsDir ?? undefined,
        format: config.format,
        capabilities: config.capabilities,
        mcpSupport: config.mcpSupport,
    };
}
/**
 * Detect which agent platform is likely being used in a project.
 */
export async function detectAgentPlatform(projectConventions) {
    const platforms = await loadPlatforms();
    const detectedFrom = [];
    let bestPlatform = 'claude-code';
    let bestScore = 0;
    for (const [key, config] of Object.entries(platforms.platforms)) {
        let score = 0;
        // Check if rules file convention is present
        if (config.rulesFile && projectConventions['ai-rules'] === config.rulesFile) {
            score += 3;
            detectedFrom.push(`Rules file: ${config.rulesFile}`);
        }
        // Check for platform-specific convention keys
        // Claude Code uses 'ai-rules', others use '{platform}-rules' pattern
        const conventionKey = key === 'claude-code' ? 'ai-rules' : `${key}-rules`;
        if (projectConventions[conventionKey]) {
            score += 5;
            detectedFrom.push(`${projectConventions[conventionKey]} detected`);
        }
        if (score > bestScore) {
            bestScore = score;
            bestPlatform = key;
        }
    }
    const confidence = Math.min(1, bestScore / 5);
    return { platform: bestPlatform, confidence, detectedFrom };
}
/**
 * Detect which AI agent platform is being used by scanning actual config files on disk.
 * More reliable than convention-based detection.
 */
export async function detectAgentPlatformFromFiles(projectPath) {
    const platforms = await loadPlatforms();
    const detectedFrom = [];
    let bestPlatform = 'claude-code';
    let bestScore = 0;
    for (const [key, config] of Object.entries(platforms.platforms)) {
        let score = 0;
        const foundFiles = [];
        const configFiles = config.configFiles ?? [config.rulesFile];
        for (const cfgFile of configFiles) {
            if (!cfgFile) {
                continue;
            }
            const filePath = join(projectPath, cfgFile);
            try {
                await access(filePath);
                score += 3;
                foundFiles.push(cfgFile);
            }
            catch {
                // File not found — not this platform
            }
        }
        if (foundFiles.length > 0) {
            detectedFrom.push(...foundFiles.map((f) => `${key}: ${f}`));
        }
        if (score > bestScore) {
            bestScore = score;
            bestPlatform = key;
        }
    }
    const confidence = bestScore > 0 ? Math.min(1, bestScore / 6) : 0;
    return { platform: bestPlatform, confidence, detectedFrom };
}
//# sourceMappingURL=detect.js.map