import { randomUUID } from 'node:crypto';
import { publishWorkspaceEvent } from '../services/notification-hub/index.js';
const connections = new Map();
const clientIdIndex = new Map();
const clientConnectedCallbacks = [];
const clientDisconnectedCallbacks = [];
let interactionResponseHandler = null;
let clientNodeResponseHandler = null;
export function addConnection(ws, workspaceId) {
    const clientId = randomUUID();
    const conn = { ws, workspaceId, clientId };
    connections.set(ws, conn);
    clientIdIndex.set(clientId, conn);
    ws.on('close', () => {
        connections.delete(ws);
        clientIdIndex.delete(clientId);
        for (const cb of clientDisconnectedCallbacks)
            cb(clientId);
    });
    for (const cb of clientConnectedCallbacks)
        cb(clientId, ws);
    return clientId;
}
export function removeConnection(ws) {
    const conn = connections.get(ws);
    if (conn) {
        clientIdIndex.delete(conn.clientId);
        for (const cb of clientDisconnectedCallbacks)
            cb(conn.clientId);
    }
    connections.delete(ws);
}
export function getClientId(ws) {
    return connections.get(ws)?.clientId;
}
export function getConnectionByClientId(clientId) {
    return clientIdIndex.get(clientId);
}
export function sendToClient(clientId, data) {
    const conn = clientIdIndex.get(clientId);
    if (!conn || conn.ws.readyState !== 1)
        return false;
    conn.ws.send(JSON.stringify(data));
    return true;
}
export function onClientConnected(cb) {
    clientConnectedCallbacks.push(cb);
}
export function onClientDisconnected(cb) {
    clientDisconnectedCallbacks.push(cb);
}
export function setInteractionResponseHandler(handler) {
    interactionResponseHandler = handler;
}
export function handleInteractionResponse(response, clientId) {
    interactionResponseHandler?.(response, clientId);
}
export function setClientNodeResponseHandler(handler) {
    clientNodeResponseHandler = handler;
}
export function handleClientNodeResponse(response, clientId) {
    clientNodeResponseHandler?.(response, clientId);
}
export function getConnectionsByWorkspace(workspaceId) {
    const result = [];
    for (const [ws, conn] of connections) {
        if (conn.workspaceId === workspaceId)
            result.push(ws);
    }
    return result;
}
export function broadcastToWorkspace(workspaceId, event, data) {
    const payload = JSON.stringify({
        event,
        workspaceId,
        timestamp: new Date().toISOString(),
        data,
    });
    for (const ws of getConnectionsByWorkspace(workspaceId)) {
        if (ws.readyState === 1)
            ws.send(payload);
    }
    publishWorkspaceEvent(workspaceId, event, data);
}
export function broadcastToAll(event, data) {
    if (connections.size === 0)
        return;
    const payload = JSON.stringify({
        event,
        timestamp: new Date().toISOString(),
        data,
    });
    for (const [ws] of connections) {
        if (ws.readyState === 1)
            ws.send(payload);
    }
}
export function getConnectionCount() {
    return connections.size;
}
//# sourceMappingURL=connection-manager.js.map