import { parseBreakingChanges } from './breaking-change-parser.js';
import { analyzeImpact } from './impact-analyzer.js';
import { parseSemver } from '../stack-auditor/health-scorer.js';
// ---------------------------------------------------------------------------
// Risk assessment
// ---------------------------------------------------------------------------
function determineRiskLevel(fromVersion, toVersion, breakingChanges) {
    const from = parseSemver(fromVersion);
    const to = parseSemver(toVersion);
    const impactedCount = breakingChanges.filter((bc) => bc.impact === 'impacted').length;
    if (impactedCount > 0) {
        return 'breaking';
    }
    if (from && to && to.major > from.major) {
        return 'high';
    }
    if (breakingChanges.length > 5) {
        return 'high';
    }
    if (breakingChanges.length > 0) {
        return 'medium';
    }
    if (from && to && to.minor > from.minor) {
        return 'medium';
    }
    return 'low';
}
// ---------------------------------------------------------------------------
// Effort estimation
// ---------------------------------------------------------------------------
/** Estimate effort in hours based on breaking changes impact. */
export function estimateEffort(breakingChanges) {
    let hours = 0;
    for (const bc of breakingChanges) {
        if (bc.impact === 'impacted') {
            hours += 0.5;
        }
        else if (bc.impact === 'unknown') {
            hours += 0.1;
        }
    }
    return Math.max(0.5, Math.round(hours * 10) / 10);
}
// ---------------------------------------------------------------------------
// Plan builder
// ---------------------------------------------------------------------------
/** Build a structured UpgradePlan from parsed/analysed data. */
export function buildUpgradePlan(packageName, fromVer, toVer, breakingChanges) {
    const riskLevel = determineRiskLevel(fromVer, toVer, breakingChanges);
    const estimatedEffortHours = estimateEffort(breakingChanges);
    const isAutoSafe = riskLevel === 'low' && breakingChanges.length === 0;
    const steps = [
        `Update ${packageName} to ${toVer} in package.json`,
        'Run pnpm install (or npm/yarn install)',
        'Run full test suite',
        'Check for TypeScript compilation errors',
    ];
    if (breakingChanges.length > 0) {
        steps.splice(2, 0, `Fix ${breakingChanges.length} breaking change(s) — see list below`);
    }
    const testingChecklist = [
        'Unit tests pass',
        'Integration tests pass',
        `Verify ${packageName} API calls work as expected`,
        'Check for deprecation warnings in logs',
    ];
    return {
        packageName,
        fromVersion: fromVer,
        toVersion: toVer,
        riskLevel,
        estimatedEffortHours,
        isAutoSafe,
        breakingChanges,
        steps,
        rollbackProcedure: `Revert ${packageName} to ${fromVer} in package.json and run pnpm install`,
        testingChecklist,
    };
}
// ---------------------------------------------------------------------------
// Orchestrator
// ---------------------------------------------------------------------------
/**
 * Plan an upgrade for a specific package.
 * If no changelogText is provided, returns a minimal plan with a warning.
 */
export async function planUpgrade(projectPath, packageName, fromVersion, toVersion, changelogText) {
    if (!changelogText) {
        return buildUpgradePlan(packageName, fromVersion, toVersion, [
            {
                description: 'No changelog provided — manual review required before upgrading',
                affectedPattern: '',
                migrationAction: 'Consult the official changelog or release notes',
                impact: 'unknown',
            },
        ]);
    }
    const rawBreakingChanges = parseBreakingChanges(changelogText, fromVersion, toVersion);
    const analyzed = await analyzeImpact(projectPath, rawBreakingChanges);
    return buildUpgradePlan(packageName, fromVersion, toVersion, analyzed);
}
//# sourceMappingURL=index.js.map