import React, { useState, useEffect, useCallback, useMemo } from 'react'; import { DeviceDetectorProvider, useDeviceDetection, DeviceType } from './device-detector'; // Simplified chat interface types export enum ChatMode { DESKTOP = 'desktop', MOBILE = 'mobile', AUTO = 'auto' } export interface ChatState { isOpen: boolean; mode: ChatMode; sessionId: string; } // Router configuration export interface UnifiedChatRouterConfig { defaultMode: ChatMode; enableDebug: boolean; enableTransitions: boolean; mobileBreakpoint: number; } const DEFAULT_CONFIG: UnifiedChatRouterConfig = { defaultMode: ChatMode.AUTO, enableDebug: false, enableTransitions: true, mobileBreakpoint: 768 }; // Context for chat router interface UnifiedChatRouterContextType { chatState: ChatState; config: UnifiedChatRouterConfig; openChat: () => void; closeChat: () => void; toggleChat: () => void; switchMode: (mode: ChatMode) => void; }const UnifiedChatRouterContext = React.createContext(undefined); // Hook to use the chat router export const useUnifiedChatRouter = (): UnifiedChatRouterContextType => { const context = React.useContext(UnifiedChatRouterContext); if (context === undefined) { throw new Error('useUnifiedChatRouter must be used within a UnifiedChatRouterProvider'); } return context; }; // Utility functions const generateSessionId = (): string => { return `chat_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`; }; const determineChatMode = (deviceInfo: any, config: UnifiedChatRouterConfig): ChatMode => { if (config.defaultMode !== ChatMode.AUTO) { return config.defaultMode; } // Auto-detect based on device if (deviceInfo.type === DeviceType.MOBILE || (deviceInfo.isTouchDevice && deviceInfo.screenWidth <= config.mobileBreakpoint)) { return ChatMode.MOBILE; } return ChatMode.DESKTOP; }; // Main router provider interface UnifiedChatRouterProviderProps { children: React.ReactNode; config?: Partial; }export const UnifiedChatRouterProvider: React.FC = ({ children, config: userConfig = {} }) => { const config = useMemo(() => ({ ...DEFAULT_CONFIG, ...userConfig }), [userConfig]); const { deviceInfo } = useDeviceDetection(); // Chat state const [chatState, setChatState] = useState(() => ({ isOpen: false, mode: determineChatMode(deviceInfo, config), sessionId: generateSessionId() })); // Update mode when device changes useEffect(() => { if (config.defaultMode === ChatMode.AUTO) { const newMode = determineChatMode(deviceInfo, config); if (newMode !== chatState.mode) { setChatState(prev => ({ ...prev, mode: newMode })); } } }, [deviceInfo, config, chatState.mode]); // Chat control functions const openChat = useCallback(() => { setChatState(prev => ({ ...prev, isOpen: true })); }, []); const closeChat = useCallback(() => { setChatState(prev => ({ ...prev, isOpen: false })); }, []); const toggleChat = useCallback(() => { setChatState(prev => ({ ...prev, isOpen: !prev.isOpen })); }, []); const switchMode = useCallback((mode: ChatMode) => { setChatState(prev => ({ ...prev, mode })); }, []); // Context value const contextValue: UnifiedChatRouterContextType = { chatState, config, openChat, closeChat, toggleChat, switchMode }; return ( {children} {config.enableDebug && } ); }; // Chat renderer component const ChatRenderer: React.FC = () => { const { chatState } = useUnifiedChatRouter(); if (!chatState.isOpen) return null; return (
{chatState.mode === ChatMode.MOBILE && ( )} {chatState.mode === ChatMode.DESKTOP && ( )}
); };// Mobile chat interface interface MobileChatInterfaceProps { sessionId: string; } const MobileChatInterface: React.FC = ({ sessionId }) => { const { closeChat } = useUnifiedChatRouter(); return (

Chat Support

Mobile Chat Interface
Session: {sessionId}
); };// Desktop chat interface interface DesktopChatInterfaceProps { sessionId: string; } const DesktopChatInterface: React.FC = ({ sessionId }) => { const { closeChat } = useUnifiedChatRouter(); return (

Chat Support

Desktop Chat Interface
Session: {sessionId}
); };// Debug panel const DebugPanel: React.FC = () => { const { chatState } = useUnifiedChatRouter(); const { deviceInfo } = useDeviceDetection(); if (process.env.NODE_ENV === 'production') return null; return (
Unified Chat Router Debug
Mode: {chatState.mode}
Open: {chatState.isOpen ? 'Yes' : 'No'}
Device: {deviceInfo.type}
Touch: {deviceInfo.isTouchDevice ? 'Yes' : 'No'}
Screen: {deviceInfo.screenWidth}x{deviceInfo.screenHeight}
Session: {chatState.sessionId.slice(-8)}
); }; // Unified floating chat button interface UnifiedFloatingChatButtonProps { position?: 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left'; size?: 'small' | 'medium' | 'large'; className?: string; }export const UnifiedFloatingChatButton: React.FC = ({ position = 'bottom-right', size = 'medium', className }) => { const { toggleChat, chatState } = useUnifiedChatRouter(); const getPositionStyles = () => { const positions = { 'bottom-right': { bottom: '20px', right: '20px' }, 'bottom-left': { bottom: '20px', left: '20px' }, 'top-right': { top: '20px', right: '20px' }, 'top-left': { top: '20px', left: '20px' } }; return positions[position]; }; const getSizeStyles = () => { const sizes = { small: { width: '48px', height: '48px', fontSize: '20px' }, medium: { width: '56px', height: '56px', fontSize: '24px' }, large: { width: '64px', height: '64px', fontSize: '28px' } }; return sizes[size]; }; return ( ); };// Complete unified chat system interface UnifiedChatSystemProps { config?: Partial; buttonProps?: Partial; children?: React.ReactNode; } export const UnifiedChatSystem: React.FC = ({ config, buttonProps, children }) => { return ( {children} ); }; export default UnifiedChatSystem;