"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const zod_1 = require("zod");
const index_core_1 = require("../index-core");
const server_1 = require("../server");
const TelemetryService_1 = require("../services/TelemetryService");
const log = (0, index_core_1.createLogger)('HostedDemoSessionRuntime');
const runtimeEnvSchema = zod_1.z.object({
    RIVET_SESSION_ID: zod_1.z.string().min(1),
    RIVET_SESSION_PROJECT_PATH: zod_1.z.string().min(1),
    RIVET_SESSION_RIVET_PORT: zod_1.z.coerce.number().int().positive(),
});
const main = async () => {
    const env = runtimeEnvSchema.parse(process.env);
    const telemetry = new TelemetryService_1.TelemetryService({ source: 'tryout' });
    const server = await (0, server_1.startServer)({
        framework: 'static',
        projectPath: env.RIVET_SESSION_PROJECT_PATH,
        staticEntry: 'index.html',
        rivetPort: env.RIVET_SESSION_RIVET_PORT,
        telemetry,
        skipProcessHandlers: true,
        isGitEnabled: false,
    });
    telemetry.trackTryoutRuntimeStarted({
        demoSessionId: env.RIVET_SESSION_ID,
        rivetPort: env.RIVET_SESSION_RIVET_PORT,
        deploymentEnv: process.env.NODE_ENV ?? 'development',
    });
    const shutdown = async (signal) => {
        telemetry.trackTryoutRuntimeShutdown({
            demoSessionId: env.RIVET_SESSION_ID,
            signal,
            deploymentEnv: process.env.NODE_ENV ?? 'development',
        });
        log.info(`Shutting down hosted session runtime ${env.RIVET_SESSION_ID}`);
        await server.close();
        process.exit(0);
    };
    process.on('SIGTERM', () => {
        void shutdown('SIGTERM');
    });
    process.on('SIGINT', () => {
        void shutdown('SIGINT');
    });
};
void main();
//# sourceMappingURL=sessionRuntime.js.map