import React from 'react';
import socketio from "socket.io-client";

let socketUrl;
let socketPath;
if (process.env.NODE_ENV === 'development') {
    socketUrl = `http://localhost:3001`;
    socketPath = undefined;
} else {
    socketUrl = `${globalThis.location.host}`;
    socketPath = `${globalThis.location.pathname}socket.io`;
}

export const socket = socketio.connect(socketUrl, { path: socketPath, transports: ["websocket"] });
export const SocketContext = React.createContext();

// Konstanty websocket eventů, musí odpovídat těm na serveru!
export const EVENT_CONNECT = 'connect';
export const EVENT_DISCONNECT = 'disconnect';
export const EVENT_MESSAGE = 'message';
export const EVENT_PENDING_QR = 'pendingQr';
