interface User { id: string; username: string; avatar?: string; isOnline: boolean; lastSeen?: Date; status?: 'online' | 'away' | 'busy' | 'offline'; } interface Message { id: string; senderId: string; content: string; timestamp: Date; type: 'text' | 'image' | 'file' | 'emoji'; replyTo?: string; isEdited?: boolean; reactions?: MessageReaction[]; } interface MessageReaction { emoji: string; users: string[]; count: number; } interface ChatRoom { id: string; name: string; description?: string; members: User[]; isPrivate: boolean; createdAt: Date; lastActivity: Date; } interface WebSocketMessage { type: 'message' | 'user_join' | 'user_leave' | 'typing' | 'reaction' | 'edit'; payload: any; timestamp: Date; } interface ChatState { currentUser: User; users: User[]; messages: Message[]; typingUsers: User[]; isConnected: boolean; } interface ChatActions { sendMessage: (content: string, type?: Message['type']) => void; editMessage: (messageId: string, newContent: string) => void; deleteMessage: (messageId: string) => void; addReaction: (messageId: string, emoji: string) => void; setTyping: (isTyping: boolean) => void; connectWebSocket: () => void; disconnectWebSocket: () => void; } class WebSocketManager { private socket: WebSocket | null = null; private reconnectAttempts: number = 0; private maxReconnectAttempts: number = 5; private reconnectDelay: number = 1000; private messageQueue: WebSocketMessage[] = []; private isConnecting: boolean = false; constructor( private url: string, private onMessage: (message: WebSocketMessage) => void, private onConnectionChange: (connected: boolean) => void ) { } connect(): Promise { return new Promise((resolve, reject) => { if (this.isConnecting || (this.socket && this.socket.readyState === WebSocket.OPEN)) { resolve(); return; } this.isConnecting = true; try { this.socket = new WebSocket(this.url); this.socket.onopen = () => { this.isConnecting = false; this.reconnectAttempts = 0; this.onConnectionChange(true); this.processMessageQueue(); resolve(); }; this.socket.onclose = () => { this.isConnecting = false; this.onConnectionChange(false); this.attemptReconnect(); }; this.socket.onmessage = (event) => { try { const message: WebSocketMessage = JSON.parse(event.data); this.onMessage(message); } catch (error) { console.error('Failed to parse WebSocket message:', error); } }; this.socket.onerror = (error) => { this.isConnecting = false; console.error('WebSocket error:', error); reject(error); }; } catch (error) { this.isConnecting = false; reject(error); } }); } disconnect(): void { if (this.socket) { this.socket.close(); this.socket = null; } this.onConnectionChange(false); } send(message: WebSocketMessage): void { if (this.socket && this.socket.readyState === WebSocket.OPEN) { this.socket.send(JSON.stringify(message)); } else { this.messageQueue.push(message); } } private attemptReconnect(): void { if (this.reconnectAttempts < this.maxReconnectAttempts) { this.reconnectAttempts++; setTimeout(() => { console.log(`Attempting to reconnect (${this.reconnectAttempts}/${this.maxReconnectAttempts})`); this.connect().catch(() => { console.error('Reconnection failed'); }); }, this.reconnectDelay * this.reconnectAttempts); } else { console.error('Max reconnection attempts reached'); } } private processMessageQueue(): void { while (this.messageQueue.length > 0) { const message = this.messageQueue.shift(); if (message) { this.send(message); } } } } class MessageValidator { static validateMessage(content: string, type: Message['type']): { isValid: boolean; errors: string[] } { const errors: string[] = []; if (!content || content.trim().length === 0) { errors.push('Message content cannot be empty'); } if (content.length > 2000) { errors.push('Message content cannot exceed 2000 characters'); } if (type === 'text' && content.includes('