// engine/architecture-pattern-detector/cell-data-bff-detector.ts — Cell-based, Data Mesh, BFF (SPEC-033)
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { glob } from 'glob';
// === Cell-based Architecture ===
/** Detects cell-based architecture from env variables and infra config */
export async function detectCellBased(projectPath) {
    const indicators = [];
    // Check .env files for CELL_ID / CELL_NAME
    const envFiles = ['.env', '.env.example', '.env.template', '.env.sample'];
    for (const envFile of envFiles) {
        try {
            const content = await readFile(join(projectPath, envFile), 'utf-8');
            if (/CELL_ID|CELL_NAME|CELL_INDEX/i.test(content)) {
                indicators.push(`env-var-cell-id in ${envFile}`);
            }
        }
        catch {
            // file not present
        }
    }
    // Check terraform for cell workspaces
    try {
        const tfFiles = await glob('**/*.tf', {
            cwd: projectPath,
            nodir: true,
            ignore: ['node_modules/**'],
        });
        for (const tf of tfFiles) {
            try {
                const content = await readFile(join(projectPath, tf), 'utf-8');
                if (/cell[-_]\w+|workspace.*cell/i.test(content)) {
                    indicators.push(`terraform-cell-workspace in ${tf}`);
                    break;
                }
            }
            catch {
                // ignore
            }
        }
    }
    catch {
        // glob failed
    }
    // Check for cell routing configs
    try {
        const routingFiles = await glob('**/cell-routing*', {
            cwd: projectPath,
            nodir: true,
            ignore: ['node_modules/**'],
        });
        if (routingFiles.length > 0) {
            indicators.push('cell-routing-config');
        }
    }
    catch {
        // ignore
    }
    if (indicators.length === 0) {
        return null;
    }
    return {
        name: 'cell-based',
        confidence: indicators.length >= 2 ? 'detected' : 'inferred',
        indicators,
        cells: [],
    };
}
// === Data Mesh ===
/** Detects Data Mesh from domain manifests and data product files */
export async function detectDataMesh(projectPath) {
    const indicators = [];
    // Check for domains/ directory with manifest files
    try {
        const manifests = await glob('**/+(domain-manifest|dataproduct|datahub|openmetadata).+(yaml|yml)', {
            cwd: projectPath,
            nodir: true,
            ignore: ['node_modules/**'],
        });
        if (manifests.length > 0) {
            indicators.push(`data-mesh-manifest-files: ${manifests.slice(0, 3).join(', ')}`);
        }
    }
    catch {
        // ignore
    }
    // Check domains/ directory
    try {
        const domainDirs = await glob('domains/*/', { cwd: projectPath });
        if (domainDirs.length >= 2) {
            indicators.push(`domains-directory with ${domainDirs.length} domain(s)`);
        }
    }
    catch {
        // ignore
    }
    // Check package.json for data catalog dependencies
    try {
        const pkg = await readFile(join(projectPath, 'package.json'), 'utf-8');
        const dataMeshDeps = ['datahub-client', 'openmetadata-sdk', 'atlan-sdk'];
        for (const dep of dataMeshDeps) {
            if (pkg.includes(dep)) {
                indicators.push(`data-catalog-dependency: ${dep}`);
            }
        }
    }
    catch {
        // ignore
    }
    // Check README for data mesh keywords
    try {
        const readme = await readFile(join(projectPath, 'README.md'), 'utf-8');
        if (/data\s+mesh|data\s+product|domain\s+owner|output\s+port/i.test(readme)) {
            indicators.push('readme-data-mesh-keywords');
        }
    }
    catch {
        // ignore
    }
    if (indicators.length === 0) {
        return null;
    }
    return {
        name: 'data-mesh',
        confidence: indicators.length >= 2 ? 'detected' : 'inferred',
        indicators,
    };
}
// === BFF (Backend for Frontend) ===
/** Detects BFF pattern from multiple specialized backend directories */
export async function detectBff(projectPath) {
    const indicators = [];
    const bffPatterns = ['bff-web', 'bff-mobile', 'bff-tv', 'bff-desktop'];
    for (const pattern of bffPatterns) {
        try {
            const dirs = await glob(`${pattern}/`, { cwd: projectPath });
            if (dirs.length > 0) {
                indicators.push(`bff-directory: ${pattern}`);
            }
        }
        catch {
            // ignore
        }
    }
    // Check for services with -bff suffix in docker-compose
    try {
        const dockerCompose = await readFile(join(projectPath, 'docker-compose.yml'), 'utf-8');
        if (/-bff:/i.test(dockerCompose)) {
            indicators.push('docker-compose-bff-service');
        }
    }
    catch {
        // ignore
    }
    if (indicators.length < 2) {
        return null;
    }
    return {
        name: 'bff',
        confidence: 'detected',
        indicators,
    };
}
//# sourceMappingURL=cell-data-bff-detector.js.map