"use strict";
/**
 * Migration: Add Claude Code configuration to root .gitignore
 *
 * Adds .claude/* and !.claude/skills entries to root .gitignore to exclude
 * Claude Code files while preserving skills directory.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = update;
const devkit_1 = require("@nx/devkit");
async function update(tree) {
    devkit_1.logger.info('🔄 Updating root .gitignore to add Claude Code configuration...');
    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;
        let hasChanges = false;
        // Check if .claude/* entry already exists
        const claudeWildcardRegex = /^\.claude\/\*\s*$/gm;
        if (!claudeWildcardRegex.test(updatedContent)) {
            // Add section comment if needed
            if (!updatedContent.includes('# Claude Code configuration')) {
                updatedContent = updatedContent.trim() + '\n\n# Claude Code configuration\n';
            }
            // Add .claude/* entry
            if (!updatedContent.includes('.claude/*')) {
                updatedContent += '.claude/*\n';
                devkit_1.logger.info('📝 Added .claude/* to .gitignore');
                hasChanges = true;
            }
        }
        else {
            devkit_1.logger.info('ℹ️ .claude/* already exists in .gitignore');
        }
        // Check if !.claude/skills entry already exists
        const claudeSkillsRegex = /^!\.claude\/skills\s*$/gm;
        if (!claudeSkillsRegex.test(updatedContent)) {
            // Add !.claude/skills entry
            if (!updatedContent.includes('!.claude/skills')) {
                updatedContent += '!.claude/skills\n';
                devkit_1.logger.info('📝 Added !.claude/skills to .gitignore');
                hasChanges = true;
            }
        }
        else {
            devkit_1.logger.info('ℹ️ !.claude/skills already exists in .gitignore');
        }
        if (hasChanges) {
            tree.write(gitignorePath, updatedContent);
            devkit_1.logger.info('✅ Successfully updated .gitignore');
        }
        else {
            devkit_1.logger.info('ℹ️ No changes needed - all entries already exist');
        }
    }
    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}`);
    }
}
