"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConversationManager = void 0;
class ConversationManager {
    conversations = new Map();
    currentConversationId = null;
    context;
    conversationCounter = 0;
    constructor(context) {
        this.context = context;
        this.loadConversations();
    }
    loadConversations() {
        const saved = this.context.globalState.get('axion-ai.conversations');
        if (saved) {
            for (const [id, conv] of Object.entries(saved)) {
                conv.createdAt = new Date(conv.createdAt);
                conv.updatedAt = new Date(conv.updatedAt);
                this.conversations.set(id, conv);
            }
        }
    }
    async saveConversations() {
        const toSave = {};
        // Only save last 50 conversations
        const sorted = Array.from(this.conversations.values())
            .sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime())
            .slice(0, 50);
        for (const conv of sorted) {
            toSave[conv.id] = conv;
        }
        await this.context.globalState.update('axion-ai.conversations', toSave);
    }
    createConversation(mode, modelId) {
        const id = `conv_${++this.conversationCounter}_${Date.now()}`;
        const conversation = {
            id,
            title: 'New Chat',
            messages: [],
            mode,
            modelId,
            createdAt: new Date(),
            updatedAt: new Date()
        };
        this.conversations.set(id, conversation);
        this.currentConversationId = id;
        this.saveConversations();
        return conversation;
    }
    getCurrentConversation() {
        if (!this.currentConversationId) {
            return null;
        }
        return this.conversations.get(this.currentConversationId) || null;
    }
    getConversation(id) {
        return this.conversations.get(id) || null;
    }
    setCurrentConversation(id) {
        if (this.conversations.has(id)) {
            this.currentConversationId = id;
            return true;
        }
        return false;
    }
    addMessage(message) {
        const conversation = this.getCurrentConversation();
        if (conversation) {
            conversation.messages.push(message);
            conversation.updatedAt = new Date();
            // Generate title from first user message
            if (conversation.title === 'New Chat' && message.role === 'user') {
                conversation.title = message.content.substring(0, 50) + (message.content.length > 50 ? '...' : '');
            }
            this.saveConversations();
        }
    }
    getMessages() {
        const conversation = this.getCurrentConversation();
        return conversation?.messages || [];
    }
    getAllConversations() {
        return Array.from(this.conversations.values())
            .sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime());
    }
    deleteConversation(id) {
        const result = this.conversations.delete(id);
        if (this.currentConversationId === id) {
            this.currentConversationId = null;
        }
        this.saveConversations();
        return result;
    }
    clearCurrentConversation() {
        const conversation = this.getCurrentConversation();
        if (conversation) {
            conversation.messages = [];
            conversation.title = 'New Chat';
            conversation.updatedAt = new Date();
            this.saveConversations();
        }
    }
    updateConversationMode(mode) {
        const conversation = this.getCurrentConversation();
        if (conversation) {
            conversation.mode = mode;
            this.saveConversations();
        }
    }
    updateConversationModel(modelId) {
        const conversation = this.getCurrentConversation();
        if (conversation) {
            conversation.modelId = modelId;
            this.saveConversations();
        }
    }
    clearAll() {
        this.conversations.clear();
        this.currentConversationId = null;
        this.saveConversations();
    }
}
exports.ConversationManager = ConversationManager;
//# sourceMappingURL=ConversationManager.js.map