export function resolveBrowserAppUrl(fallback: string): string {
  if (typeof window === 'undefined') return fallback

  try {
    const target = new URL(fallback)
    target.protocol = window.location.protocol
    target.hostname = window.location.hostname
    return target.toString().replace(/\/$/, '')
  } catch {
    return fallback
  }
}

export function resolveServerAppUrl(fallback: string, host: string | null, proto?: string | null): string {
  if (!host) return fallback

  try {
    const target = new URL(fallback)
    target.protocol = `${proto || target.protocol.replace(':', '')}:`
    target.host = host
    return target.toString().replace(/\/$/, '')
  } catch {
    return fallback
  }
}
