import { Container, Spacer } from "@earendil-works/pi-tui";
import { renderChatMessage, } from "./chat_message_model.js";
export class ChatContainerComponent extends Container {
    chatContainer;
    theme;
    toolUiRegistry;
    thoughtsVisible = false;
    compactToolUi = false;
    allMessages = [];
    idToIndex = new Map();
    constructor(theme, toolUiRegistry, thoughtsVisible = false) {
        super();
        this.theme = theme;
        this.toolUiRegistry = toolUiRegistry;
        this.thoughtsVisible = thoughtsVisible;
        this.chatContainer = new Container();
        this.addChild(this.chatContainer);
    }
    setTheme(theme) {
        if (this.theme === theme)
            return;
        this.theme = theme;
        this.rebuild();
    }
    addMessage(model, id) {
        const finalId = id ?? this.generateId();
        const record = { id: finalId, model, rendered: false };
        this.allMessages.push(record);
        this.idToIndex.set(finalId, this.allMessages.length - 1);
        const rendered = this.renderMessage(record);
        if (this.shouldShowMessage(rendered)) {
            this.addSpacerIfNeeded();
            this.chatContainer.addChild(rendered.component);
            record.rendered = true;
        }
        return finalId;
    }
    replaceMessage(id, model) {
        const index = this.idToIndex.get(id);
        if (index === undefined)
            return false;
        this.allMessages[index] = { id, model, rendered: false };
        this.rebuild();
        return true;
    }
    updateMessage(id, model) {
        const index = this.idToIndex.get(id);
        if (index === undefined)
            return;
        const record = this.allMessages[index];
        if (!record)
            return;
        record.model = model;
        const rendered = record.renderedMessage;
        if (rendered?.update) {
            const wasVisible = rendered.hasVisibleText ? rendered.hasVisibleText() : true;
            const updated = rendered.update(model, {
                theme: this.theme,
                thoughtsVisible: this.thoughtsVisible,
                compactToolUi: this.compactToolUi,
                toolUiRegistry: this.toolUiRegistry,
            });
            if (updated) {
                const shouldShow = this.shouldShowMessage(rendered);
                const isVisibleNow = rendered.hasVisibleText ? rendered.hasVisibleText() : true;
                if (record.rendered && shouldShow) {
                    if (!wasVisible && isVisibleNow) {
                        this.rebuild();
                    }
                    return;
                }
                if (!record.rendered && shouldShow) {
                    this.rebuild();
                    return;
                }
                if (record.rendered && !shouldShow) {
                    this.rebuild();
                }
                return;
            }
        }
        this.rebuild();
    }
    setThinkingVisibility(visible) {
        if (this.thoughtsVisible === visible)
            return;
        this.thoughtsVisible = visible;
        this.rebuild();
    }
    setCompactToolUi(compact) {
        if (this.compactToolUi === compact)
            return;
        this.compactToolUi = compact;
        this.rebuild();
    }
    clear() {
        this.allMessages = [];
        this.idToIndex.clear();
        this.chatContainer.clear();
    }
    removeMessages(ids) {
        if (ids.length === 0 || this.allMessages.length === 0)
            return;
        const idSet = new Set(ids);
        this.allMessages = this.allMessages.filter((record) => !idSet.has(record.id));
        this.idToIndex.clear();
        this.allMessages.forEach((record, index) => {
            this.idToIndex.set(record.id, index);
        });
        this.rebuild();
    }
    removeMessagesFrom(id) {
        const index = this.idToIndex.get(id);
        if (index === undefined)
            return;
        this.allMessages = this.allMessages.slice(0, index);
        this.idToIndex.clear();
        this.allMessages.forEach((record, messageIndex) => {
            this.idToIndex.set(record.id, messageIndex);
        });
        this.rebuild();
    }
    rebuild() {
        this.chatContainer.clear();
        for (const record of this.allMessages) {
            record.rendered = false;
            const rendered = this.renderMessage(record);
            if (this.shouldShowMessage(rendered)) {
                this.addSpacerIfNeeded();
                this.chatContainer.addChild(rendered.component);
                record.rendered = true;
            }
        }
    }
    renderMessage(record) {
        const rendered = renderChatMessage(record.model, {
            theme: this.theme,
            thoughtsVisible: this.thoughtsVisible,
            compactToolUi: this.compactToolUi,
            toolUiRegistry: this.toolUiRegistry,
        });
        record.renderedMessage = rendered;
        return rendered;
    }
    shouldShowMessage(rendered) {
        if (!rendered.isAssistant)
            return true;
        if (rendered.hasVisibleText) {
            return rendered.hasVisibleText();
        }
        return true;
    }
    addSpacerIfNeeded() {
        const isFirst = this.chatContainer.children.length === 0;
        if (isFirst)
            return;
        this.chatContainer.addChild(new Spacer(1));
    }
    generateId() {
        return Math.random().toString(36).slice(2, 11);
    }
}
//# sourceMappingURL=chat_container.js.map