import { safeRedirect } from '@/lib/utils'

export function continueWithManagedGoogle(redirectPath: string) {
  const brokerUrl = import.meta.env.VITE_GOOGLE_AUTH_BROKER_URL as string | undefined
  if (!brokerUrl) throw new Error('O broker gerenciado do Google não foi configurado.')
  const redirect = safeRedirect(redirectPath)
  sessionStorage.setItem('auth:redirect', redirect)
  const callback = `${window.location.origin}/auth/callback`
  const url = new URL(brokerUrl)
  url.searchParams.set('callback', callback)
  url.searchParams.set('redirect', redirect)
  window.location.assign(url.toString())
}
