import { io } from 'socket.io-client';
import { socketUrl } from '@/config/env';
import { userSetting } from '@/plugins/setting';
import { configApp } from '@/config/app';

export const socketAuthPayload = () => ({
  ref: 'extension',
  version: configApp.verText,
  manifest: configApp.manifest,
  token: userSetting.userToken.get(),
  username: userSetting.userName.get(),
  id: userSetting.userId.get(),
});

/**
 *
 * @returns {SocketClient}
 */
export const socketConnect = () => {
  return io(socketUrl, {
    auth: socketAuthPayload(),
    transports: ['websocket'],
    reconnectionAttempts: 5,
    reconnectionDelay: 2000,
  });
};

export const socketEvent = {
  connect: 'connect',
  disconnected: 'disconnected',
  auth: 'extension:auth',
};
