import { createQrMatrix } from '@/lib/qr-code'

export function QrCodeSvg({
  value,
  label = '二维码',
  className = '',
}: {
  value: string
  label?: string
  className?: string
}) {
  const matrix = createQrMatrix(value || 'EMPTY')
  const scale = 8
  const quiet = 4
  const size = matrix.length
  const dimension = (size + quiet * 2) * scale
  const cells = matrix.flatMap((row, y) => row.map((dark, x) => ({ x, y, dark }))).filter((cell) => cell.dark)

  return (
    <svg viewBox={`0 0 ${dimension} ${dimension}`} role="img" aria-label={label} className={className}>
      <rect width={dimension} height={dimension} rx="16" fill="white" />
      {cells.map((cell) => (
        <rect key={`${cell.x}-${cell.y}`} x={(cell.x + quiet) * scale} y={(cell.y + quiet) * scale} width={scale} height={scale} rx="1.1" fill="#0f172a" />
      ))}
    </svg>
  )
}
