"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = update;
const devkit_1 = require("@nx/devkit");
/**
 * Migration: Remove publishConfig from package.json files in packages
 *
 * This migration removes the publishConfig section from package.json files
 * in packages only (not applications, runtimes, or visuals).
 */
async function update(tree) {
    devkit_1.logger.info('🔄 Removing publishConfig from package.json files in packages');
    if (!tree.exists('.unisphere')) {
        devkit_1.logger.warn('No .unisphere file found, skipping migration');
        return;
    }
    const unisphereConfig = (0, devkit_1.readJson)(tree, '.unisphere');
    const packagesConfig = unisphereConfig.elements?.packages || {};
    let updatedCount = 0;
    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 (packageJson.publishConfig) {
            delete packageJson.publishConfig;
            (0, devkit_1.writeJson)(tree, packageJsonPath, packageJson);
            devkit_1.logger.info(`✅ Removed publishConfig from ${packageJsonPath}`);
            updatedCount++;
        }
    }
    if (updatedCount === 0) {
        devkit_1.logger.info('No packages had publishConfig to remove');
    }
    else {
        devkit_1.logger.info(`✅ Removed publishConfig from ${updatedCount} package(s)`);
    }
}
