"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = update;
const devkit_1 = require("@nx/devkit");
async function update(tree) {
    devkit_1.logger.info('🔄 Fixing @changesets/cli version in root package.json...');
    let filesUpdated = 0;
    const targetPackage = '@changesets/cli';
    const updatePackageJson = (filePath) => {
        const content = tree.read(filePath, 'utf-8');
        if (!content)
            return;
        try {
            const packageJson = JSON.parse(content);
            let modified = false;
            // Process dependencies
            if (packageJson.dependencies && packageJson.dependencies[targetPackage]) {
                const version = packageJson.dependencies[targetPackage];
                if (typeof version === 'string' && version.startsWith('^')) {
                    packageJson.dependencies[targetPackage] = version.substring(1);
                    modified = true;
                    devkit_1.logger.info(`  └─ ${targetPackage}: ${version} → ${packageJson.dependencies[targetPackage]}`);
                }
            }
            // Process devDependencies
            if (packageJson.devDependencies && packageJson.devDependencies[targetPackage]) {
                const version = packageJson.devDependencies[targetPackage];
                if (typeof version === 'string' && version.startsWith('^')) {
                    packageJson.devDependencies[targetPackage] = version.substring(1);
                    modified = true;
                    devkit_1.logger.info(`  └─ ${targetPackage}: ${version} → ${packageJson.devDependencies[targetPackage]}`);
                }
            }
            // Process peerDependencies
            if (packageJson.peerDependencies && packageJson.peerDependencies[targetPackage]) {
                const version = packageJson.peerDependencies[targetPackage];
                if (typeof version === 'string' && version.startsWith('^')) {
                    packageJson.peerDependencies[targetPackage] = version.substring(1);
                    modified = true;
                    devkit_1.logger.info(`  └─ ${targetPackage}: ${version} → ${packageJson.peerDependencies[targetPackage]}`);
                }
            }
            // Process optionalDependencies
            if (packageJson.optionalDependencies && packageJson.optionalDependencies[targetPackage]) {
                const version = packageJson.optionalDependencies[targetPackage];
                if (typeof version === 'string' && version.startsWith('^')) {
                    packageJson.optionalDependencies[targetPackage] = version.substring(1);
                    modified = true;
                    devkit_1.logger.info(`  └─ ${targetPackage}: ${version} → ${packageJson.optionalDependencies[targetPackage]}`);
                }
            }
            if (modified) {
                tree.write(filePath, JSON.stringify(packageJson, null, 2) + '\n');
                filesUpdated++;
                devkit_1.logger.info(`✅ Updated ${filePath}`);
            }
        }
        catch (error) {
            devkit_1.logger.warn(`⚠️ Failed to parse ${filePath}: ${error}`);
        }
    };
    // Update root package.json only
    if (tree.exists('package.json')) {
        updatePackageJson('package.json');
    }
    else {
        devkit_1.logger.warn('⚠️ No package.json found in root directory.');
    }
    await (0, devkit_1.formatFiles)(tree);
    if (filesUpdated === 0) {
        devkit_1.logger.info('ℹ️ No changes needed in package.json.');
    }
    else {
        devkit_1.logger.info(`✅ Fixed dependency versions in package.json.`);
    }
}
