export class QueuedUserMessages {
    messages;
    isDraining = false;
    pendingIdleNotification = false;
    constructor(messages) {
        this.messages = messages;
    }
    enqueue(text, requestRender) {
        this.messages.push(text);
        requestRender();
    }
    popIntoEditor(editor) {
        if (editor.getEditorText() !== "")
            return;
        const last = this.messages.pop();
        if (!last)
            return;
        editor.setEditorText(last);
    }
    dequeueIntoEditor(editor) {
        if (this.messages.length === 0)
            return;
        const editorText = editor.getEditorText();
        const parts = [];
        if (editorText !== "") {
            parts.push(editorText);
        }
        parts.push(...this.messages);
        this.messages.length = 0;
        editor.setEditorText(parts.join("\n\n---\n\n"));
    }
    markPendingIdleNotification() {
        this.pendingIdleNotification = true;
    }
    async drain(adapter) {
        if (this.isDraining)
            return;
        this.isDraining = true;
        try {
            while (!adapter.isStreaming() && this.messages.length > 0) {
                const next = this.messages.shift();
                if (!next)
                    return;
                adapter.requestRender();
                await adapter.onUserInput(next);
            }
        }
        finally {
            this.isDraining = false;
            if (this.pendingIdleNotification && !adapter.isStreaming() && this.messages.length === 0) {
                this.pendingIdleNotification = false;
                adapter.sendTerminalNotification(adapter.buildIdleNotificationTitle());
            }
        }
    }
}
//# sourceMappingURL=queued_user_messages.js.map