"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CliServiceCommand = void 0;
function __assignType(fn, args) {
    fn.__type = args;
    return fn;
}
const event_1 = require("@deepkit/event");
const framework_1 = require("@deepkit/framework");
const http_1 = require("@deepkit/http");
const resolver_1 = require("../app/resolver");
const state_1 = require("../app/state");
const healthcheck_controller_1 = require("../health/healthcheck.controller");
const metrics_controller_1 = require("../telemetry/otel/metrics.controller");
class CliServiceCommand {
    constructor() {
        this.eventDispatcher = (0, resolver_1.r)(event_1.EventDispatcher);
        this.appServer = (0, resolver_1.r)(framework_1.ApplicationServer);
        this.routerRegistry = (0, resolver_1.r)(http_1.HttpRouterRegistry);
        this.shouldRun = true;
        this.stop = () => { };
    }
    async execute() {
        // note that we're a CLI service to prevent non-CLI side effects
        state_1.globalState.isCliService = true;
        // disable workers
        this.appServer.config.workers = 0;
        // remove all controllers except health-related ones
        const routes = this.routerRegistry.getRoutes();
        for (let i = 0; i < routes.length; i++) {
            const route = routes[i];
            if (route.action.type === 'controller') {
                if (route.action.controller === healthcheck_controller_1.HealthcheckController || route.action.controller === metrics_controller_1.MetricsController) {
                    continue;
                }
            }
            routes.splice(i, 1);
            i--;
        }
        // start the application server. tell it to wait for signals, but don't want for it to return,
        // as when listening to stop signals, it won't return until the server shuts down. instead, we
        // can listen to the onServerMainBootstrapDone event to actually wait for the server to start
        const onServerMainBootstrapDonePromise = (Promise.Ω = [['$']], new Promise(__assignType(resolve => {
            this.eventDispatcher.listen(framework_1.onServerMainBootstrapDone, () => resolve());
        }, ['resolve', '', 'P"2!"/"'])));
        this.appServer.start(true);
        await onServerMainBootstrapDonePromise;
        // prepare stop
        const hasRunService = this.runService !== CliServiceCommand.prototype.runService;
        const stopPromise = (Promise.Ω = [['$']], new Promise(__assignType(resolve => {
            const handleStop = () => {
                this.shouldRun = false;
                resolve();
            };
            this.eventDispatcher.listen(framework_1.onServerShutdown, handleStop);
            this.stop = handleStop;
        }, ['resolve', '', 'P"2!"/"'])));
        // start our CLI service
        await this.startService();
        // wait for stop signal
        if (hasRunService) {
            await this.runService();
        }
        else {
            await stopPromise;
        }
        // process shutdown
        await this.shutdownService();
    }
    async startService() { }
    async runService() { }
    async shutdownService() { }
}
exports.CliServiceCommand = CliServiceCommand;
CliServiceCommand.__type = ['eventDispatcher', function () { return (0, resolver_1.r)(event_1.EventDispatcher); }, 'appServer', function () { return (0, resolver_1.r)(framework_1.ApplicationServer); }, 'routerRegistry', function () { return (0, resolver_1.r)(http_1.HttpRouterRegistry); }, 'shouldRun', function () { return true; }, '', 'stop', function () { return () => { }; }, 'execute', 'startService', 'runService', 'shutdownService', 'CliServiceCommand', '!3!;>"!3#;>$!3%;>&)3\'<>(P$/)3*>+P"0,P$`0-<P$`0.<P$`0/<5w0'];
//# sourceMappingURL=cli.js.map