/**
 * P2P 内存存储 - 简化版，无 IndexedDB 依赖
 */
export class P2PStoreMemory {
    history = [];
    messages = [];
    offlineQueue = [];
    unreadCount = 0;
    preferences = {
        autoReconnect: true,
        autoConnectOnStartup: true,
        maxRetries: 3,
        notifications: {
            newMessage: true,
            connectionEstablished: true
        }
    };
    // ==================== 连接历史 ====================
    async addToHistory(entry) {
        const existingIndex = this.history.findIndex(h => h.cid === entry.cid);
        if (existingIndex >= 0) {
            this.history[existingIndex] = {
                ...this.history[existingIndex],
                ...entry,
                lastConnectedAt: Date.now()
            };
            return this.history[existingIndex].id;
        }
        const id = crypto.randomUUID();
        this.history.push({
            ...entry,
            id,
            lastConnectedAt: Date.now()
        });
        return id;
    }
    async getHistory() {
        return [...this.history].sort((a, b) => {
            if (a.isPinned !== b.isPinned)
                return a.isPinned ? -1 : 1;
            return b.lastMessageAt - a.lastMessageAt;
        });
    }
    async updateHistory(id, updates) {
        const index = this.history.findIndex(h => h.id === id);
        if (index >= 0) {
            this.history[index] = { ...this.history[index], ...updates };
        }
    }
    async deleteHistory(id) {
        this.history = this.history.filter(h => h.id !== id);
    }
    // ==================== 消息 ====================
    async addMessage(msg) {
        this.messages.push({
            ...msg,
            id: crypto.randomUUID(),
            isRead: false
        });
        this.unreadCount++;
    }
    async getMessages() {
        return [...this.messages].sort((a, b) => b.timestamp - a.timestamp);
    }
    async markRead(id) {
        const msg = this.messages.find(m => m.id === id);
        if (msg && !msg.isRead) {
            msg.isRead = true;
            this.unreadCount = Math.max(0, this.unreadCount - 1);
        }
    }
    async markAllRead() {
        this.messages.forEach(m => m.isRead = true);
        this.unreadCount = 0;
    }
    getUnreadCount() {
        return this.unreadCount;
    }
    // ==================== 离线队列 ====================
    async addToQueue(msg) {
        const id = crypto.randomUUID();
        this.offlineQueue.push({
            ...msg,
            id,
            createdAt: Date.now(),
            retryCount: 0,
            status: 'pending'
        });
        return id;
    }
    async getQueue() {
        return [...this.offlineQueue];
    }
    async updateQueueMessage(id, updates) {
        const msg = this.offlineQueue.find(m => m.id === id);
        if (msg) {
            Object.assign(msg, updates);
        }
    }
    async removeFromQueue(id) {
        this.offlineQueue = this.offlineQueue.filter(m => m.id !== id);
    }
    async getQueueCount() {
        return this.offlineQueue.length;
    }
    // ==================== 偏好设置 ====================
    getPreferences() {
        return { ...this.preferences };
    }
    setPreferences(prefs) {
        this.preferences = { ...this.preferences, ...prefs };
    }
    // ==================== 导出/清除 ====================
    exportData() {
        return JSON.stringify({
            history: this.history,
            messages: this.messages,
            preferences: this.preferences
        }, null, 2);
    }
    clear() {
        this.history = [];
        this.messages = [];
        this.offlineQueue = [];
        this.unreadCount = 0;
    }
}
// 全局单例
export const p2pStore = new P2PStoreMemory();
