/**
 * /collab slash command — thin TUI wrapper over CollabManager.
 *
 * All orchestration logic lives in app/collab/collab-manager.ts.
 * This file only handles argument parsing and TUI rendering.
 */
import { Spacer, Text } from '../../tui/index.js';
import { theme } from '../theme/runtime.js';
// ── Command handler ─────────────────────────────────────────────────────
export async function collabCommand(args, ctx) {
    const parts = args.trim().split(/\s+/);
    const subcommand = parts[0] || '';
    const manager = ctx.getCollabClient();
    // /collab name <username>
    if (subcommand === 'name') {
        const newName = parts.slice(1).join(' ').trim();
        if (!newName) {
            const current = manager.currentDisplayName() ?? '(not set)';
            ctx.chat.addChild(new Spacer(1));
            ctx.chat.addChild(new Text(theme.fg('dim', `Collab name: ${current}`), 1, 0));
            ctx.requestRender();
            return;
        }
        manager.setDisplayName(newName);
        ctx.chat.addChild(new Spacer(1));
        ctx.chat.addChild(new Text(theme.fg('dim', `Collab name set: ${newName}`), 1, 0));
        ctx.requestRender();
        return;
    }
    // /collab leave
    if (subcommand === 'leave') {
        if (!manager.inRoom) {
            ctx.feedback.showError('Not in a collaboration session.');
            return;
        }
        ctx.stopSuperFight?.();
        manager.leave();
        ctx.chat.addChild(new Spacer(1));
        ctx.chat.addChild(new Text(theme.fg('dim', 'Left the collaboration session.'), 1, 0));
        ctx.requestRender();
        return;
    }
    // /collab status
    if (subcommand === 'status') {
        if (!manager.inRoom) {
            ctx.feedback.showError('Not in a collaboration session. Use /collab to create or join one.');
            return;
        }
        renderStatus(manager, ctx);
        return;
    }
    // Show generated name on first use
    const { name: displayName, isNew: isNewName } = manager.getDisplayName();
    if (isNewName) {
        ctx.chat.addChild(new Spacer(1));
        ctx.chat.addChild(new Text(theme.fg('dim', `Your collab name: ${displayName} — change it with /collab name <name>`), 1, 0));
    }
    // /collab (no args) — create room or show status
    if (!subcommand) {
        if (manager.inRoom) {
            renderStatus(manager, ctx);
            return;
        }
        try {
            const result = await manager.createRoom(displayName);
            ctx.chat.addChild(new Spacer(1));
            ctx.chat.addChild(new Text(theme.fg('accent', `Room created: ${result.roomId}`), 1, 0));
            ctx.chat.addChild(new Text(theme.fg('dim', `Share this ID with peers: /collab ${result.roomId}`), 1, 0));
            ctx.requestRender();
        }
        catch (err) {
            ctx.feedback.showError(`Failed to create room: ${err instanceof Error ? err.message : err}`);
        }
        return;
    }
    // /collab <roomId> — join room
    try {
        const result = await manager.joinRoom(subcommand, displayName);
        ctx.chat.addChild(new Spacer(1));
        ctx.chat.addChild(new Text(theme.fg('accent', `Joined room: ${result.roomId}`), 1, 0));
        if (result.peers.length > 0) {
            const names = result.peers.map((p) => p.displayName).join(', ');
            ctx.chat.addChild(new Text(theme.fg('dim', `Peers: ${names}`), 1, 0));
        }
        else {
            ctx.chat.addChild(new Text(theme.fg('dim', 'No other peers in the room yet.'), 1, 0));
        }
        ctx.requestRender();
    }
    catch (err) {
        ctx.feedback.showError(`Failed to join room: ${err instanceof Error ? err.message : err}`);
    }
}
// ── Status rendering ────────────────────────────────────────────────────
function renderStatus(manager, ctx) {
    const status = manager.getStatus();
    ctx.chat.addChild(new Spacer(1));
    ctx.chat.addChild(new Text(theme.fg('accent', `Room: ${status.roomId}`), 1, 0));
    ctx.chat.addChild(new Text(theme.fg('dim', `Peers (${status.peers.length}):`), 1, 0));
    for (const peer of status.peers) {
        let line = `  • ${peer.displayName}`;
        if (peer.sheets && peer.sheets.length > 0) {
            const sheetNames = peer.sheets
                .map((s) => `${s.name} (${s.cellCount} cells${s.usedRange ? `, ${s.usedRange}` : ''})`)
                .join(', ');
            line += ` — ${sheetNames}`;
        }
        ctx.chat.addChild(new Text(theme.fg('dim', line), 1, 0));
    }
    if (status.peers.length === 0) {
        ctx.chat.addChild(new Text(theme.fg('dim', '  (none)'), 1, 0));
    }
    ctx.requestRender();
}
//# sourceMappingURL=collab-command.js.map