"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.initSocketIO = initSocketIO;
exports.emitToCourse = emitToCourse;
exports.emitToNotification = emitToNotification;
exports.getSocketIO = getSocketIO;
const socket_io_1 = require("socket.io");
const courseSocketMap = new Map(); // slug -> Set<socket.id>
const notificationSocketMap = new Map(); // notificationId -> Set<socket.id>
const socketStore = new Map(); // socket.id -> socket
let io = null;
function initSocketIO(server) {
    io = new socket_io_1.Server(server, {
        cors: {
            origin: "*",
            methods: ["GET", "POST"],
        },
        path: "/sockete",
    });
    io.on("connection", socket => {
        console.log("🧠 Socket connected:", socket.id);
        socketStore.set(socket.id, socket);
        socket.on("register", (data) => {
            var _a;
            const { courseSlug } = data;
            if (!courseSlug)
                return;
            if (!courseSocketMap.has(courseSlug)) {
                courseSocketMap.set(courseSlug, new Set());
            }
            (_a = courseSocketMap.get(courseSlug)) === null || _a === void 0 ? void 0 : _a.add(socket.id);
            console.log(`📦 Socket ${socket.id} registered to course: ${courseSlug}`);
        });
        socket.on("registerNotification", (data) => {
            var _a;
            const { notificationId } = data;
            if (!notificationId)
                return;
            if (!notificationSocketMap.has(notificationId)) {
                notificationSocketMap.set(notificationId, new Set());
            }
            (_a = notificationSocketMap.get(notificationId)) === null || _a === void 0 ? void 0 : _a.add(socket.id);
            console.log(`📧 Socket ${socket.id} registered to notification: ${notificationId}`);
        });
        socket.on("disconnect", () => {
            console.log("🔥 Socket disconnected:", socket.id);
            socketStore.delete(socket.id);
            for (const set of courseSocketMap.values()) {
                set.delete(socket.id);
            }
        });
    });
    return io;
}
function emitToCourse(courseSlug, event, payload) {
    const socketIds = courseSocketMap.get(courseSlug);
    if (!socketIds || socketIds.size === 0)
        return;
    for (const id of socketIds) {
        const socket = socketStore.get(id);
        if (socket)
            socket.emit(event, payload);
    }
}
function emitToNotification(notificationId, payload, retry = 0) {
    const socketIds = notificationSocketMap.get(notificationId);
    if (!socketIds || socketIds.size === 0) {
        if (retry > 3) {
            console.log("❌ Notification", notificationId, "not found");
            return;
        }
        setTimeout(() => {
            emitToNotification(notificationId, payload, retry + 1);
        }, 3000);
        return;
    }
    for (const id of socketIds) {
        const socket = socketStore.get(id);
        if (socket)
            socket.emit(notificationId, payload);
    }
}
function getSocketIO() {
    if (!io)
        throw new Error("Socket.IO not initialized");
    return io;
}
