#!/usr/bin/env node
"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;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
require("dotenv").config({ quiet: true, override: true });
const commander_1 = require("commander");
const logger_1 = require("./utils/logger");
const error_handler_1 = require("./utils/error.handler");
const create_command_1 = require("./commands/create.command");
const components_command_1 = require("./commands/components.command");
const apps_command_1 = require("./commands/apps.command");
const license_command_1 = require("./commands/license.command");
const modifications_command_1 = require("./commands/modifications.command");
const build_command_1 = require("./commands/build.command");
const dev_command_1 = require("./commands/dev.command");
const test_command_1 = require("./commands/test.command");
const setup_command_1 = require("./commands/setup.command");
const clack = __importStar(require("@clack/prompts"));
const colors_1 = require("./utils/colors");
const pacakge_json_helper_1 = require("./utils/pacakge.json.helper");
const config_service_1 = require("./services/config/config.service");
const proxy_1 = require("./utils/proxy");
const version_check_1 = require("./utils/version-check");
const program = new commander_1.Command();
const logger = logger_1.Logger.getInstance("cli");
clack.intro((0, colors_1.bgCyanBlack)(` io.Connect Desktop CLI ${pacakge_json_helper_1.PackageJSONHelper.getCliPackageJson().version} `));
logger.debug(`io.Connect Desktop CLI ${pacakge_json_helper_1.PackageJSONHelper.getCliPackageJson().version}`);
// Initialize proxy support for fetch requests (reads HTTP_PROXY, HTTPS_PROXY, NO_PROXY)
(0, proxy_1.initializeProxy)();
logger.debug(`Node.js version: ${process.version}`);
logger.debug(`Operating System: ${process.platform} ${process.arch}`);
logger.debug(`Command line arguments: ${JSON.stringify(process.argv)}`);
logger.printLogLocation();
logger.debug(`Config is ${config_service_1.ConfigManager.getConfigStringWithoutSecrets()}`);
// Initialize Sentry for error tracking, should be after proxy initialization
if (config_service_1.ConfigManager.config.sentry) {
    require("./utils/sentry");
}
program
    .name("@interopio/iocd-cli")
    .description("CLI tool for setting up, building and packaging io.Connect Desktop projects")
    .version(pacakge_json_helper_1.PackageJSONHelper.getCliPackageJson().version)
    .option("--verbose", "Enable detailed output and debug logging")
    .hook("preAction", thisCommand => {
    // Set verbose logging if flag is present
    const opts = thisCommand.opts();
    if (opts["verbose"]) {
        logger.setVerbose(true);
        logger.debug("Verbose logging enabled");
    }
});
// Register all command modules
program.addCommand(create_command_1.createCommand);
program.addCommand(setup_command_1.setupCommand);
program.addCommand(dev_command_1.devCommand);
program.addCommand(test_command_1.testCommand);
program.addCommand(build_command_1.buildCommand);
program.addCommand(components_command_1.componentsCommand);
program.addCommand(apps_command_1.appsCommand);
program.addCommand(modifications_command_1.modificationsCommand);
program.addCommand(license_command_1.licenseCommand);
// global hook runs for all subcommands
program.hook("preAction", (_thisCommand, actionCommand) => {
    const cmdPath = actionCommand.name(); // e.g. "init" or "build"
    const fullPath = actionCommand?.parent
        ? actionCommand.parent.name() + " " + cmdPath
        : cmdPath;
    logger.debug(`Executing command: ${fullPath}`);
    if (config_service_1.ConfigManager.config.sentry) {
        const Sentry = require("@sentry/node");
        Sentry.addBreadcrumb({
            category: "cli",
            message: `Command executed: ${fullPath}`,
            level: "info",
        });
    }
});
// === ERROR HANDLING ===
process.on("uncaughtException", error => {
    logger.error("Uncaught exception:", error);
    error_handler_1.ErrorHandler.handle(error);
});
process.on("unhandledRejection", (reason, promise) => {
    logger.error("Unhandled rejection at:", promise, "reason:", reason);
    error_handler_1.ErrorHandler.handle(new Error(String(reason)));
});
// Parse command line arguments
error_handler_1.ErrorHandler.wrap(async () => {
    // Check for CLI updates (non-blocking, will not fail the CLI)
    await (0, version_check_1.checkForUpdates)();
    await program.parseAsync(process.argv);
});
//# sourceMappingURL=cli.js.map