// engine/architecture-pattern-detector/ddd-detector.ts — DDD detection (SPEC-033)
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { glob } from 'glob';
// === DDD Detection ===
/** Detects DDD from tactical patterns (suffixes), bounded context dirs, and ubiquitous language */
export async function detectDdd(projectPath) {
    const indicators = [];
    let hasUbiquitousLanguage = false;
    let ubiquitousLanguagePath;
    let hasEventStormingDiagrams = false;
    const boundedContexts = [];
    // Check for DDD tactical suffixes
    const dddSuffixes = ['Aggregate', 'ValueObject', 'DomainEvent', 'Repository', 'Specification'];
    try {
        for (const suffix of dddSuffixes) {
            const files = await glob(`**/*${suffix}.{ts,js,cs,java,py,go}`, {
                cwd: projectPath,
                nodir: true,
                ignore: ['node_modules/**', 'dist/**', '.git/**'],
            });
            if (files.length > 0) {
                indicators.push(`ddd-suffix-${suffix}: ${files.length} file(s)`);
            }
        }
    }
    catch {
        // ignore
    }
    // Check for bounded contexts directory
    const contextDirs = ['bounded-contexts', 'contexts', 'domain'];
    for (const dir of contextDirs) {
        try {
            const subDirs = await glob(`${dir}/*/`, { cwd: projectPath });
            if (subDirs.length > 0) {
                indicators.push(`bounded-context-dir: ${dir}/ with ${subDirs.length} context(s)`);
                subDirs.forEach((d) => {
                    const name = d.replace(`${dir}/`, '').replace('/', '');
                    if (name && !boundedContexts.includes(name)) {
                        boundedContexts.push(name);
                    }
                });
            }
        }
        catch {
            // ignore
        }
    }
    // Check for ubiquitous language file
    const ulPaths = ['ubiquitous-language.md', 'docs/ubiquitous-language.md', 'domain/glossary.md'];
    for (const ulPath of ulPaths) {
        try {
            await readFile(join(projectPath, ulPath), 'utf-8');
            hasUbiquitousLanguage = true;
            ubiquitousLanguagePath = ulPath;
            indicators.push(`ubiquitous-language: ${ulPath}`);
            break;
        }
        catch {
            // not found
        }
    }
    // Check README for EventStorming links
    try {
        const readme = await readFile(join(projectPath, 'README.md'), 'utf-8');
        if (/miro\.com|mural\.co|lucidchart|EventStorming|event.storming/i.test(readme)) {
            hasEventStormingDiagrams = true;
            indicators.push('readme-eventstorming-links');
        }
    }
    catch {
        // ignore
    }
    // Require at least 3 indicators for DDD to be "detected"
    const detected = indicators.length >= 3;
    // Build context map if we have bounded contexts
    let contextMap;
    if (detected && boundedContexts.length > 0) {
        contextMap = {
            contexts: boundedContexts,
            relations: [],
            updatedAt: new Date().toISOString(),
        };
    }
    return {
        detected,
        boundedContexts,
        hasUbiquitousLanguage,
        ubiquitousLanguagePath,
        hasEventStormingDiagrams,
        contextMap,
    };
}
//# sourceMappingURL=ddd-detector.js.map