"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = update;
const devkit_1 = require("@nx/devkit");
/**
 * Migration: Remove private field from package.json files in applications, runtimes, and packages
 *
 * This migration removes the "private" field from package.json files
 * in applications, runtimes, and packages.
 */
async function update(tree) {
    devkit_1.logger.info('🔄 Removing private field from package.json files in applications, runtimes, and packages');
    if (!tree.exists('.unisphere')) {
        devkit_1.logger.warn('No .unisphere file found, skipping migration');
        return;
    }
    const unisphereConfig = (0, devkit_1.readJson)(tree, '.unisphere');
    let updatedCount = 0;
    // Process applications
    const applicationsConfig = unisphereConfig.elements?.applications || {};
    for (const [applicationName, config] of Object.entries(applicationsConfig)) {
        const sourceRoot = config.sourceRoot;
        if (!sourceRoot) {
            devkit_1.logger.warn(`No sourceRoot found for application ${applicationName}, skipping`);
            continue;
        }
        const packageJsonPath = `${sourceRoot}/package.json`;
        if (!tree.exists(packageJsonPath)) {
            devkit_1.logger.warn(`No package.json found at ${packageJsonPath}, skipping`);
            continue;
        }
        const packageJson = (0, devkit_1.readJson)(tree, packageJsonPath);
        if ('private' in packageJson) {
            delete packageJson.private;
            (0, devkit_1.writeJson)(tree, packageJsonPath, packageJson);
            devkit_1.logger.info(`✅ Removed private from ${packageJsonPath}`);
            updatedCount++;
        }
    }
    // Process runtimes
    const runtimesConfig = unisphereConfig.elements?.runtimes || {};
    for (const [runtimeName, config] of Object.entries(runtimesConfig)) {
        const sourceRoot = config.sourceRoot;
        if (!sourceRoot) {
            devkit_1.logger.warn(`No sourceRoot found for runtime ${runtimeName}, skipping`);
            continue;
        }
        const packageJsonPath = `${sourceRoot}/package.json`;
        if (!tree.exists(packageJsonPath)) {
            devkit_1.logger.warn(`No package.json found at ${packageJsonPath}, skipping`);
            continue;
        }
        const packageJson = (0, devkit_1.readJson)(tree, packageJsonPath);
        if ('private' in packageJson) {
            delete packageJson.private;
            (0, devkit_1.writeJson)(tree, packageJsonPath, packageJson);
            devkit_1.logger.info(`✅ Removed private from ${packageJsonPath}`);
            updatedCount++;
        }
    }
    // Process packages
    const packagesConfig = unisphereConfig.elements?.packages || {};
    for (const [packageName, config] of Object.entries(packagesConfig)) {
        const sourceRoot = config.sourceRoot;
        if (!sourceRoot) {
            devkit_1.logger.warn(`No sourceRoot found for package ${packageName}, skipping`);
            continue;
        }
        const packageJsonPath = `${sourceRoot}/package.json`;
        if (!tree.exists(packageJsonPath)) {
            devkit_1.logger.warn(`No package.json found at ${packageJsonPath}, skipping`);
            continue;
        }
        const packageJson = (0, devkit_1.readJson)(tree, packageJsonPath);
        if ('private' in packageJson) {
            delete packageJson.private;
            (0, devkit_1.writeJson)(tree, packageJsonPath, packageJson);
            devkit_1.logger.info(`✅ Removed private from ${packageJsonPath}`);
            updatedCount++;
        }
    }
    if (updatedCount === 0) {
        devkit_1.logger.info('No applications, runtimes, or packages had private field to remove');
    }
    else {
        devkit_1.logger.info(`✅ Removed private field from ${updatedCount} package(s)`);
    }
}
