import { SessionManager } from "../../../core/session-manager.js";
import { abortChatSessionBash, abortChatSessionCompaction, interruptChatSession, restoreQueuedMessagesToEditor, submitChatSession, } from "./chat-session-host-actions.js";
import { createChatSessionEditor, handleChatSessionInput, } from "./chat-session-host-editor.js";
import { applyChatSessionAgentEvent } from "./chat-session-host-events.js";
import { renderChatSessionBody, renderChatSessionEditor, renderChatSessionEntry, renderChatSessionFooter, renderChatSessionPendingMessages, renderChatSessionUsage, renderChatSessionWorkingStatus, transcriptCacheKey, } from "./chat-session-host-rendering.js";
import { clearChatSessionBusyForTerminalWorkflowStage, disposeChatSession, isChatSessionBashRunning, isChatSessionStreaming, syncChatSessionAnimationTick, } from "./chat-session-host-runtime.js";
import { ChatSessionHostState } from "./chat-session-host-state.js";
export class ChatSessionHost {
    constructor(opts) {
        this.focused = true;
        this.state = new ChatSessionHostState(opts, {
            renderEntry: (state, entry) => renderChatSessionEntry(state, entry),
            transcriptCacheKey: (state, entry, index) => transcriptCacheKey(state, entry, index),
        });
        this.state.editor = createChatSessionEditor(this.state, opts.tui, opts.keybindings, opts.editorTheme, opts.editorFactory, this.editorCallbacks());
        this.syncAnimationTick();
    }
    appendMessages(messages) {
        this.state.liveChat.appendMessages(messages);
    }
    loadSessionFile(sessionFile) {
        if (this.state.transcript.length > 0 || sessionFile === undefined)
            return;
        let messages;
        try {
            messages = SessionManager.open(sessionFile).buildSessionContext()
                .messages;
        }
        catch {
            return;
        }
        this.state.liveChat.appendMessages(messages);
    }
    appendExtraEntry(entry) {
        this.state.transcript.push(entry);
    }
    entries() {
        return this.state.transcript;
    }
    applyAgentEvent(event) {
        return applyChatSessionAgentEvent(this.state, event);
    }
    render(width) {
        return this.renderBody(width, 1);
    }
    invalidate() {
        this.state.transcriptComponent.invalidate();
        this.state.bodyViewport.invalidate();
        this.state.editor?.invalidate();
    }
    renderBody(width, budget) {
        return renderChatSessionBody(this.state, width, budget);
    }
    renderPendingMessages(width) {
        return renderChatSessionPendingMessages(this.state, width);
    }
    renderWorkingStatus(width) {
        return renderChatSessionWorkingStatus(this.state, width);
    }
    renderUsage(width) {
        return renderChatSessionUsage(this.state, width);
    }
    renderEditor(width) {
        return renderChatSessionEditor(this.state, width, this.focused);
    }
    renderFooter(width) {
        return renderChatSessionFooter(this.state, width);
    }
    handleScrollInput(data) {
        return this.state.bodyViewport.handleInput(data);
    }
    handleInput(data) {
        return handleChatSessionInput(this.state, data, this.editorCallbacks());
    }
    async interrupt() {
        await interruptChatSession(this.state);
    }
    async submit(mode = "auto", submittedText) {
        await submitChatSession(this.state, mode, submittedText);
    }
    isStreaming() {
        return isChatSessionStreaming(this.state);
    }
    isBashRunning() {
        return isChatSessionBashRunning(this.state);
    }
    isEditingBashCommand() {
        return this.state.isBashMode;
    }
    isCompacting() {
        return this.state.compacting;
    }
    hasInputText() {
        return this.state.inputBuffer.length > 0;
    }
    hasAnimationTick() {
        return this.state.animationTimer !== undefined;
    }
    bodyScrollFromBottom() {
        return this.state.bodyViewport.getScrollFromBottom();
    }
    bodyMaxScroll() {
        return this.state.bodyViewport.getMaxScroll();
    }
    inputText() {
        return this.state.inputBuffer;
    }
    statusText() {
        return this.state.statusMessage;
    }
    scrollToBottom() {
        this.state.bodyViewport.scrollToBottom();
    }
    syncAnimationTick() {
        syncChatSessionAnimationTick(this.state);
    }
    clearBusyForTerminalWorkflowStage() {
        clearChatSessionBusyForTerminalWorkflowStage(this.state);
    }
    dispose() {
        disposeChatSession(this.state);
    }
    restoreQueuedMessagesToEditor() {
        return restoreQueuedMessagesToEditor(this.state);
    }
    editorCallbacks() {
        return {
            submit: (mode, submittedText) => this.submit(mode, submittedText),
            restoreQueuedMessagesToEditor: () => this.restoreQueuedMessagesToEditor(),
            abortCompaction: () => abortChatSessionCompaction(this.state),
            interrupt: () => this.interrupt(),
            abortBash: () => abortChatSessionBash(this.state),
        };
    }
}
//# sourceMappingURL=chat-session-host.js.map