"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CliServiceCommand = void 0;
const tslib_1 = require("tslib");
const _a = tslib_1.__importStar(require("@zyno-io/ts-server-foundation/type-metadata-runtime"));
const resolver_1 = require("../app/resolver");
const base_1 = require("../app/base");
const lifecycle_1 = require("../app/lifecycle");
const __tsf_metadata_type = _a.createCompactMetadataRegistryV1("[1,[{\"kind\":2},{\"kind\":2,\"typeName\":\"() =\\u003e void\"},{\"kind\":22,\"type\":{\"kind\":3}}]]", []);
let _CliServiceCommandMetadata_1;
class CliServiceCommand {
    shouldRun = true;
    stop = () => { };
    async execute() {
        const app = (0, resolver_1.r)(base_1.App);
        const hasRunService = this.runService !== CliServiceCommand.prototype.runService;
        let serviceStarted = false;
        const removeShutdownListener = app.on(lifecycle_1.onServerShutdown, () => {
            this.shouldRun = false;
        });
        this.stop = () => {
            this.shouldRun = false;
            void app.stop();
        };
        try {
            app.configureForCliService();
            await app.http.listen();
            await this.startService();
            serviceStarted = true;
            if (hasRunService)
                await this.runService();
            else
                await new Promise(resolve => app.on(lifecycle_1.onServerShutdown, () => resolve()));
        }
        finally {
            this.shouldRun = false;
            removeShutdownListener();
            if (serviceStarted)
                await this.shutdownService();
            await app.stop();
        }
    }
    async startService() { }
    async runService() { }
    async shutdownService() { }
    static __tsfType = _CliServiceCommandMetadata_1 = _a.decodeCompactMetadataV1("[1,{\"kind\":16,\"name\":\"CliServiceCommand\",\"typeName\":\"CliServiceCommand\",\"classType\":{\"$tsf\":0},\"properties\":[{\"name\":\"shouldRun\",\"type\":{\"$tsfType\":0},\"optional\":false},{\"name\":\"stop\",\"type\":{\"$tsfType\":1},\"optional\":false}],\"methods\":[{\"name\":\"execute\",\"parameters\":[],\"returnType\":{\"$tsfType\":2}},{\"name\":\"startService\",\"parameters\":[],\"returnType\":{\"$tsfType\":2}},{\"name\":\"runService\",\"parameters\":[],\"returnType\":{\"$tsfType\":2}},{\"name\":\"shutdownService\",\"parameters\":[],\"returnType\":{\"$tsfType\":2}}],\"hasConstructor\":false,\"constructorParameters\":[]}]", [() => CliServiceCommand], __tsf_metadata_type);
}
exports.CliServiceCommand = CliServiceCommand;
_CliServiceCommandMetadata_1.classType = CliServiceCommand;
CliServiceCommand.__tsfType = _CliServiceCommandMetadata_1;
//# sourceMappingURL=cli.js.map