// tools/migrate-tech/build-tool-handlers.ts — SPEC-050 S4
// Migration plan handlers for build tool migrations.
// C10: webpack-to-vite → structured migration plan from catalog
// C11: vite6-to-vite7 → structured migration plan from catalog
import { readFile } from 'node:fs/promises';
// ---------------------------------------------------------------------------
// Cache
// ---------------------------------------------------------------------------
let catalogCache = null;
/** Reset cache (for testing). */
export function resetBuildToolMigrationCatalogCache() {
    catalogCache = null;
}
async function loadCatalog() {
    if (catalogCache) {
        return catalogCache;
    }
    try {
        const configPath = new URL('../../config/build-tools-catalog.json', import.meta.url);
        const content = await readFile(configPath, 'utf-8');
        catalogCache = JSON.parse(content);
        return catalogCache;
    }
    catch {
        return {
            recommendations: {
                'new-project': { tool: 'vite', version: '>=7.0.0', justification: '', alternatives: [] },
            },
            migrations: {
                'webpack-to-vite': {
                    description: 'Migrate Webpack to Vite',
                    phases: [],
                    configEquivalences: {},
                    postMigrationChecklist: [],
                },
                'vite6-to-vite7': {
                    description: 'Migrate Vite 6 to Vite 7',
                    breakingChanges: [],
                    viteConfigDiff: { before: '', after: '' },
                    rollbackStrategy: '',
                    incompatiblePlugins: [],
                },
            },
        };
    }
}
// ---------------------------------------------------------------------------
// C10: webpack-to-vite
// ---------------------------------------------------------------------------
export async function handleWebpackToVite(args) {
    const catalog = await loadCatalog();
    const migration = catalog.migrations['webpack-to-vite'];
    const warnings = [
        'This migration plan is informational — it does not modify any project files.',
        'Review each phase carefully and test after each step.',
    ];
    return {
        action: 'webpack-to-vite',
        data: migration,
        message: `Webpack to Vite migration plan generated — ${migration.phases.length} phases, ${migration.postMigrationChecklist.length} post-migration checks.`,
        warnings,
        savedTo: args.projectId
            ? `data/projects/${args.projectId}/webpack-to-vite-plan.json`
            : undefined,
    };
}
// ---------------------------------------------------------------------------
// C11: vite6-to-vite7
// ---------------------------------------------------------------------------
export async function handleVite6ToVite7(args) {
    const catalog = await loadCatalog();
    const migration = catalog.migrations['vite6-to-vite7'];
    const warnings = [
        'This migration plan is informational — it does not modify any project files.',
        migration.rollbackStrategy
            ? `Rollback: ${migration.rollbackStrategy}`
            : 'Ensure you have a git tag before upgrading for easy rollback.',
    ];
    if (migration.incompatiblePlugins.length > 0) {
        warnings.push(`Known incompatible plugins: ${migration.incompatiblePlugins.join(', ')} — check before upgrading.`);
    }
    return {
        action: 'vite6-to-vite7',
        data: migration,
        message: `Vite 6 → Vite 7 migration plan generated — ${migration.breakingChanges.length} breaking changes documented.`,
        warnings,
        savedTo: args.projectId
            ? `data/projects/${args.projectId}/vite6-to-vite7-plan.json`
            : undefined,
    };
}
//# sourceMappingURL=build-tool-handlers.js.map