"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.componentsCommand = void 0;
const commander_1 = require("commander");
const logger_1 = require("../utils/logger");
const error_handler_1 = require("../utils/error.handler");
const components_service_1 = __importDefault(require("../services/components/components.service"));
const clack = __importStar(require("@clack/prompts"));
const logger = logger_1.Logger.getInstance();
exports.componentsCommand = new commander_1.Command('components')
    .description('Component management commands');
exports.componentsCommand
    .command('uninstall <name>')
    .description('Remove a component from the project')
    .action(async (name) => {
    try {
        await components_service_1.default.uninstallComponent(name);
    }
    catch (error) {
        error_handler_1.ErrorHandler.handle(error);
    }
});
exports.componentsCommand
    .command('install [name]')
    .description('Install all components listed in iocd.cli.config.json or a specific component by name')
    .action(async (name) => {
    try {
        if (name) {
            logger.debug(`Component install command called for: ${name}`);
            await components_service_1.default.installComponent(name);
            return;
        }
        else {
            await components_service_1.default.installComponents();
        }
    }
    catch (error) {
        error_handler_1.ErrorHandler.handle(error);
    }
});
exports.componentsCommand
    .command('list')
    .description('List all installed components')
    .action(async () => {
    try {
        const components = await components_service_1.default.listComponentsInThisProject();
        // Display components nicely using @clack/prompts
        if (components.length === 0) {
            clack.note('No components are currently configured in your project.', '📦 Components');
            return;
        }
        // Create formatted list for display
        const componentList = components
            .map(comp => `• ${comp.name} → ${comp.version}`)
            .join('\n');
        clack.note(componentList, `📦 Configured Components (${components.length})`);
    }
    catch (error) {
        error_handler_1.ErrorHandler.handle(error);
    }
});
exports.componentsCommand
    .command('browse')
    .description('Browse all available components from currently selected store')
    .action(async () => {
    try {
        const remoteComponents = await components_service_1.default.browseComponentsFromStore();
        // Display components nicely using @clack/prompts
        if (remoteComponents.length === 0) {
            clack.note('No components are currently available in the store.', '📦 Components');
            return;
        }
        // sort by component name and version
        remoteComponents.sort((a, b) => {
            const nameComparison = a.name.localeCompare(b.name);
            if (nameComparison !== 0)
                return nameComparison;
            const versionComp = a.version.localeCompare(b.version);
            if (versionComp !== 0)
                return versionComp;
            if (a.buildNumber !== undefined && b.buildNumber !== undefined) {
                return a.buildNumber - b.buildNumber;
            }
            else if (a.buildNumber !== undefined) {
                return -1; // a has build number, b doesn't -> b is greater
            }
            else if (b.buildNumber !== undefined) {
                return 1; // b has build number, a doesn't -> a is greater
            }
            return 0;
        });
        // Create formatted list for display
        const componentList = remoteComponents
            .map(comp => `• ${comp.name} → ${comp.fullVersion} ${comp.platform} ${comp.arch} ${comp.prerelease ? '(prerelease)' : ''}`)
            .join('\n');
        clack.note(componentList, `📦 Available Components (${remoteComponents.length})`);
    }
    catch (error) {
        error_handler_1.ErrorHandler.handle(error);
    }
});
exports.componentsCommand
    .command('pin')
    .description('Pin all "latest" component versions to their current specific versions for reproducible builds')
    .action(async () => {
    try {
        await components_service_1.default.pinComponentVersions();
    }
    catch (error) {
        error_handler_1.ErrorHandler.handle(error);
    }
});
//# sourceMappingURL=components.command.js.map