import * as ptyService from '../services/pty.js';
import { broadcastToWorkspace } from './connection-manager.js';
const DEBUG_TERMINAL_DUP = '[DEBUG-terminal-dup]';
export function sendTerminalSessions(ws, workspaceId) {
    const existingSessions = ptyService.getSessionsByWorkspace(workspaceId);
    console.log(DEBUG_TERMINAL_DUP, 'server sendTerminalSessions', {
        workspaceId,
        sessions: existingSessions.map(s => ({
            sessionId: s.id,
            bufferLength: s.buffer.join('').length,
        })),
    });
    ws.send(JSON.stringify({
        event: 'terminal.sessions',
        workspaceId,
        timestamp: new Date().toISOString(),
        data: {
            sessions: existingSessions.map(s => ({ sessionId: s.id, cwd: s.cwd, shell: s.shell, buffer: s.buffer.join('') })),
        },
    }));
}
export function handleTerminalEvent(ws, workspaceId, event, data) {
    if (event === 'terminal.list') {
        sendTerminalSessions(ws, workspaceId);
        return;
    }
    switch (event) {
        case 'terminal.create': {
            const payload = data;
            const cwd = payload.cwd || process.env.HOME || process.env.USERPROFILE || (process.platform === 'win32' ? process.env.SYSTEMROOT || 'C:\\' : '/tmp');
            console.log(DEBUG_TERMINAL_DUP, 'server terminal.create received', {
                workspaceId,
                sessionId: payload.sessionId,
                shell: payload.shell,
                cwd,
            });
            try {
                const sessionId = ptyService.createSession(workspaceId, cwd, (id, output) => {
                    console.log(DEBUG_TERMINAL_DUP, 'server terminal.output broadcast', {
                        workspaceId,
                        sessionId: id,
                        outputLength: output.length,
                        preview: output.slice(0, 80),
                    });
                    broadcastToWorkspace(workspaceId, 'terminal.output', { sessionId: id, data: output });
                }, (id, exitCode) => {
                    broadcastToWorkspace(workspaceId, 'terminal.closed', { sessionId: id, exitCode });
                }, payload.shell, undefined, payload.sessionId);
                const session = ptyService.getSession(sessionId);
                console.log(DEBUG_TERMINAL_DUP, 'server terminal.created broadcast', {
                    workspaceId,
                    sessionId,
                    cwd,
                    shell: session?.shell ?? payload.shell,
                });
                broadcastToWorkspace(workspaceId, 'terminal.created', { sessionId, cwd, shell: session?.shell ?? payload.shell });
            }
            catch (err) {
                broadcastToWorkspace(workspaceId, 'terminal.error', { sessionId: payload.sessionId, error: err.message || String(err) });
            }
            break;
        }
        case 'terminal.input': {
            const payload = data;
            ptyService.write(payload.sessionId, payload.data);
            break;
        }
        case 'terminal.resize': {
            const payload = data;
            ptyService.resize(payload.sessionId, payload.cols, payload.rows);
            break;
        }
        case 'terminal.close': {
            const payload = data;
            ptyService.kill(payload.sessionId);
            break;
        }
    }
}
//# sourceMappingURL=terminal-handler.js.map