// @crash-shield-ignore-file — config/cache reader for Planu-controlled JSON; writer is this codebase, shape guaranteed by build/seed.
// Planu — project-dna/ai-tool-detector.ts
// Detects which AI tool is active in a project by checking detection files.
import { access, readFile } from 'node:fs/promises';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const REGISTRY_PATH = join(dirname(fileURLToPath(import.meta.url)), '../../config/ai-tool-registry.json');
let cachedRegistry = null;
async function loadRegistry() {
    if (cachedRegistry !== null) {
        return cachedRegistry;
    }
    const raw = await readFile(REGISTRY_PATH, 'utf-8');
    cachedRegistry = JSON.parse(raw);
    return cachedRegistry;
}
// ---------------------------------------------------------------------------
// Detection order (priority: highest first)
// ---------------------------------------------------------------------------
const DETECTION_ORDER = [
    'claude',
    'cursor',
    'windsurf',
    'gemini',
    'kiro',
    'cline',
    'copilot',
    'aider',
];
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
async function pathExists(filePath) {
    try {
        await access(filePath);
        return true;
    }
    catch {
        return false;
    }
}
async function anyDetectionFileExists(projectPath, detectionFiles) {
    for (const f of detectionFiles) {
        if (await pathExists(join(projectPath, f))) {
            return true;
        }
    }
    return false;
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Detects which AI tool is active by checking detection files in priority order.
 * Returns 'unknown' if no known AI tool is detected.
 */
export async function detectAITool(projectPath) {
    const registry = await loadRegistry();
    for (const toolId of DETECTION_ORDER) {
        const entry = registry.tools[toolId];
        if (entry === undefined) {
            continue;
        }
        if (await anyDetectionFileExists(projectPath, entry.detectionFiles)) {
            return toolId;
        }
    }
    return 'unknown';
}
/**
 * Loads the full AI tool profile from the registry for a given tool ID.
 * Returns an 'unknown' profile if the tool ID is not found.
 */
export async function loadAIToolProfile(toolId) {
    if (toolId === 'unknown') {
        return buildUnknownProfile();
    }
    const registry = await loadRegistry();
    const entry = registry.tools[toolId];
    if (entry === undefined) {
        return buildUnknownProfile();
    }
    return {
        id: toolId,
        name: entry.name,
        detectionFiles: entry.detectionFiles,
        rulesDir: entry.rulesDir,
        rulesFile: entry.rulesFile,
        rulesFormat: entry.rulesFormat,
        docsUrl: entry.docsUrl,
        configFile: entry.configFile,
        frontmatterRequired: entry.frontmatterRequired,
    };
}
function buildUnknownProfile() {
    return {
        id: 'unknown',
        name: 'Unknown AI Tool',
        detectionFiles: [],
        rulesFormat: 'markdown',
        docsUrl: '',
    };
}
//# sourceMappingURL=ai-tool-detector.js.map