const livekitEndpoint = (url: string, groupId: string) => {
  const httpUrl = url
    .replace(/^wss:\/\//, "https://")
    .replace(/^ws:\/\//, "http://")
    .replace(/\/$/, "")
  return `${httpUrl}/.well-known/nip29/livekit/${groupId}`
}

export const checkRelayHasLivekit = async (url: string): Promise<boolean> => {
  const endpoint = livekitEndpoint(url, "nop")

  try {
    // Zooid returns 401 when livekit is configured and 404 if it is not.
    const response = await fetch(endpoint)
    return response.status === 401
  } catch {
    return false
  }
}

export const getLivekitEndpoint = (url: string, groupId: string) => livekitEndpoint(url, groupId)
