"use strict";
/**
 * Migration: Disable @nx/enforce-module-boundaries for rollup.config files
 *
 * Under nx 22.7.x the @nx/enforce-module-boundaries rule started flagging the
 * relative `require('../../node_modules/@unisphere/cli/bundler/.../rollup.js')`
 * that every generated rollup.config.js uses:
 *   "External resources cannot be imported using a relative or absolute path"
 *
 * That require is intentional — @unisphere/cli's `exports` map does not expose
 * the bundler entry, so a bare specifier cannot resolve. rollup.config.js is a
 * build-config file, not application code, so the boundaries rule should not
 * apply to it. This migration adds an override to the workspace-root
 * .eslintrc.json that turns the rule off for rollup.config.{js,mjs} files.
 *
 * Idempotent: re-running makes no further changes.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = update;
const devkit_1 = require("@nx/devkit");
const ESLINTRC_PATH = '.eslintrc.json';
const ROLLUP_FILES_PATTERN = '**/rollup.config.{js,mjs}';
const RULE = '@nx/enforce-module-boundaries';
async function update(tree) {
    devkit_1.logger.info(`🔄 Disabling ${RULE} for rollup.config files in ${ESLINTRC_PATH}...`);
    if (!tree.exists(ESLINTRC_PATH)) {
        devkit_1.logger.info(`ℹ️ No ${ESLINTRC_PATH} found, skipping migration`);
        return;
    }
    const eslintrc = (0, devkit_1.readJson)(tree, ESLINTRC_PATH);
    if (!Array.isArray(eslintrc.overrides)) {
        eslintrc.overrides = [];
    }
    const overrides = eslintrc.overrides;
    // Idempotency: skip if an override already turns the rule off for rollup configs
    const alreadyDisabled = overrides.some((override) => {
        const files = Array.isArray(override.files)
            ? override.files
            : override.files
                ? [override.files]
                : [];
        return (files.includes(ROLLUP_FILES_PATTERN) &&
            override.rules?.[RULE] === 'off');
    });
    if (alreadyDisabled) {
        devkit_1.logger.info(`✅ ${RULE} already disabled for rollup configs, skipping`);
        return;
    }
    // Append last so it wins over the earlier *.js override that enables the rule
    overrides.push({
        files: [ROLLUP_FILES_PATTERN],
        rules: {
            [RULE]: 'off',
        },
    });
    (0, devkit_1.writeJson)(tree, ESLINTRC_PATH, eslintrc);
    devkit_1.logger.info(`✅ Disabled ${RULE} for ${ROLLUP_FILES_PATTERN}`);
}
