/**
 * Normalize URLs for window.open / shell so Electron can intercept with http(s).
 * Handles whitespace, protocol-relative //, and path-only relative URLs.
 */
export function normalizeExternalUrl(raw: string): string | null {
  const s = raw.trim()
  if (!s) return null
  if (/^https?:\/\//i.test(s)) return s
  if (s.startsWith('//')) return `https:${s}`
  try {
    const base =
      typeof window !== 'undefined' && window.location?.origin
        ? window.location.origin
        : import.meta.env.DEV
          ? 'http://127.0.0.1:3000'
          : 'https://api.hnyunzhu.com'
    return new URL(s, base).href
  } catch {
    return null
  }
}
