"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = update;
const devkit_1 = require("@nx/devkit");
const path_1 = require("path");
/**
 * Migration: Update @changesets/cli patch to also exclude local packages and
 * local applications from the changeset flow.
 *
 * The previous patch (shipped in 3.0.0) only filtered `unisphere-runtime-*`
 * packages out of the MAJOR bump prompt. This updated patch additionally
 * excludes local packages and local applications (anything under
 * `packages/local` / `applications/local`, or using the `@local/` scope) from
 * `getPackagesToRelease` — so they never appear in the "Which packages would
 * you like to include?" prompt and never get their own version bump. Runtimes
 * keep their existing behavior (dropped from the major prompt only).
 *
 * Replaces any existing @changesets/cli patch in the consuming project's
 * `patches/` directory with the updated one bundled alongside this migration.
 */
async function update(tree) {
    devkit_1.logger.info('🔄 Updating @changesets/cli patch');
    try {
        const patchesPath = 'patches';
        const patchesTemplatePath = (0, path_1.join)(__dirname, 'patches');
        const patchFileName = '@changesets+cli+2.29.7.patch';
        // Delete existing changeset patch if it exists
        if (tree.exists(patchesPath)) {
            const existingFiles = tree.children(patchesPath);
            for (const file of existingFiles) {
                if (file.includes('@changesets+cli')) {
                    devkit_1.logger.info(`🗑️ Removing existing patch: ${file}`);
                    tree.delete((0, path_1.join)(patchesPath, file));
                }
            }
        }
        // Copy the updated patch file
        (0, devkit_1.generateFiles)(tree, patchesTemplatePath, `./${patchesPath}`, {});
        devkit_1.logger.info(`✅ Updated patch: ${patchFileName}`);
        devkit_1.logger.info('ℹ️  This patch hides local packages and local applications from the changeset prompt, and keeps unisphere-runtime-* packages out of the major bump selection');
    }
    catch (error) {
        devkit_1.logger.error(`❌ Failed to update patch: ${error?.message || 'Unknown error'}`);
        throw error;
    }
}
