"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('🔄 Removing rollup-plugin-node-builtins dependency...');
    const packageJsonPath = 'package.json';
    if (!tree.exists(packageJsonPath)) {
        devkit_1.logger.warn('⚠️ package.json not found');
        throw new Error('package.json not found');
    }
    try {
        let dependencyRemoved = false;
        (0, devkit_1.updateJson)(tree, packageJsonPath, (packageJson) => {
            const targetDependency = 'rollup-plugin-node-builtins';
            // Check and remove from dependencies
            if (packageJson.dependencies && packageJson.dependencies[targetDependency]) {
                delete packageJson.dependencies[targetDependency];
                dependencyRemoved = true;
                devkit_1.logger.info(`📦 Removed ${targetDependency} from dependencies`);
            }
            // Check and remove from devDependencies
            if (packageJson.devDependencies && packageJson.devDependencies[targetDependency]) {
                delete packageJson.devDependencies[targetDependency];
                dependencyRemoved = true;
                devkit_1.logger.info(`📦 Removed ${targetDependency} from devDependencies`);
            }
            // Check and remove from peerDependencies
            if (packageJson.peerDependencies && packageJson.peerDependencies[targetDependency]) {
                delete packageJson.peerDependencies[targetDependency];
                dependencyRemoved = true;
                devkit_1.logger.info(`📦 Removed ${targetDependency} from peerDependencies`);
            }
            // Check and remove from optionalDependencies
            if (packageJson.optionalDependencies && packageJson.optionalDependencies[targetDependency]) {
                delete packageJson.optionalDependencies[targetDependency];
                dependencyRemoved = true;
                devkit_1.logger.info(`📦 Removed ${targetDependency} from optionalDependencies`);
            }
            return packageJson;
        });
        if (dependencyRemoved) {
            devkit_1.logger.info('✅ Successfully removed rollup-plugin-node-builtins dependency');
        }
        else {
            devkit_1.logger.info('ℹ️ rollup-plugin-node-builtins dependency not found');
        }
    }
    catch (error) {
        const errorMessage = error instanceof Error ? error.message : 'Unknown error';
        devkit_1.logger.error(`❌ Could not update package.json: ${errorMessage}`);
        throw new Error(`Could not update package.json: ${errorMessage}`);
    }
}
