"use strict";
/**
 * Migration: Update .gitignore to exclude Claude Code local settings
 *
 * Adds .claude/settings.local.json to .gitignore to prevent local Claude Code
 * settings from being committed to the repository.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = update;
const devkit_1 = require("@nx/devkit");
async function update(tree) {
    devkit_1.logger.info('🔄 Updating .gitignore to exclude Claude Code local settings...');
    const gitignorePath = '.gitignore';
    if (!tree.exists(gitignorePath)) {
        devkit_1.logger.warn('⚠️ .gitignore file not found, creating one');
        tree.write(gitignorePath, '');
    }
    try {
        const currentContent = tree.read(gitignorePath, 'utf-8') || '';
        let updatedContent = currentContent;
        // Check if .claude/settings.local.json is already in .gitignore
        const claudeSettingsRegex = /^\.claude\/settings\.local\.json\s*$/gm;
        if (!claudeSettingsRegex.test(updatedContent)) {
            // Add Claude Code local settings section and entry
            if (!updatedContent.includes('# Claude Code local settings')) {
                updatedContent =
                    updatedContent.trim() + '\n\n# Claude Code local settings\n';
            }
            if (!updatedContent.includes('.claude/settings.local.json')) {
                updatedContent += '.claude/settings.local.json\n';
                devkit_1.logger.info('📝 Added .claude/settings.local.json to .gitignore');
            }
            tree.write(gitignorePath, updatedContent);
            devkit_1.logger.info('✅ Successfully updated .gitignore');
        }
        else {
            devkit_1.logger.info('ℹ️ .claude/settings.local.json already exists in .gitignore');
        }
    }
    catch (error) {
        const errorMessage = error instanceof Error ? error.message : 'Unknown error';
        devkit_1.logger.error(`❌ Failed to update .gitignore: ${errorMessage}`);
        throw new Error(`Failed to update .gitignore: ${errorMessage}`);
    }
}
