"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.guiCommand = guiCommand;
const chalk_1 = __importDefault(require("chalk"));
const inquirer_1 = __importDefault(require("inquirer"));
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const module_system_1 = require("../utils/module-system");
const link_1 = require("./link");
async function guiCommand(options = {}) {
    try {
        console.log(chalk_1.default.blue('\n🎨 Augment Extensions Module Manager\n'));
        // Check if initialized
        const configPath = path.join(process.cwd(), '.augment', 'extensions.json');
        if (!fs.existsSync(configPath)) {
            console.error(chalk_1.default.red('Augment Extensions not initialized. Run: augx init'));
            process.exit(1);
        }
        const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
        const linkedModules = config.modules.map((m) => m.name);
        // Discover available modules and collections
        const modules = (0, module_system_1.discoverModules)();
        const collections = (0, module_system_1.discoverCollections)();
        // Main menu
        const { action } = await inquirer_1.default.prompt([
            {
                type: 'list',
                name: 'action',
                message: 'What would you like to do?',
                choices: [
                    { name: '📦 Link Modules', value: 'link-modules' },
                    { name: '📚 Link Collection', value: 'link-collection' },
                    { name: '🔍 Search Modules', value: 'search' },
                    { name: '❌ Exit', value: 'exit' }
                ]
            }
        ]);
        if (action === 'exit') {
            console.log(chalk_1.default.gray('Goodbye!'));
            return;
        }
        if (action === 'link-modules') {
            await linkModulesInteractive(modules, linkedModules);
        }
        else if (action === 'link-collection') {
            await linkCollectionInteractive(collections, linkedModules);
        }
        else if (action === 'search') {
            await searchModulesInteractive(modules);
        }
    }
    catch (error) {
        console.error(chalk_1.default.red(`Error: ${error.message}`));
        process.exit(1);
    }
}
async function linkModulesInteractive(modules, linkedModules) {
    const choices = modules.map(m => ({
        name: `${m.metadata.displayName} (${m.fullName}) - ${m.metadata.description}`,
        value: m.fullName,
        checked: linkedModules.includes(m.fullName)
    }));
    const { selectedModules } = await inquirer_1.default.prompt([
        {
            type: 'checkbox',
            name: 'selectedModules',
            message: 'Select modules to link (use space to select, enter to confirm):',
            choices,
            pageSize: 15
        }
    ]);
    if (selectedModules.length === 0) {
        console.log(chalk_1.default.yellow('No modules selected.'));
        return;
    }
    console.log(chalk_1.default.blue(`\nLinking ${selectedModules.length} module(s)...\n`));
    for (const moduleName of selectedModules) {
        if (!linkedModules.includes(moduleName)) {
            await (0, link_1.linkCommand)(moduleName, {});
        }
        else {
            console.log(chalk_1.default.gray(`Skipping already linked module: ${moduleName}`));
        }
    }
    console.log(chalk_1.default.green('\n✓ Module linking complete!'));
}
async function linkCollectionInteractive(collections, linkedModules) {
    if (collections.length === 0) {
        console.log(chalk_1.default.yellow('No collections available.'));
        return;
    }
    const choices = collections.map(c => ({
        name: `${c.metadata.displayName} - ${c.metadata.description}`,
        value: c.fullName
    }));
    const { selectedCollection } = await inquirer_1.default.prompt([
        {
            type: 'list',
            name: 'selectedCollection',
            message: 'Select a collection to link:',
            choices
        }
    ]);
    const collection = collections.find(c => c.fullName === selectedCollection);
    if (!collection) {
        console.error(chalk_1.default.red('Collection not found.'));
        return;
    }
    console.log(chalk_1.default.blue(`\nLinking collection: ${collection.metadata.displayName}\n`));
    console.log(chalk_1.default.gray(`This will link ${collection.metadata.modules.length} module(s):\n`));
    for (const module of collection.metadata.modules) {
        console.log(chalk_1.default.gray(`  - ${module.id}`));
    }
    const { confirm } = await inquirer_1.default.prompt([
        {
            type: 'confirm',
            name: 'confirm',
            message: 'Proceed with linking?',
            default: true
        }
    ]);
    if (!confirm) {
        console.log(chalk_1.default.yellow('Cancelled.'));
        return;
    }
    // Link all modules in the collection
    for (const module of collection.metadata.modules) {
        if (!linkedModules.includes(module.id)) {
            await (0, link_1.linkCommand)(module.id, { version: module.version });
        }
        else {
            console.log(chalk_1.default.gray(`Skipping already linked module: ${module.id}`));
        }
    }
    console.log(chalk_1.default.green('\n✓ Collection linking complete!'));
}
async function searchModulesInteractive(modules) {
    const { searchTerm } = await inquirer_1.default.prompt([
        {
            type: 'input',
            name: 'searchTerm',
            message: 'Enter search term:'
        }
    ]);
    if (!searchTerm) {
        console.log(chalk_1.default.yellow('No search term provided.'));
        return;
    }
    const results = modules.filter(m => m.metadata.displayName.toLowerCase().includes(searchTerm.toLowerCase()) ||
        m.metadata.description.toLowerCase().includes(searchTerm.toLowerCase()) ||
        m.metadata.tags?.some((tag) => tag.toLowerCase().includes(searchTerm.toLowerCase())));
    if (results.length === 0) {
        console.log(chalk_1.default.yellow(`No modules found matching "${searchTerm}"`));
        return;
    }
    console.log(chalk_1.default.blue(`\nFound ${results.length} module(s):\n`));
    for (const module of results) {
        console.log(chalk_1.default.green(`  ${module.metadata.displayName}`));
        console.log(chalk_1.default.gray(`    ${module.fullName}`));
        console.log(chalk_1.default.gray(`    ${module.metadata.description}`));
        console.log();
    }
    const { linkNow } = await inquirer_1.default.prompt([
        {
            type: 'confirm',
            name: 'linkNow',
            message: 'Would you like to link any of these modules?',
            default: false
        }
    ]);
    if (linkNow) {
        await linkModulesInteractive(results, []);
    }
}
//# sourceMappingURL=gui.js.map