"use strict";
/**
 * Migration: Update ESLint dependency-checks ignoredFiles
 *
 * Updates the @nx/dependency-checks ignoredFiles patterns in .eslintrc.json
 * to use glob patterns that cover all vite/rollup config file extensions
 * (ts, mts, js, mjs). Previously only vite.config.ts and rollup.config.js
 * were ignored, causing false positives when projects use .mts or .mjs extensions.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = update;
const devkit_1 = require("@nx/devkit");
const OLD_VITE_PATTERN = '**/vite.config.ts';
const OLD_ROLLUP_PATTERN = '**/rollup.config.js';
const NEW_VITE_PATTERN = '**/vite.config.{ts,mts,js,mjs}';
const NEW_ROLLUP_PATTERN = '**/rollup.config.{js,mjs}';
async function update(tree) {
    devkit_1.logger.info('🔄 Updating ESLint dependency-checks ignoredFiles patterns...');
    const eslintrcPath = '.eslintrc.json';
    if (!tree.exists(eslintrcPath)) {
        devkit_1.logger.info('ℹ️ No .eslintrc.json found, skipping migration');
        return;
    }
    const eslintrc = (0, devkit_1.readJson)(tree, eslintrcPath);
    if (!eslintrc.overrides || !Array.isArray(eslintrc.overrides)) {
        devkit_1.logger.info('ℹ️ No overrides found in .eslintrc.json, skipping migration');
        return;
    }
    let updated = false;
    for (const override of eslintrc.overrides) {
        const depChecks = override.rules?.['@nx/dependency-checks'];
        if (!depChecks || !Array.isArray(depChecks))
            continue;
        const options = depChecks[1];
        if (!options?.ignoredFiles || !Array.isArray(options.ignoredFiles))
            continue;
        const ignoredFiles = options.ignoredFiles;
        // Check if already using new patterns
        if (ignoredFiles.includes(NEW_VITE_PATTERN) &&
            ignoredFiles.includes(NEW_ROLLUP_PATTERN)) {
            devkit_1.logger.info('✅ ESLint ignoredFiles already using updated patterns, skipping');
            return;
        }
        // Replace old patterns with new ones
        const newIgnoredFiles = ignoredFiles.map((pattern) => {
            if (pattern === OLD_VITE_PATTERN)
                return NEW_VITE_PATTERN;
            if (pattern === OLD_ROLLUP_PATTERN)
                return NEW_ROLLUP_PATTERN;
            return pattern;
        });
        options.ignoredFiles = newIgnoredFiles;
        updated = true;
    }
    if (updated) {
        (0, devkit_1.writeJson)(tree, eslintrcPath, eslintrc);
        devkit_1.logger.info('✅ Updated ignoredFiles to cover all vite/rollup config extensions');
    }
    else {
        devkit_1.logger.info('ℹ️ No @nx/dependency-checks ignoredFiles found to update');
    }
}
