import { useZxing } from "react-zxing";
import styles from "./scanner.module.css"

type Props = {
  paused: boolean,
  onScan: (string: string) => void
}

const Scanner = (props: Props) => {
  const { ref } = useZxing({
    constraints: {
      video: {
        facingMode: 'environment',
        width: { ideal: 1280 },
        height: { ideal: 720 },
        frameRate: { ideal: 30, max: 60 }
      }
    },
    timeBetweenDecodingAttempts: 100,
    paused: props.paused,
    onDecodeResult(result) {
      props.onScan(result.getText());
    },
  })

  const restartCam = () => {
    ref.current?.pause()
    ref.current?.play()
  }

  return (
  <div className="relative w-full h-[125px] overflow-hidden">
    <video ref={ref} className="w-full h-full object-cover" />
  </div>
  )
}

export default Scanner