/**
 * Same-origin relative path only. Blocks open redirects (`//…`, absolute URLs).
 */
export function safeRedirectPath(value: string | null | undefined, fallback = '/'): string {
  if (!value) return fallback
  if (!value.startsWith('/') || value.startsWith('//')) return fallback
  if (value.includes('://')) return fallback
  return value
}

/** Build `/login?redirect=…` (or another login path) from a return path. */
export function loginPathWithRedirect(returnTo: string, loginPath = '/login'): string {
  const safe = safeRedirectPath(returnTo, '')
  if (!safe) return loginPath
  return `${loginPath}?redirect=${encodeURIComponent(safe)}`
}
