// engine/resilience-detector/ratelimit-detector.ts — Rate limit detection (SPEC-034)
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
/**
 * Detects rate limiting mechanism and algorithm from dependencies and project structure.
 */
export async function detectRateLimitMechanism(projectPath, deps) {
    const depsLower = deps.map((d) => d.toLowerCase());
    if (depsLower.includes('rate-limiter-flexible')) {
        return { mechanism: 'rate-limiter-flexible', algorithm: 'token-bucket', distributed: true };
    }
    if (depsLower.includes('@upstash/ratelimit')) {
        return { mechanism: '@upstash/ratelimit', algorithm: 'sliding-window', distributed: true };
    }
    if (depsLower.some((d) => d.includes('django-ratelimit'))) {
        return { mechanism: 'django-ratelimit', algorithm: 'fixed-window', distributed: false };
    }
    if (depsLower.some((d) => d.includes('flask-limiter'))) {
        return { mechanism: 'flask-limiter', algorithm: 'fixed-window', distributed: false };
    }
    if (depsLower.some((d) => d.includes('github.com/go-redis/redis_rate'))) {
        return { mechanism: 'go-redis/redis_rate', algorithm: 'sliding-window', distributed: true };
    }
    if (depsLower.includes('slowapi')) {
        return { mechanism: 'slowapi', algorithm: 'fixed-window', distributed: false };
    }
    if (depsLower.some((d) => d.includes('express-rate-limit'))) {
        return { mechanism: 'express-rate-limit', algorithm: 'fixed-window', distributed: false };
    }
    if (depsLower.some((d) => d.includes('golang.org/x/time/rate'))) {
        return {
            mechanism: 'golang.org/x/time/rate',
            algorithm: 'token-bucket',
            distributed: false,
        };
    }
    try {
        const nginxContent = await readFile(join(projectPath, 'nginx.conf'), 'utf-8');
        if (nginxContent.includes('limit_req_zone')) {
            return { mechanism: 'nginx', algorithm: 'leaky-bucket', distributed: true };
        }
    }
    catch {
        // no nginx config
    }
    try {
        await readFile(join(projectPath, 'kong.yml'), 'utf-8');
        return { mechanism: 'kong', algorithm: 'sliding-window', distributed: true };
    }
    catch {
        // no kong config
    }
    return { mechanism: null, algorithm: 'unknown', distributed: false };
}
//# sourceMappingURL=ratelimit-detector.js.map