"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.searchCommand = searchCommand;
const chalk_1 = __importDefault(require("chalk"));
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
async function searchCommand(keyword, options) {
    try {
        console.log(chalk_1.default.blue(`\n🔍 Searching for: "${keyword}"\n`));
        const results = await searchModules(keyword, options.type);
        if (results.length === 0) {
            console.log(chalk_1.default.yellow('No modules found matching your search.'));
            console.log(chalk_1.default.gray('\nTry:'));
            console.log(chalk_1.default.gray('  • Using different keywords'));
            console.log(chalk_1.default.gray('  • Removing the --type filter'));
            console.log(chalk_1.default.gray('  • Running "augx list" to see all modules\n'));
            return;
        }
        console.log(chalk_1.default.bold(`Found ${results.length} module(s):\n`));
        results.forEach((module) => {
            console.log(chalk_1.default.bold.cyan(module.displayName));
            console.log(chalk_1.default.gray(`  Module: ${module.name}`));
            console.log(chalk_1.default.gray(`  Version: ${module.version}`));
            console.log(chalk_1.default.gray(`  Type: ${module.type}`));
            console.log(chalk_1.default.gray(`  Description: ${module.description}\n`));
        });
        console.log(chalk_1.default.gray(`Use "augx show <module>" to view details`));
        console.log(chalk_1.default.gray(`Use "augx link <module>" to link a module\n`));
    }
    catch (error) {
        console.error(chalk_1.default.red('Error searching modules:'), error);
        process.exit(1);
    }
}
async function searchModules(keyword, typeFilter) {
    const modules = [];
    const modulesDir = path.join(__dirname, '../../../augment-extensions');
    if (!fs.existsSync(modulesDir)) {
        return modules;
    }
    const lowerKeyword = keyword.toLowerCase();
    const categories = fs.readdirSync(modulesDir, { withFileTypes: true })
        .filter(dirent => dirent.isDirectory())
        .map(dirent => dirent.name);
    for (const category of categories) {
        // Skip if type filter doesn't match
        if (typeFilter && category !== typeFilter) {
            continue;
        }
        const categoryPath = path.join(modulesDir, category);
        const moduleNames = fs.readdirSync(categoryPath, { withFileTypes: true })
            .filter(dirent => dirent.isDirectory())
            .map(dirent => dirent.name);
        for (const moduleName of moduleNames) {
            const modulePath = path.join(categoryPath, moduleName);
            const moduleJsonPath = path.join(modulePath, 'module.json');
            if (fs.existsSync(moduleJsonPath)) {
                const moduleData = JSON.parse(fs.readFileSync(moduleJsonPath, 'utf-8'));
                const fullName = `${category}/${moduleName}`;
                // Search in name, description, and display name
                const searchableText = [
                    fullName,
                    moduleData.displayName || '',
                    moduleData.description || '',
                    category,
                    moduleName
                ].join(' ').toLowerCase();
                if (searchableText.includes(lowerKeyword)) {
                    modules.push({
                        name: fullName,
                        version: moduleData.version,
                        description: moduleData.description,
                        type: moduleData.type,
                        displayName: moduleData.displayName || fullName
                    });
                }
            }
        }
    }
    return modules;
}
//# sourceMappingURL=search.js.map