/** 将 target_addr 转为可打开的 URL */
export function targetHref(addr) {
  if (!addr) return ''
  const trimmed = String(addr).trim()
  if (/^https?:\/\//i.test(trimmed)) return trimmed
  return `http://${trimmed}`
}

/** 域名解析：点击域名打开对外访问地址 */
export function hostHref(row) {
  const domain = row?.host?.trim()
  if (!domain) return ''
  if (/^https?:\/\//i.test(domain)) return domain
  const useHttps = row.auto_https || row.scheme === 'https'
  const scheme = useHttps ? 'https' : 'http'
  let loc = (row.location || '/').trim()
  if (!loc.startsWith('/')) loc = `/${loc}`
  const suffix = loc === '/' ? '' : loc
  return `${scheme}://${domain}${suffix}`
}

/** 隧道：点击监听端口打开服务端对外入口 */
export function tunnelHref(row) {
  const port = row?.listen_port
  if (!port) return ''
  const host = window.location.hostname || 'localhost'
  return `http://${host}:${port}`
}

export function openTarget(addr) {
  const url = targetHref(addr)
  if (url) window.open(url, '_blank', 'noopener,noreferrer')
}

export function openHost(row) {
  const url = hostHref(row)
  if (url) window.open(url, '_blank', 'noopener,noreferrer')
}

export function openTunnel(row) {
  const url = tunnelHref(row)
  if (url) window.open(url, '_blank', 'noopener,noreferrer')
}
