const WS_BASE_URL = import.meta.env.VITE_GAME_WS_URL ?? '/api/v1/ws'

export function buildWsUrl(token: string): string {
    const baseUrl = /^wss?:\/\//.test(WS_BASE_URL)
        ? new URL(WS_BASE_URL)
        : new URL(
            WS_BASE_URL.startsWith('/') ? WS_BASE_URL : `/${WS_BASE_URL}`,
            `${window.location.protocol === 'https:' ? 'wss:' : 'ws:'}//${window.location.host}`,
        )

    baseUrl.searchParams.set('token', token)
    return baseUrl.toString()
}