"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('🔄 Checking .npmrc for legacy-peer-deps=true...');
    let filesScanned = 0;
    let filesUpdated = 0;
    const updateContent = (content) => {
        const re = /(\blegacy-peer-deps\b\s*=\s*)true(\b|\s*)/gi;
        if (re.test(content)) {
            // Replace only the value while preserving whitespace and formatting
            const replaced = content.replace(re, '$1false$2');
            return replaced;
        }
        return null;
    };
    // Prefer root .npmrc, but scan workspace to be safe
    const tryUpdateFile = (filePath) => {
        const content = tree.read(filePath, 'utf-8');
        if (!content)
            return;
        filesScanned++;
        const updated = updateContent(content);
        if (updated && updated !== content) {
            tree.write(filePath, updated);
            filesUpdated++;
            devkit_1.logger.info(`✅ Updated ${filePath}: legacy-peer-deps=false`);
        }
    };
    if (tree.exists('.npmrc')) {
        tryUpdateFile('.npmrc');
    }
    (0, devkit_1.visitNotIgnoredFiles)(tree, '.', (p) => {
        if (p.endsWith('/.npmrc') || p === '.npmrc') {
            if (p !== '.npmrc') {
                tryUpdateFile(p);
            }
        }
    });
    await (0, devkit_1.formatFiles)(tree);
    if (filesUpdated === 0) {
        devkit_1.logger.info('ℹ️ No .npmrc files required changes.');
    }
    else {
        devkit_1.logger.warn(`✅ Updated ${filesUpdated} .npmrc file(s).`);
    }
}
