"use strict";
/**
 * Socket.IO namespaces:
 * /actors
 * /session-status
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.onSessionProgress = exports.onSessionStatusChanged = exports.onActorsChanged = exports.initialize = void 0;
const configLoader = require("./config-loader");
const express_session_1 = require("./express-session");
const socketIo = require("socket.io");
let io = null;
let ioActorsNs = null;
let ioSessionStatusNs = null;
function initialize(httpServer) {
    if (io)
        return;
    const config = configLoader.getConfig();
    io = socketIo(httpServer, {
        path: `${config.urlPrefix}/socket.io`
    });
    const ioSession = require('socket.io-express-session');
    io.use(ioSession(express_session_1.sessionSettings));
    io.on('connection', function (socket) {
        const session = socket.handshake.session;
        if (session && session.id) {
            console.log('Established WebSocket connection for session ' + session.id);
        }
    });
    ioActorsNs = io.of('/actors');
    ioSessionStatusNs = io.of('/session-status');
}
exports.initialize = initialize;
function onActorsChanged(actorId) {
    if (!io)
        return;
    ioActorsNs.emit('actors-changed', {
        actorId: actorId
    });
}
exports.onActorsChanged = onActorsChanged;
function onSessionStatusChanged(sessionId, newStatus) {
    if (!io)
        return;
    ioSessionStatusNs.emit('status-changed', {
        sessionId: sessionId,
        newStatus: newStatus
    });
}
exports.onSessionStatusChanged = onSessionStatusChanged;
function onSessionProgress(sessionId, eventData) {
    if (!io)
        return;
    ioSessionStatusNs.emit('progress', Object.assign({ sessionId: sessionId }, eventData));
}
exports.onSessionProgress = onSessionProgress;
//# sourceMappingURL=websocket-notifier.js.map