import { useCallback, useRef, useState } from 'react'

export function useCameraGate() {
  const videoRef = useRef<HTMLVideoElement>(null)
  const [cameraReady, setCameraReady] = useState(false)
  const [cameraDenied, setCameraDenied] = useState(false)

  const enableCamera = useCallback(() => {
    navigator.mediaDevices
      .getUserMedia({ video: true })
      .then((stream) => {
        if (videoRef.current) {
          videoRef.current.srcObject = stream
        }
        setCameraDenied(false)
        setCameraReady(true)
      })
      .catch(() => {
        setCameraDenied(true)
      })
  }, [])

  return {
    videoRef,
    cameraReady,
    cameraDenied,
    enableCamera,
  }
}
