"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get IO_MESSAGES () {
        return IO_MESSAGES;
    },
    get SocketIoGateway () {
        return SocketIoGateway;
    }
});
const _socketio = require("socket.io");
const _eventconstants = require("../constants/event.constants");
const _passport = require("../middleware/passport");
const _socketiomiddleware = require("../middleware/socketio.middleware");
const _promclient = require("prom-client");
const socketIoGatewaySessions = new _promclient.Gauge({
    name: "socketio_gateway_sessions",
    help: "Gateway active sessions"
});
const socketIoGatewayDisconnects = new _promclient.Counter({
    name: "socketio_gateway_disconnects",
    help: "Gateway connections closed"
});
const socketIoGatewayMessagesSent = new _promclient.Counter({
    name: "socketio_messages_sent",
    help: "Gateway messages sent"
});
const socketIoGatewayMessageSentSize = new _promclient.Gauge({
    name: "socketio_message_size",
    help: "Gateway message sent size"
});
class SocketIoGateway {
    eventEmitter2;
    settingsStore;
    userService;
    configService;
    logger;
    io;
    constructor(loggerFactory, eventEmitter2, settingsStore, userService, configService){
        this.eventEmitter2 = eventEmitter2;
        this.settingsStore = settingsStore;
        this.userService = userService;
        this.configService = configService;
        this.logger = loggerFactory(SocketIoGateway.name);
    }
    attachServer(httpServer) {
        this.io = new _socketio.Server(httpServer, {
            cors: {
                origin: "*"
            }
        });
        const opts = (0, _passport.getPassportJwtOptions)(this.settingsStore, this.configService, (value)=>value.handshake.auth.token);
        this.io.use((0, _socketiomiddleware.authorize)(this.settingsStore, opts, this.logger, (0, _passport.verifyUserCallback)(this.userService)));
        this.io.on("connection", (socket)=>this.onConnect.bind(this)(socket));
    }
    onConnect(socket) {
        this.logger.debug("SocketIO Client connected", {
            socketId: socket.id
        });
        this.eventEmitter2.emit(_eventconstants.socketIoConnectedEvent, socket.id);
        socketIoGatewaySessions.inc();
        socket.on("disconnect", ()=>{
            this.logger.debug("SocketIO Client disconnected", {
                socketId: socket.id
            });
            socketIoGatewaySessions.dec(1);
            socketIoGatewayDisconnects.inc();
        });
    }
    send(event, data) {
        if (!this.io) {
            this.logger.debug(`Cant send event ${event}, socketio gateway must be created first`);
            return;
        }
        this.io.emit(event, data);
        socketIoGatewayMessagesSent.inc();
        const payload = JSON.stringify(data);
        const sizeInBytes = Buffer.byteLength(payload);
        socketIoGatewayMessageSentSize.set(sizeInBytes);
    }
}
const IO_MESSAGES = {
    Update: "update"
};

//# sourceMappingURL=socket-io.gateway.js.map