// engine/migration/service-migrator.ts — External service migration (SPEC-028b)
// Detects, maps and plans migration of external services (queues, caches, storage, auth, etc.)
import { PROVIDER_CATALOG, SERVICE_EQUIVALENCES } from './service-migrator-catalogs.js';
export { planServicesMigration, generateServiceValidationChecklist, } from './service-migrator-planner.js';
// --- Detection helpers ---
function detectServiceFromKeywords(text) {
    const lower = text.toLowerCase();
    for (const entry of PROVIDER_CATALOG) {
        if (entry.keywords.some((kw) => lower.includes(kw))) {
            return {
                id: entry.provider,
                name: entry.provider.charAt(0).toUpperCase() + entry.provider.slice(1),
                type: entry.type,
                provider: entry.provider,
                critical: entry.riskLevel === 'stateful-critical',
                stateless: entry.stateless,
                riskLevel: entry.riskLevel,
                detectedIn: [],
            };
        }
    }
    return null;
}
function deduplicateServices(services) {
    const seen = new Map();
    for (const svc of services) {
        const existing = seen.get(svc.id);
        if (existing) {
            existing.detectedIn = [...new Set([...existing.detectedIn, ...svc.detectedIn])];
        }
        else {
            seen.set(svc.id, { ...svc });
        }
    }
    return [...seen.values()];
}
// --- Public API ---
export function analyzeExternalServices(params) {
    const detected = [];
    for (const dep of params.dependencies) {
        const svc = detectServiceFromKeywords(dep);
        if (svc) {
            detected.push({ ...svc, detectedIn: [dep] });
        }
    }
    // Also scan the stack string itself for common services
    const stackSvc = detectServiceFromKeywords(params.sourceStack);
    if (stackSvc) {
        detected.push({ ...stackSvc, detectedIn: [params.sourceStack] });
    }
    const services = deduplicateServices(detected);
    const criticalCount = services.filter((s) => s.critical).length;
    return {
        services,
        totalDetected: services.length,
        criticalCount,
        analyzedAt: new Date().toISOString(),
    };
}
export function buildServiceEquivalenceMap(sourceStack, targetStack, services) {
    const equivalences = [];
    const unmappedServices = [];
    for (const svc of services) {
        const entry = SERVICE_EQUIVALENCES.find((e) => e.source === svc.provider || e.type === svc.type);
        if (entry) {
            const adapter = {
                interfaceName: entry.adapterInterfaceName,
                methods: entry.methods,
                sourceImplementation: entry.sourceImpl,
                targetImplementation: entry.targetImpl,
                portPattern: 'ports-and-adapters',
            };
            equivalences.push({
                sourceService: svc.provider,
                targetService: entry.target,
                serviceType: svc.type,
                behaviorDifferences: entry.differences,
                adapterSpec: adapter,
                migrationComplexity: entry.complexity,
            });
        }
        else {
            unmappedServices.push(svc.provider);
        }
    }
    return {
        sourceStack,
        targetStack,
        equivalences,
        unmappedServices,
        generatedAt: new Date().toISOString(),
    };
}
//# sourceMappingURL=service-migrator.js.map