import { createAgentMessagePartsTracker } from './agent-message-parts.js';
import * as issueCommentService from '../services/issue-comment.js';
import * as issueService from '../services/issue.js';
import * as messageService from '../services/message.js';
import { broadcastToWorkspace } from '../ws/connection-manager.js';
export function createIssueAgentProgressTracker(input) {
    const startTime = Date.now();
    let tracker;
    tracker = createAgentMessagePartsTracker({
        workspaceId: input.workspaceId,
        channelId: input.issue.channelId,
        messageId: input.progress.message.id,
        workspaceRoot: input.workspaceRoot,
        onOutput: (line) => {
            input.onOutput?.(line);
            const live = messageService.updateMessage(input.workspaceId, input.issue.channelId, input.progress.message.id, {
                content: tracker.output.join('\n') || input.progress.message.content,
                status: 'streaming',
                metadata: {
                    ...input.progress.message.metadata,
                    duration: Date.now() - startTime,
                },
                parts: tracker.buildParts({
                    sessionId: input.agentSessionId,
                    workspaceRoot: input.workspaceRoot,
                    model: input.progress.message.metadata?.model,
                    success: true,
                }),
            });
            if (live)
                broadcastToWorkspace(input.workspaceId, 'channel.message.updated', live);
        },
    });
    return tracker;
}
export function createIssueAgentProgress(workspaceId, issue, preset, agentSessionId, metadata) {
    const content = `${preset.name || preset.role} is processing...`;
    const message = messageService.createMessage(workspaceId, issue.channelId, {
        senderId: preset.id,
        senderRole: preset.role,
        content,
        type: 'text',
        status: 'streaming',
        metadata: {
            agentSessionId,
            runtime: metadata.runtime,
            model: metadata.model,
            summary: content,
            duration: 0,
            taskId: metadata.taskId,
            phase: metadata.phase,
        },
    });
    broadcastToWorkspace(workspaceId, 'channel.message', message);
    const comment = issueCommentService.createIssueComment(workspaceId, issue.id, {
        senderId: preset.id,
        senderRole: preset.role,
        content,
        source: 'agent_progress',
        metadata: {
            channelId: issue.channelId,
            messageId: message.id,
            agentSessionId,
            runtime: metadata.runtime,
            model: metadata.model,
            summary: content,
            duration: 0,
            taskId: metadata.taskId,
            phase: metadata.phase,
        },
    });
    if (comment)
        broadcastToWorkspace(workspaceId, 'issue.updated', issueService.getById(workspaceId, issue.id));
    return { message, comment };
}
export function completeIssueAgentProgress(workspaceId, issue, progress, summary, output, metadata) {
    const parts = metadata.parts;
    const finalText = [...(parts ?? [])].reverse().find((part) => part.type === 'text')?.text.trim();
    const content = finalText || summary.trim() || output.join('\n').trim();
    if (!content.trim())
        return;
    const message = messageService.updateMessage(workspaceId, issue.channelId, progress.message.id, {
        content,
        type: 'text',
        status: metadata.messageStatus,
        metadata: {
            ...progress.message.metadata,
            runtime: metadata.runtime,
            model: metadata.model,
            summary,
            duration: metadata.duration,
        },
        parts,
    });
    if (message)
        broadcastToWorkspace(workspaceId, 'channel.message.updated', message);
    const comment = progress.comment
        ? issueCommentService.updateIssueComment(workspaceId, issue.id, progress.comment.id, {
            content,
            metadata: {
                ...progress.comment.metadata,
                channelId: issue.channelId,
                messageId: progress.message.id,
                runtime: metadata.runtime,
                model: metadata.model,
                summary,
                duration: metadata.duration,
            },
        })
        : null;
    if (comment)
        broadcastToWorkspace(workspaceId, 'issue.updated', issueService.getById(workspaceId, issue.id));
}
//# sourceMappingURL=issue-agent-progress.js.map