"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = update;
const devkit_1 = require("@nx/devkit");
/**
 * Migration: Fix npm workspaces pattern to use explicit includes
 *
 * This migration updates the root package.json workspaces array to use
 * explicit include patterns instead of a catch-all pattern with exclusions.
 *
 * The problem with "unisphere/**" + "!unisphere/applications/documentation/**"
 * is that npm's negation patterns don't work reliably. This caused documentation
 * apps (like Docusaurus) to be unable to have their own node_modules.
 *
 * The fix is to use explicit patterns that only include what should be workspaces:
 * - unisphere/packages/**
 * - unisphere/runtimes/**
 * - unisphere/visuals/**
 * - unisphere/applications/local/**
 * - unisphere/applications/server/**
 *
 * This automatically excludes unisphere/applications/documentation/** without
 * needing a negation pattern.
 *
 * After all migrations complete, the user should run:
 *   rm -rf node_modules && npm run reset && npm ci && npm run check
 */
async function update(tree) {
    devkit_1.logger.info('🔄 Fixing npm workspaces pattern to use explicit includes');
    const packageJsonPath = 'package.json';
    if (!tree.exists(packageJsonPath)) {
        devkit_1.logger.warn('No package.json found, skipping migration');
        return [];
    }
    const packageJson = (0, devkit_1.readJson)(tree, packageJsonPath);
    if (!packageJson.workspaces || !Array.isArray(packageJson.workspaces)) {
        devkit_1.logger.warn('No workspaces array found in package.json, skipping migration');
        return [];
    }
    const oldWorkspaces = packageJson.workspaces;
    // Check if using the old catch-all pattern
    const hasCatchAllPattern = oldWorkspaces.includes('unisphere/**');
    const hasNegationPattern = oldWorkspaces.some((pattern) => pattern.startsWith('!'));
    if (!hasCatchAllPattern && !hasNegationPattern) {
        // Check if already using explicit patterns
        const expectedPatterns = [
            'unisphere/packages/**',
            'unisphere/runtimes/**',
            'unisphere/visuals/**',
            'unisphere/applications/local/**',
            'unisphere/applications/server/**',
        ];
        const hasAllExpectedPatterns = expectedPatterns.every(pattern => oldWorkspaces.includes(pattern));
        if (hasAllExpectedPatterns) {
            devkit_1.logger.info('Workspaces already using explicit patterns, skipping migration');
            return [];
        }
    }
    // Replace with explicit patterns
    const newWorkspaces = [
        'unisphere/packages/**',
        'unisphere/runtimes/**',
        'unisphere/visuals/**',
        'unisphere/applications/local/**',
        'unisphere/applications/server/**',
    ];
    packageJson.workspaces = newWorkspaces;
    (0, devkit_1.writeJson)(tree, packageJsonPath, packageJson);
    devkit_1.logger.info('✅ Updated workspaces from:');
    devkit_1.logger.info(`   ${JSON.stringify(oldWorkspaces)}`);
    devkit_1.logger.info('   to:');
    devkit_1.logger.info(`   ${JSON.stringify(newWorkspaces)}`);
    devkit_1.logger.info('');
    devkit_1.logger.info('This allows documentation apps (like Docusaurus) to have their own node_modules.');
    devkit_1.logger.info('');
    devkit_1.logger.info('⚠️  After all migrations complete, run the following commands:');
    devkit_1.logger.info('   rm -rf node_modules');
    devkit_1.logger.info('   npm run reset');
    devkit_1.logger.info('   npm ci');
    devkit_1.logger.info('   npm run check');
    return [];
}
