import { spawn } from 'node:child_process'

export function openBrowser(url: string): boolean {
  const target = String(url ?? '').trim()
  if (!target)
    return false

  const platform = process.platform
  let cmd = ''
  let args: string[] = []

  if (platform === 'darwin') {
    cmd = 'open'
    args = [target]
  }
  else if (platform === 'win32') {
    cmd = 'cmd'
    args = ['/c', 'start', '', target]
  }
  else {
    cmd = 'xdg-open'
    args = [target]
  }

  try {
    const child = spawn(cmd, args, { stdio: 'ignore', detached: true })
    child.unref()
    return true
  }
  catch {
    return false
  }
}
