/**
 * QueueTracker — tracks pending steering and follow-up messages for UI display.
 *
 * When the user sends a message while the agent is streaming, the text is
 * queued via AgentController.steer() or AgentController.followUp(). This tracker mirrors those
 * queues so the UI can show what's pending. When the AgentController delivers a queued
 * message (message_start with matching text), the tracker removes it.
 *
 * Part of the AgentSession event pipeline (step 1). Does not reference
 * AgentSession, AgentController, or any other pipeline handler.
 */
// ============================================================================
// Handler
// ============================================================================
export class QueueTracker {
    _steering = [];
    _followUp = [];
    /** Pending steering messages (read-only). */
    get steeringMessages() {
        return this._steering;
    }
    /** Pending follow-up messages (read-only). */
    get followUpMessages() {
        return this._followUp;
    }
    /** Total pending messages (steering + follow-up). */
    get pendingCount() {
        return this._steering.length + this._followUp.length;
    }
    /** Track a new steering message. */
    pushSteering(text) {
        this._steering.push(text);
    }
    /** Track a new follow-up message. */
    pushFollowUp(text) {
        this._followUp.push(text);
    }
    /**
     * Clear all tracked messages and return them.
     * Used when aborting or switching sessions.
     */
    clearAll() {
        const steering = [...this._steering];
        const followUp = [...this._followUp];
        this._steering = [];
        this._followUp = [];
        return { steering, followUp };
    }
    /**
     * Handle an agent event: remove delivered messages from the tracking queues.
     *
     * On message_start for a user message, checks if the text matches a tracked
     * steering or follow-up message and removes the first match. This must run
     * BEFORE the event is emitted to external listeners so the UI sees the
     * updated queue state.
     */
    handle(event) {
        if (event.type !== 'message_start')
            return;
        if (event.message.role !== 'user')
            return;
        const text = extractUserText(event.message);
        if (!text)
            return;
        // Check steering queue first
        const steeringIndex = this._steering.indexOf(text);
        if (steeringIndex !== -1) {
            this._steering.splice(steeringIndex, 1);
            return;
        }
        // Then follow-up queue
        const followUpIndex = this._followUp.indexOf(text);
        if (followUpIndex !== -1) {
            this._followUp.splice(followUpIndex, 1);
        }
    }
}
// ============================================================================
// Helpers
// ============================================================================
/** Extract text content from a user message. */
function extractUserText(message) {
    if (message.role !== 'user')
        return '';
    const content = message.content;
    if (typeof content === 'string')
        return content;
    const textBlocks = content.filter((c) => c.type === 'text');
    return textBlocks.map((c) => c.text).join('');
}
//# sourceMappingURL=queue-tracker.js.map