// WS execution channel handlers — register workflow execution control events
import { getClientId } from './connection-manager.js';
import { registerHandler } from './handler.js';
export function registerExecutionChannels(executionManager) {
    registerHandler('workflow:execute', async (ws, _workspaceId, data) => {
        const clientId = getClientId(ws);
        if (!clientId)
            return;
        try {
            const result = await executionManager.execute(data, clientId, (channel, payload) => {
                if (ws.readyState === 1) {
                    ws.send(JSON.stringify({ event: channel, data: payload }));
                }
            });
            ws.send(JSON.stringify({
                event: 'workflow:execute:result',
                data: result,
            }));
        }
        catch (err) {
            ws.send(JSON.stringify({
                event: 'workflow:execute:error',
                data: { error: err.message || String(err) },
            }));
        }
    });
    registerHandler('workflow:pause', (ws, _workspaceId, data) => {
        const { executionId } = data;
        if (!executionId)
            return;
        try {
            const result = executionManager.pause(executionId);
            ws.send(JSON.stringify({ event: 'workflow:pause:result', data: result }));
        }
        catch (err) {
            ws.send(JSON.stringify({ event: 'workflow:pause:error', data: { error: err.message } }));
        }
    });
    registerHandler('workflow:resume', async (ws, _workspaceId, data) => {
        const { executionId } = data;
        if (!executionId)
            return;
        try {
            const result = await executionManager.resume(executionId);
            ws.send(JSON.stringify({ event: 'workflow:resume:result', data: result }));
        }
        catch (err) {
            ws.send(JSON.stringify({ event: 'workflow:resume:error', data: { error: err.message } }));
        }
    });
    registerHandler('workflow:stop', (ws, _workspaceId, data) => {
        const { executionId } = data;
        if (!executionId)
            return;
        try {
            const result = executionManager.stop(executionId);
            ws.send(JSON.stringify({ event: 'workflow:stop:result', data: result }));
        }
        catch (err) {
            ws.send(JSON.stringify({ event: 'workflow:stop:error', data: { error: err.message } }));
        }
    });
    registerHandler('workflow:debug-node', async (ws, _workspaceId, data) => {
        const clientId = getClientId(ws);
        if (!clientId)
            return;
        try {
            const result = await executionManager.debugNode(data, clientId);
            ws.send(JSON.stringify({ event: 'workflow:debug-node:result', data: result }));
        }
        catch (err) {
            ws.send(JSON.stringify({ event: 'workflow:debug-node:error', data: { error: err.message } }));
        }
    });
    registerHandler('workflow:get-execution-recovery', (ws, _workspaceId, data) => {
        const clientId = getClientId(ws);
        if (!clientId)
            return;
        try {
            const result = executionManager.getExecutionRecovery(data, clientId);
            ws.send(JSON.stringify({ event: 'workflow:get-execution-recovery:result', data: result }));
        }
        catch (err) {
            ws.send(JSON.stringify({ event: 'workflow:get-execution-recovery:error', data: { error: err.message } }));
        }
    });
}
//# sourceMappingURL=execution-channels.js.map