import { loadBuiltinCatalog, loadCustomCatalog, mergeCatalogPolicies, getLatestKnownMajor, } from './dynamic-migration-detector/catalog-loader.js';
import { assessMigrationRisk } from './dynamic-migration-detector/risk-assessor.js';
import { readNpmManifest, readPypiManifest, readCargoManifest, readGoManifest, readMavenManifest, readComposerManifest, } from './dynamic-migration-detector/manifest-readers.js';
import { readRubygemsManifest, readNugetManifest, readPubManifest, readSwiftPMManifest, } from './dynamic-migration-detector/manifest-readers-extended.js';
// Re-export plan generator for callers that need it directly.
export { generateMigrationPlan } from './dynamic-migration-detector/plan-generator.js';
/** Detect pending major migrations for one ecosystem using its manifest map. */
async function detectForEcosystem(projectPath, ecosystem, catalog) {
    let manifestMap;
    switch (ecosystem) {
        case 'npm':
            manifestMap = await readNpmManifest(projectPath);
            break;
        case 'pypi':
            manifestMap = await readPypiManifest(projectPath);
            break;
        case 'cargo':
            manifestMap = await readCargoManifest(projectPath);
            break;
        case 'go':
            manifestMap = await readGoManifest(projectPath);
            break;
        case 'maven':
            manifestMap = await readMavenManifest(projectPath);
            break;
        case 'composer':
            manifestMap = await readComposerManifest(projectPath);
            break;
        case 'rubygems':
            manifestMap = await readRubygemsManifest(projectPath);
            break;
        case 'nuget':
            manifestMap = await readNugetManifest(projectPath);
            break;
        case 'pub':
            manifestMap = await readPubManifest(projectPath);
            break;
        case 'swiftpm':
            manifestMap = await readSwiftPMManifest(projectPath);
            break;
    }
    const pending = [];
    for (const [pkg, currentMajor] of manifestMap) {
        const latestMajor = getLatestKnownMajor(catalog, ecosystem, pkg);
        if (latestMajor === null || latestMajor <= currentMajor) {
            continue;
        }
        const { risk, reason, migrationWindow } = assessMigrationRisk(catalog, ecosystem, pkg, currentMajor, latestMajor);
        pending.push({
            ecosystem,
            package: pkg,
            currentMajor,
            latestMajor,
            risk,
            reason,
            migrationWindow,
        });
    }
    return pending;
}
/** Build a human-readable summary string. */
function buildSummary(pending) {
    if (pending.length === 0) {
        return 'No pending major version migrations detected.';
    }
    const criticalCount = pending.filter((p) => p.risk === 'critical').length;
    const highCount = pending.filter((p) => p.risk === 'high').length;
    const parts = [
        `${String(pending.length)} pending migration(s)`,
        criticalCount > 0 ? `${String(criticalCount)} critical` : null,
        highCount > 0 ? `${String(highCount)} high` : null,
    ].filter((p) => p !== null);
    return parts.join(', ') + '.';
}
/**
 * Detect all pending major version migrations across all supported ecosystems.
 * Uses the bundled migration-policies-catalog.json — no live API calls.
 */
export async function detectPendingMajorMigrations(projectPath) {
    const builtinCatalog = loadBuiltinCatalog();
    const customPolicies = await loadCustomCatalog(projectPath);
    const catalog = mergeCatalogPolicies(builtinCatalog, customPolicies);
    const ecosystems = [
        'npm',
        'pypi',
        'cargo',
        'go',
        'maven',
        'composer',
        'rubygems',
        'nuget',
        'pub',
        'swiftpm',
    ];
    const results = await Promise.all(ecosystems.map((eco) => detectForEcosystem(projectPath, eco, catalog)));
    const pending = results.flat();
    const criticalCount = pending.filter((p) => p.risk === 'critical').length;
    return {
        projectPath,
        pending,
        totalPending: pending.length,
        criticalCount,
        summary: buildSummary(pending),
    };
}
//# sourceMappingURL=dynamic-migration-detector.js.map