"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DevConsoleRuntime = void 0;
const tslib_1 = require("tslib");
const _a = tslib_1.__importStar(require("@zyno-io/ts-server-foundation/type-metadata-runtime"));
const observers_1 = require("./observers");
const server_1 = require("./server");
const store_1 = require("./store");
const __tsf_metadata_type = _a.createCompactMetadataRegistryV1("[1,[{\"kind\":2},{\"kind\":3}]]", []);
let _DevConsoleRuntimeMetadata_1;
class DevConsoleRuntime {
    app;
    logger;
    store = new store_1.DevConsoleStore();
    cleanupObservers;
    srpc;
    constructor(app, logger) {
        this.app = app;
        this.logger = logger;
        this.installObservers();
    }
    start(server) {
        this.installObservers();
        if (this.srpc)
            return;
        this.srpc = new server_1.DevConsoleSrpcServer(this.app, this.store, this.logger.scoped('devconsole'), server);
        this.store.onEvent = (type, data) => this.srpc?.broadcast(type, data);
    }
    close() {
        this.store.onEvent = undefined;
        this.srpc?.close();
        this.srpc = undefined;
        this.cleanupObservers?.();
        this.cleanupObservers = undefined;
    }
    installObservers() {
        this.cleanupObservers ??= (0, observers_1.installDevConsoleObservers)(this.app, this.store);
    }
    static __tsfType = _DevConsoleRuntimeMetadata_1 = _a.decodeCompactMetadataV1("[1,{\"kind\":16,\"name\":\"DevConsoleRuntime\",\"typeName\":\"DevConsoleRuntime\",\"classType\":{\"$tsf\":0},\"properties\":[{\"name\":\"store\",\"type\":{\"$tsfType\":0},\"optional\":false}],\"methods\":[{\"name\":\"start\",\"parameters\":[{\"name\":\"server\",\"type\":{\"$tsfAlias\":[1,\"node:http\",\"Server\",\"Server\"]},\"optional\":false,\"default\":false}],\"returnType\":{\"$tsfType\":1}},{\"name\":\"close\",\"parameters\":[],\"returnType\":{\"$tsfType\":1}}],\"hasConstructor\":true,\"constructorParameters\":[{\"name\":\"app\",\"type\":{\"kind\":16,\"typeName\":\"App\",\"classType\":{\"$tsfImport\":[1,\"../app/index\",\"App\"]},\"typeArguments\":[{\"kind\":1}]},\"optional\":false,\"default\":false},{\"name\":\"logger\",\"type\":{\"kind\":16,\"typeName\":\"ScopedLogger\",\"classType\":{\"$tsfImport\":[1,\"../services/index\",\"ScopedLogger\"]}},\"optional\":false,\"default\":false}]}]", [() => DevConsoleRuntime, require], __tsf_metadata_type);
}
exports.DevConsoleRuntime = DevConsoleRuntime;
_DevConsoleRuntimeMetadata_1.classType = DevConsoleRuntime;
DevConsoleRuntime.__tsfType = _DevConsoleRuntimeMetadata_1;
//# sourceMappingURL=runtime.js.map