"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = removeDistributionChannel;
const devkit_1 = require("@nx/devkit");
/**
 * Migration: Remove distributionChannel property from packages and update schema version
 *
 * After the reorganize-packages-by-distribution-channel migration, the distributionChannel
 * property for packages is redundant and can be inferred from the folder path:
 *
 * Packages:
 * - unisphere/packages/private/* → distributionChannel: "none"
 * - unisphere/packages/corp/* → distributionChannel: "github"
 * - unisphere/packages/public/* → distributionChannel: "npm"
 *
 * Runtimes & Applications:
 * - Keep distributionChannel property (cannot be inferred from path)
 *
 * This migration:
 * 1. Removes the distributionChannel property only from packages in .unisphere
 * 2. Updates schemaVersion from "1.0.0" to "2.0.0"
 */
async function removeDistributionChannel(tree) {
    devkit_1.logger.info('');
    devkit_1.logger.info('========================================');
    devkit_1.logger.info('Upgrading .unisphere to schema version 2.0.0');
    devkit_1.logger.info('========================================');
    devkit_1.logger.info('');
    // Check if .unisphere file exists
    if (!tree.exists('.unisphere')) {
        devkit_1.logger.warn('⚠️  .unisphere file not found. Skipping migration.');
        return;
    }
    // Read current configuration
    let config;
    try {
        config = (0, devkit_1.readJson)(tree, '.unisphere');
    }
    catch (error) {
        devkit_1.logger.error(`❌ Failed to read .unisphere: ${error instanceof Error ? error.message : 'Unknown error'}`);
        throw error;
    }
    // Track changes
    let totalRemoved = 0;
    let schemaUpdated = false;
    // Update schema version from 1.0.0 to 2.0.0
    if (config.schemaVersion === '1.0.0') {
        config.schemaVersion = '2.0.0';
        schemaUpdated = true;
        devkit_1.logger.info('✅ Updated schemaVersion: 1.0.0 → 2.0.0');
        devkit_1.logger.info('');
    }
    else if (config.schemaVersion === '2.0.0') {
        devkit_1.logger.info('ℹ️  Schema already at version 2.0.0');
        devkit_1.logger.info('');
    }
    else {
        devkit_1.logger.warn(`⚠️  Unknown schemaVersion: ${config.schemaVersion}. Expected 1.0.0 or 2.0.0`);
        devkit_1.logger.info('');
    }
    // Process packages)
    if (config.elements?.packages) {
        for (const packageName in config.elements.packages) {
            const packageConfig = config.elements.packages[packageName];
            if ('distributionChannel' in packageConfig) {
                delete packageConfig.distributionChannel;
                totalRemoved++;
            }
        }
    }
    // Process applications 
    if (config.elements?.applications) {
        for (const applicationName in config.elements.applications) {
            const applicationConfig = config.elements.applications[applicationName];
            if ('distributionChannel' in applicationConfig) {
                delete applicationConfig.distributionChannel;
                totalRemoved++;
            }
        }
    }
    // Write updated configuration back
    if (totalRemoved > 0 || schemaUpdated) {
        try {
            (0, devkit_1.writeJson)(tree, '.unisphere', config);
            if (totalRemoved > 0) {
                devkit_1.logger.info('✅ Successfully removed distributionChannel property from packages');
                devkit_1.logger.info('');
                devkit_1.logger.info(`Removed distributionChannel from ${totalRemoved} package(s)`);
                devkit_1.logger.info('');
                devkit_1.logger.info('ℹ️  Distribution channel for packages can now be inferred from folder paths:');
                devkit_1.logger.info('   - packages/private/* → "none"');
                devkit_1.logger.info('   - packages/corp/* → "github"');
                devkit_1.logger.info('   - packages/public/* → "npm"');
                devkit_1.logger.info('');
                devkit_1.logger.info('ℹ️  Runtimes and applications still have distributionChannel property');
            }
            else if (schemaUpdated) {
                devkit_1.logger.info('ℹ️  No distributionChannel properties found in packages.');
                devkit_1.logger.info('ℹ️  Only schema version was updated.');
            }
        }
        catch (error) {
            devkit_1.logger.error(`❌ Failed to write .unisphere: ${error instanceof Error ? error.message : 'Unknown error'}`);
            throw error;
        }
    }
    else {
        devkit_1.logger.info('ℹ️  No changes needed. Schema already at 2.0.0 and no distributionChannel properties found.');
    }
    devkit_1.logger.info('');
    devkit_1.logger.info('========================================');
    devkit_1.logger.info('Migration complete');
    devkit_1.logger.info('========================================');
    devkit_1.logger.info('');
}
