// engine/resilience-detector/microservice-detector.ts — Microservice config detection (SPEC-034)
import { safeGlob, hasGlobMatches, readEnvContent, readSourceContent } from './helpers.js';
const TRACING_LIBRARIES = {
    opentelemetry: ['@opentelemetry/sdk-node', '@opentelemetry/api', 'opentelemetry-sdk'],
    jaeger: ['jaeger-client', 'jaegertracing'],
    zipkin: ['zipkin', 'zipkin-instrumentation-express'],
    datadog: ['dd-trace'],
    'aws-xray': ['aws-xray-sdk', '@aws-sdk/client-xray'],
};
function detectServiceDiscovery(hasKubernetesManifests, hasServiceUrlEnv, depsLower) {
    if (hasKubernetesManifests) {
        return 'kubernetes-dns';
    }
    if (depsLower.some((d) => d.includes('consul'))) {
        return 'consul';
    }
    if (depsLower.some((d) => d.includes('eureka'))) {
        return 'eureka';
    }
    if (hasServiceUrlEnv) {
        return 'direct-endpoint';
    }
    return null;
}
function detectTracingLibrary(depsLower) {
    for (const [name, patterns] of Object.entries(TRACING_LIBRARIES)) {
        if (patterns.some((p) => depsLower.some((d) => d.includes(p.toLowerCase())))) {
            return name;
        }
    }
    return null;
}
/**
 * Detects microservice configuration: service discovery, gRPC, tracing, health checks.
 */
export async function detectMicroserviceConfig(projectPath, deps) {
    const depsLower = deps.map((d) => d.toLowerCase());
    const protoFiles = await safeGlob(projectPath, '**/*.proto');
    const usesGrpc = protoFiles.length > 0;
    const hasKubernetesManifests = await hasGlobMatches(projectPath, [
        '**/k8s/**/*.yaml',
        '**/kubernetes/**/*.yaml',
        '**/manifests/**/*.yaml',
        'deployment.yaml',
        'service.yaml',
    ]);
    const envContent = await readEnvContent(projectPath);
    const hasServiceUrlEnv = envContent.includes('_SERVICE_HOST') ||
        envContent.includes('_SERVICE_URL') ||
        envContent.includes('_SERVICE_PORT') ||
        envContent.includes('SERVICE_URL=');
    const serviceDiscovery = detectServiceDiscovery(hasKubernetesManifests, hasServiceUrlEnv, depsLower);
    const signals = [
        usesGrpc,
        hasKubernetesManifests,
        serviceDiscovery !== null,
        depsLower.some((d) => d.includes('grpc') || d.includes('protobuf')),
        depsLower.some((d) => ['istio', 'linkerd', 'consul-connect', 'envoy'].some((mesh) => d.includes(mesh))),
    ].filter(Boolean).length;
    const isMicroservice = signals >= 2;
    const tracingLibrary = detectTracingLibrary(depsLower);
    const hasDistributedTracing = tracingLibrary !== null;
    const sourceContent = await readSourceContent(projectPath);
    const hasLivenessEndpoint = sourceContent.includes('/health') ||
        sourceContent.includes('/healthz') ||
        sourceContent.includes('liveness');
    const hasReadinessEndpoint = sourceContent.includes('/ready') ||
        sourceContent.includes('/readyz') ||
        sourceContent.includes('readiness');
    return {
        isMicroservice,
        serviceDiscovery,
        usesGrpc,
        hasDistributedTracing,
        tracingLibrary,
        hasLivenessEndpoint,
        hasReadinessEndpoint,
    };
}
//# sourceMappingURL=microservice-detector.js.map