"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DependencyHealthChecker = void 0;
class DependencyHealthChecker {
    check(dependencies, latestVersions) {
        const reports = [];
        for (const [name, current] of Object.entries(dependencies)) {
            const cleanCurrent = current.replace(/^[\^~]/, '');
            const latest = latestVersions[name] || cleanCurrent;
            const status = this.getStatus(cleanCurrent, latest);
            const breaking = this.hasBreakingChanges(cleanCurrent, latest);
            reports.push({
                name,
                current: cleanCurrent,
                latest,
                status,
                breakingChanges: breaking,
                updatePlan: this.generateUpdatePlan(name, cleanCurrent, latest, status, breaking),
            });
        }
        return reports.sort((a, b) => {
            const order = { deprecated: 0, major_behind: 1, minor_behind: 2, up_to_date: 3 };
            return order[a.status] - order[b.status];
        });
    }
    getNonBreakingUpdates(reports) {
        return reports.filter((r) => !r.breakingChanges && r.status !== 'up_to_date');
    }
    getFullUpdatePlan(reports) {
        const nonBreaking = this.getNonBreakingUpdates(reports);
        const breaking = reports.filter((r) => r.breakingChanges);
        let plan = '📦 Dependency Update Plan\n\n';
        if (nonBreaking.length > 0) {
            plan += `✅ Safe Updates (${nonBreaking.length}):\n`;
            for (const r of nonBreaking) {
                plan += `   • ${r.name}: ${r.current} → ${r.latest}\n`;
            }
        }
        if (breaking.length > 0) {
            plan += `\n⚠️  Breaking Changes (${breaking.length}):\n`;
            for (const r of breaking) {
                plan += `   • ${r.name}: ${r.current} → ${r.latest} (BREAKING)\n`;
            }
        }
        return plan;
    }
    getStats(reports) {
        return {
            total: reports.length,
            outdated: reports.filter((r) => r.status !== 'up_to_date').length,
            deprecated: reports.filter((r) => r.status === 'deprecated').length,
            upToDate: reports.filter((r) => r.status === 'up_to_date').length,
        };
    }
    getStatus(current, latest) {
        const curClean = current.split('-')[0];
        const latClean = latest.split('-')[0];
        if (curClean === latClean)
            return 'up_to_date';
        const [curMajor, curMinor] = curClean.split('.').map(Number);
        const [latMajor, latMinor] = latClean.split('.').map(Number);
        if (isNaN(curMajor) || isNaN(latMajor))
            return 'up_to_date';
        if (latMajor > curMajor + 1)
            return 'deprecated';
        if (latMajor > curMajor)
            return 'major_behind';
        if (latMinor > curMinor)
            return 'minor_behind';
        return 'up_to_date';
    }
    hasBreakingChanges(current, latest) {
        const curClean = current.split('-')[0];
        const latClean = latest.split('-')[0];
        const [curMajor, curMinor] = curClean.split('.').map(Number);
        const [latMajor, latMinor] = latClean.split('.').map(Number);
        if (isNaN(curMajor) || isNaN(latMajor))
            return false;
        if (latMajor > curMajor)
            return true;
        if (curMajor === 0 && latMinor > curMinor)
            return true;
        return false;
    }
    generateUpdatePlan(name, current, latest, status, breaking) {
        if (status === 'up_to_date')
            return `${name} is up to date.`;
        if (breaking)
            return `${name}: ${current} → ${latest}. BREAKING — review changelog first.`;
        return `${name}: ${current} → ${latest}. Safe update. Run: npm install ${name}@${latest}`;
    }
}
exports.DependencyHealthChecker = DependencyHealthChecker;
//# sourceMappingURL=dependency-health-checker.js.map