"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('🔄 Sync package json files deps...');
    let filesUpdated = 0;
    // Read the root package.json to get the target versions
    const rootPackageJson = tree.read('package.json', 'utf-8');
    if (!rootPackageJson) {
        devkit_1.logger.error('❌ Could not read root package.json');
        return;
    }
    const rootPackage = JSON.parse(rootPackageJson);
    const targetVersions = {
        ...rootPackage.dependencies,
        ...rootPackage.devDependencies
    };
    devkit_1.logger.info(`📦 Found ${Object.keys(targetVersions).length} dependencies to sync`);
    // Find all package.json files in packages directory
    (0, devkit_1.visitNotIgnoredFiles)(tree, '.', (filePath) => {
        if (filePath.endsWith('package.json')) {
            const content = tree.read(filePath, 'utf-8');
            if (content) {
                try {
                    const packageJson = JSON.parse(content);
                    let hasChanges = false;
                    // Sync dependencies
                    if (packageJson.dependencies) {
                        if (packageJson.dependencies["@nextgis/cancelable-promise"]) {
                            devkit_1.logger.info(`🔄 deleting dependency '@nextgis/cancelable-promise'`);
                            delete packageJson.dependencies["@nextgis/cancelable-promise"];
                            hasChanges = true;
                        }
                        for (const [depName, currentVersion] of Object.entries(packageJson.dependencies)) {
                            if (targetVersions[depName] && currentVersion !== targetVersions[depName]) {
                                devkit_1.logger.info(`🔄 Updating ${depName} from ${currentVersion} to ${targetVersions[depName]} in ${filePath}`);
                                packageJson.dependencies[depName] = targetVersions[depName];
                                hasChanges = true;
                            }
                        }
                    }
                    // Sync devDependencies
                    if (packageJson.devDependencies) {
                        if (packageJson.devDependencies["@nextgis/cancelable-promise"]) {
                            devkit_1.logger.info(`🔄 deleting devDependencies '@nextgis/cancelable-promise'`);
                            delete packageJson.devDependencies["@nextgis/cancelable-promise"];
                            hasChanges = true;
                        }
                        for (const [depName, currentVersion] of Object.entries(packageJson.devDependencies)) {
                            if (targetVersions[depName] && currentVersion !== targetVersions[depName]) {
                                devkit_1.logger.info(`🔄 Updating ${depName} from ${currentVersion} to ${targetVersions[depName]} in ${filePath}`);
                                packageJson.devDependencies[depName] = targetVersions[depName];
                                hasChanges = true;
                            }
                        }
                    }
                    if (hasChanges) {
                        // Write the updated package.json back
                        const updatedContent = JSON.stringify(packageJson, null, 2) + '\n';
                        tree.write(filePath, updatedContent);
                        filesUpdated++;
                        devkit_1.logger.info(`✅ Updated ${filePath}`);
                    }
                }
                catch (error) {
                    devkit_1.logger.error(`❌ Error parsing ${filePath}: ${error}`);
                }
            }
        }
    });
    if (filesUpdated > 0) {
        await (0, devkit_1.formatFiles)(tree);
    }
    else {
        devkit_1.logger.info('ℹ️ No package.json files needed updating.');
    }
}
