"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    IO_MESSAGES: function() {
        return IO_MESSAGES;
    },
    SocketIoGateway: function() {
        return SocketIoGateway;
    }
});
const _socketio = require("socket.io");
const _eventconstants = require("../constants/event.constants");
const _passport = require("../middleware/passport");
const _passportjwt = require("passport-jwt");
const authorize = (settingsStore, options, verify)=>{
    const strategy = new _passportjwt.Strategy(options, verify);
    return async function authorizeCallback(socket, next) {
        if (!await settingsStore.getLoginRequired()) {
            return next();
        }
        strategy.success = function success(user) {
            socket.handshake.user = user;
            next();
        };
        strategy.fail = (info)=>next(new Error(info));
        strategy.error = (error)=>next(error);
        strategy.authenticate(socket, {});
    };
};
class SocketIoGateway {
    logger;
    eventEmitter2;
    io;
    authService;
    settingsStore;
    configService;
    userService;
    constructor({ loggerFactory, eventEmitter2, settingsStore, authService, configService, userService }){
        this.logger = loggerFactory(SocketIoGateway.name);
        this.eventEmitter2 = eventEmitter2;
        this.settingsStore = settingsStore;
        this.authService = authService;
        this.userService = userService;
        this.configService = configService;
    }
    attachServer(httpServer) {
        this.io = new _socketio.Server(httpServer, {
            cors: {
                origin: "*"
            }
        });
        const opts = (0, _passport.getPassportJwtOptions)(this.settingsStore, this.configService, (value)=>value.handshake.auth.token);
        const verify = (0, _passport.verifyUserCallback)(this.userService);
        this.io.use(authorize(this.settingsStore, opts, verify));
        this.io.on("connection", (socket)=>this.onConnect.bind(this)(socket));
    }
    onConnect(socket) {
        this.logger.debug("SocketIO Client connected", socket.id);
        this.eventEmitter2.emit(_eventconstants.socketIoConnectedEvent, socket.id);
        socket.on("disconnect", ()=>{
            this.logger.debug("SocketIO Client disconnected", socket.id);
        });
    }
    send(event, data) {
        if (!this.io) {
            this.logger.debug("No io server setup yet");
            return;
        }
        if (this.settingsStore.getServerSettings().debugSettings?.debugSocketIoEvents) {
            this.logger.log(`Sending event ${event}`);
        }
        this.io.emit(event, data);
    }
}
const IO_MESSAGES = {
    LegacyUpdate: "legacy-update",
    LegacyPrinterTest: "legacy-printer-test",
    CompletionEvent: "completion-event",
    HostState: "host-state",
    ApiAccessibility: "api-accessibility"
};

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