import { DesktopIpcError } from '@/lib/api/desktop'
import type { DesktopErrorEnvelope } from '@/lib/types/desktop'

export function asEnvelope(error: unknown): DesktopErrorEnvelope {
  if (error instanceof DesktopIpcError) return error.envelope
  if (error instanceof Error) {
    return {
      kind: 'runtime',
      message: error.message,
      hint: '请查看浏览器控制台或 Tauri 日志。',
      explain: error.stack ?? error.message,
    }
  }
  return {
    kind: 'runtime',
    message: String(error),
    hint: '未知错误。',
    explain: String(error),
  }
}
