import { useState } from 'react'

function detectNoHardwareAcceleration() {
  const canvas = document.createElement('canvas')
  const gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl')
  if (!gl) return false

  const webgl = gl as WebGLRenderingContext
  const dbg = webgl.getExtension('WEBGL_debug_renderer_info')
  if (!dbg) return false
  const renderer = webgl.getParameter(dbg.UNMASKED_RENDERER_WEBGL)
  return /swiftshader|software/i.test(renderer)
}

export function useHardwareAccelerationWarning() {
  const [noHwAccel, setNoHwAccel] = useState(detectNoHardwareAcceleration)

  return {
    noHwAccel,
    dismissNoHwAccel: () => setNoHwAccel(false),
  }
}
