"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.initCommand = initCommand;
const chalk_1 = __importDefault(require("chalk"));
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const inquirer_1 = __importDefault(require("inquirer"));
const install_rules_1 = require("../utils/install-rules");
async function initCommand(options) {
    console.log(chalk_1.default.bold.blue('\n🚀 Initializing Augment Extensions\n'));
    try {
        // Check if already initialized
        const augmentDir = path.join(process.cwd(), '.augment');
        const extensionsConfig = path.join(augmentDir, 'extensions.json');
        if (fs.existsSync(extensionsConfig)) {
            const { overwrite } = await inquirer_1.default.prompt([
                {
                    type: 'confirm',
                    name: 'overwrite',
                    message: 'Augment Extensions already initialized. Overwrite?',
                    default: false
                }
            ]);
            if (!overwrite) {
                console.log(chalk_1.default.yellow('Initialization cancelled.'));
                return;
            }
        }
        // Create .augment directory if it doesn't exist
        if (!fs.existsSync(augmentDir)) {
            fs.mkdirSync(augmentDir, { recursive: true });
            console.log(chalk_1.default.green('✓ Created .augment directory'));
        }
        // Create extensions.json
        const config = {
            version: '0.1.0',
            modules: [],
            settings: {
                autoUpdate: false,
                checkUpdatesOnInit: true
            }
        };
        fs.writeFileSync(extensionsConfig, JSON.stringify(config, null, 2));
        console.log(chalk_1.default.green('✓ Created extensions.json'));
        // Update or create AGENTS.md
        const agentsPath = path.join(process.cwd(), 'AGENTS.md');
        const agentsContent = `
# Augment Extensions Integration

This project uses Augment Extensions for additional AI coding guidelines.

## For AI Agents

Use the \`augx\` CLI to discover and apply extension modules:

\`\`\`bash
# List linked modules
augx list --linked

# Show module details
augx show <module-name>

# Search for modules
augx search <keyword>
\`\`\`

## Linked Modules

Check \`.augment/extensions.json\` for currently linked modules.
`;
        if (fs.existsSync(agentsPath)) {
            const existing = fs.readFileSync(agentsPath, 'utf-8');
            if (!existing.includes('Augment Extensions')) {
                fs.appendFileSync(agentsPath, '\n' + agentsContent);
                console.log(chalk_1.default.green('✓ Updated AGENTS.md'));
            }
        }
        else {
            fs.writeFileSync(agentsPath, agentsContent.trim());
            console.log(chalk_1.default.green('✓ Created AGENTS.md'));
        }
        // Create .gitignore entry if needed
        const gitignorePath = path.join(process.cwd(), '.gitignore');
        if (fs.existsSync(gitignorePath)) {
            const gitignore = fs.readFileSync(gitignorePath, 'utf-8');
            if (!gitignore.includes('.augment/extensions.json')) {
                fs.appendFileSync(gitignorePath, '\n# Augment Extensions\n.augment/extensions.json\n');
                console.log(chalk_1.default.green('✓ Updated .gitignore'));
            }
        }
        // Install character count management rule
        console.log(chalk_1.default.blue('\n📏 Installing character count management rule...\n'));
        const ruleResult = await (0, install_rules_1.installCharacterCountRule)({
            targetDir: process.cwd(),
            skipIfExists: true,
            verbose: true
        });
        if (!ruleResult.success) {
            console.log(chalk_1.default.yellow(`⚠ Warning: Could not install character count rule: ${ruleResult.error}`));
            console.log(chalk_1.default.gray('You can manually copy the rule from .augment/rules/character-count-management.md'));
        }
        console.log(chalk_1.default.bold.green('\n✨ Augment Extensions initialized successfully!\n'));
        console.log(chalk_1.default.gray('Next steps:'));
        console.log(chalk_1.default.gray('  1. Link modules: augx link coding-standards/typescript'));
        console.log(chalk_1.default.gray('  2. View modules: augx list'));
        console.log(chalk_1.default.gray('  3. Show details: augx show <module-name>\n'));
    }
    catch (error) {
        console.error(chalk_1.default.red('Error during initialization:'), error);
        process.exit(1);
    }
}
//# sourceMappingURL=init.js.map