"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.linkCommand = linkCommand;
const chalk_1 = __importDefault(require("chalk"));
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const module_system_1 = require("../utils/module-system");
async function linkCommand(moduleName, options) {
    try {
        console.log(chalk_1.default.blue(`Linking module: ${moduleName}`));
        // Load extensions config
        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'));
        // Check if module exists
        const module = (0, module_system_1.findModule)(moduleName);
        if (!module) {
            console.error(chalk_1.default.red(`Module not found: ${moduleName}`));
            process.exit(1);
        }
        // Check if already linked
        const existingIndex = config.modules.findIndex((m) => m.name === moduleName);
        if (existingIndex >= 0) {
            console.log(chalk_1.default.yellow(`Module already linked: ${moduleName}`));
            console.log(chalk_1.default.gray('Use "augx update" to update to latest version'));
            return;
        }
        // Add to config
        config.modules.push({
            name: module.fullName,
            version: options.version || module.metadata.version,
            type: module.metadata.type,
            description: module.metadata.description
        });
        fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
        console.log(chalk_1.default.green(`✓ Linked ${module.fullName} (v${options.version || module.metadata.version})`));
        console.log(chalk_1.default.gray(`\nUse "augx show ${module.fullName}" to view module details`));
    }
    catch (error) {
        console.error(chalk_1.default.red('Error linking module:'), error);
        process.exit(1);
    }
}
//# sourceMappingURL=link.js.map