"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.appsCommand = void 0;
const commander_1 = require("commander");
const logger_1 = require("../utils/logger");
const error_handler_1 = require("../utils/error.handler");
const app_service_1 = __importDefault(require("../services/app.service"));
const template_service_1 = require("../services/template.service");
const logger = logger_1.Logger.getInstance("apps.command");
exports.appsCommand = new commander_1.Command("apps").description("Application management commands");
exports.appsCommand
    .command("install")
    .description("Install dependencies for all template applications")
    .option("--verbose", "Enable verbose logging")
    .action(async (options) => {
    try {
        if (options.verbose) {
            logger.setVerbose(true);
        }
        logger.info("Installing dependencies for template applications...");
        await app_service_1.default.installAllApps();
        logger.info("All template application dependencies installed successfully!");
    }
    catch (error) {
        error_handler_1.ErrorHandler.handle(error);
    }
});
exports.appsCommand
    .command("build")
    .description("Build all template applications and copy output to modifications folder. Requires prior installation of dependencies.")
    .option("--verbose", "Enable verbose logging")
    .action(async (options) => {
    try {
        if (options.verbose) {
            logger.setVerbose(true);
        }
        logger.debug("Building template applications...");
        await app_service_1.default.buildAllApps();
    }
    catch (error) {
        error_handler_1.ErrorHandler.handle(error);
    }
});
exports.appsCommand
    .command("dev")
    .description("Start all template applications in development mode")
    .option("--verbose", "Enable verbose logging")
    .action(async (options) => {
    try {
        if (options.verbose) {
            logger.setVerbose(true);
        }
        await app_service_1.default.startAllAppsInDevelopment();
    }
    catch (error) {
        error_handler_1.ErrorHandler.handle(error);
    }
});
exports.appsCommand
    .command("list")
    .description("List all template applications that are added to the seed project, and all available templates")
    .action(async () => {
    try {
        await printApps();
    }
    catch (error) {
        error_handler_1.ErrorHandler.handle(error);
    }
});
exports.appsCommand
    .command("add <appName>")
    .description("Add a new template application from the registry")
    .action(async (_appName) => {
    try {
        const templateService = new template_service_1.TemplateService();
        await templateService.addTemplateApplication(_appName);
        await app_service_1.default.installAppByName(_appName);
        await printApps();
    }
    catch (error) {
        error_handler_1.ErrorHandler.handle(error);
    }
});
const printApps = async () => {
    const apps = await app_service_1.default.getAllApps();
    if (apps.length === 0) {
        logger.info("No template applications found.");
    }
    else {
        logger.info("Added template applications:");
        apps.forEach(app => {
            logger.info(`- ${app.name} (${app.path})`);
        });
    }
    const templateService = new template_service_1.TemplateService();
    const all = templateService.getAvailableApplicationTemplates();
    // list all that are not part of the project
    const notAdded = all.filter(t => !apps.find(a => a.name === t.name));
    if (notAdded.length > 0) {
        logger.info("Available templates that can be added:");
        notAdded.forEach(t => {
            logger.info(`- ${t.name}: ${t.description}`);
        });
    }
};
//# sourceMappingURL=apps.command.js.map