// engine/detectors/otel-ecosystem-detectors.ts — SPEC-044
// Per-ecosystem OTel detection helpers extracted from otel-detector.ts.
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
// === OTel package patterns per ecosystem ===
/** npm package names that indicate OTel is installed */
export const OTEL_NPM_PACKAGES = [
    '@opentelemetry/api',
    '@opentelemetry/sdk-node',
    '@opentelemetry/core',
];
/** Python package patterns (requirements.txt, pyproject.toml) */
export const OTEL_PYTHON_PATTERNS = [
    'opentelemetry-api',
    'opentelemetry-sdk',
    'opentelemetry-sdk-trace',
];
/** Go module path */
export const OTEL_GO_MODULE = 'go.opentelemetry.io/otel';
/** Cargo.toml crate name */
export const OTEL_RUST_CRATE = 'opentelemetry';
/** Maven/Gradle artifact pattern */
export const OTEL_JAVA_PATTERN = 'opentelemetry';
/** Dependencies that indicate a project might need OTel (async workers, HTTP clients) */
export const MULTISERVICE_DEPS = [
    'axios',
    'httpx',
    'celery',
    'bull',
    'bullmq',
    'kafka-node',
    'kafkajs',
    '@nestjs/microservices',
    'amqplib',
    'nats',
];
// === Shared helper ===
export async function readFileOrNull(filePath) {
    try {
        return await readFile(filePath, 'utf-8');
    }
    catch {
        return null;
    }
}
export async function readPackageDeps(projectPath) {
    const content = await readFileOrNull(join(projectPath, 'package.json'));
    if (!content) {
        return null;
    }
    try {
        const pkg = JSON.parse(content);
        return { deps: pkg.dependencies ?? {}, devDeps: pkg.devDependencies ?? {} };
    }
    catch {
        return null;
    }
}
// === Per-ecosystem detection ===
/** Detect OTel for Node.js/TypeScript projects via package.json */
export async function detectNodeOtel(projectPath) {
    const pkg = await readPackageDeps(projectPath);
    if (!pkg) {
        return false;
    }
    const all = { ...pkg.deps, ...pkg.devDeps };
    return OTEL_NPM_PACKAGES.some((p) => p in all);
}
/** Detect OTel for Python projects via requirements.txt or pyproject.toml */
export async function detectPythonOtel(projectPath) {
    const reqContent = await readFileOrNull(join(projectPath, 'requirements.txt'));
    if (reqContent && OTEL_PYTHON_PATTERNS.some((p) => reqContent.includes(p))) {
        return true;
    }
    const pyprojectContent = await readFileOrNull(join(projectPath, 'pyproject.toml'));
    if (pyprojectContent && OTEL_PYTHON_PATTERNS.some((p) => pyprojectContent.includes(p))) {
        return true;
    }
    return false;
}
/** Detect OTel for Go projects via go.mod */
export async function detectGoOtel(projectPath) {
    const content = await readFileOrNull(join(projectPath, 'go.mod'));
    return content?.includes(OTEL_GO_MODULE) === true;
}
/** Detect OTel for Rust projects via Cargo.toml */
export async function detectRustOtel(projectPath) {
    const content = await readFileOrNull(join(projectPath, 'Cargo.toml'));
    return content?.includes(OTEL_RUST_CRATE) === true;
}
/** Detect OTel for Java/Kotlin projects via pom.xml or build.gradle */
export async function detectJavaOtel(projectPath) {
    const pomContent = await readFileOrNull(join(projectPath, 'pom.xml'));
    if (pomContent?.includes(OTEL_JAVA_PATTERN)) {
        return true;
    }
    const gradleContent = await readFileOrNull(join(projectPath, 'build.gradle'));
    if (gradleContent?.includes(OTEL_JAVA_PATTERN)) {
        return true;
    }
    const gradleKts = await readFileOrNull(join(projectPath, 'build.gradle.kts'));
    return gradleKts?.includes(OTEL_JAVA_PATTERN) === true;
}
//# sourceMappingURL=otel-ecosystem-detectors.js.map