// src/lib/socketInstance.ts
import { io, Socket } from "socket.io-client";
import { getEnvUrl } from "@/shared/config/env";

let socket: Socket | null = null;

export const getSocketInstance = (userId: string) => {
  if (!socket) {
    const url = getEnvUrl("VITE_SOCKET_URL");

    socket = io(url, {
      transports: ["websocket"],
      query: { id: userId, type: "user" },
      reconnection: true,
      reconnectionAttempts: 5,
      reconnectionDelay: 2000,
    });
  }
  return socket;
};

export const disconnectSocket = () => {
  if (socket) {
    socket.disconnect();
    socket = null;
  }
};
