import { useContext } from 'react';
import { ConversationContext, useConversations, } from '@/stores/Conversations/ConversationContext';
import { AuthContext } from '@/stores/AuthContext';
import { RealTimeAPI } from '@/lib/realTimeAPI';

// const WS_URL = 'ws://202.103.68.144:8888/whatever/';
// const WS_URL = 'ws://120.79.9.217:10022/whatever/';
const WS_URL = 'wss://p9axztuwd7x8a7.mycht.cn/whatsapp_callback'; // prod:

export const ConversationProvider = ({ children, loginUser, realtimeAPI }) => {

  const conversations = useConversations({loginUser, realtimeAPI});
  return <ConversationContext.Provider value={conversations}>{children}</ConversationContext.Provider>;
};

// export default ConversationProvider;

const AuthAndConversationProvider = ({ children }) => {
  const { loginUser } = useContext(AuthContext);
  const {userId} = loginUser;
  const realtimeAPI = new RealTimeAPI({ url: `${WS_URL}?opisn=${userId || ''}&_spam=${Date.now().toString()}`, protocol: 'WhatsApp' });

  return (
    <ConversationProvider loginUser={loginUser} realtimeAPI={realtimeAPI} >
      {children}
    </ConversationProvider>
  );
};

export default AuthAndConversationProvider;
