import React, { createContext, useContext } from 'react';
import useJamServer, { ConnectionStatus } from '../hooks/useJamServer';

const JamServerContext = createContext(null);

export const JamServerProvider = ({ children }) => {
  const { isConnected,
    connectionStatus,
    reconnectAttempts,
    lastError,
    jamClient,
    server,
    registerMessageCallback,
    unregisterMessageCallback,
    subscribe,
    unsubscribe } = useJamServer(process.env.REACT_APP_WEBSOCKET_GATEWAY_URL);

  return (
    <JamServerContext.Provider value={{
      ConnectionStatus,
      connectionStatus,
      isConnected,
      jamClient,
      server,
      reconnectAttempts,
      lastError,
      registerMessageCallback,
      unregisterMessageCallback,
      subscribe,
      unsubscribe,
    }}
    >
      {children}
    </JamServerContext.Provider>
  );
};

export const useJamServerContext = () => useContext(JamServerContext);
