import { gql, useMutation, useQuery } from '@apollo/client'
import { signIn } from 'next-auth/react'
import { useEffect } from 'react'
import Col from 'react-bootstrap/Col'
import Container from 'react-bootstrap/Container'
import Row from 'react-bootstrap/Row'
import AccordianItem from './accordian-item'
import Qr, { QrSkeleton } from './qr'
import styles from './lightning-auth.module.css'
import BackIcon from '@/svgs/arrow-left-line.svg'
import { useRouter } from 'next/router'
import { FAST_POLL_INTERVAL, SSR } from '@/lib/constants'
function QrAuth ({ k1, encodedUrl, callbackUrl, multiAuth }) {
  const query = gql`
  {
    lnAuth(k1: "${k1}") {
      pubkey
      k1
    }
  }`
  const { data } = useQuery(query, SSR ? {} : { pollInterval: FAST_POLL_INTERVAL, nextFetchPolicy: 'cache-and-network' })
  useEffect(() => {
    if (data?.lnAuth?.pubkey) {
      signIn('lightning', { ...data.lnAuth, callbackUrl, multiAuth })
    }
  }, [data?.lnAuth])
  useEffect(() => {
    if (typeof window.webln === 'undefined') return
    // optimistically use WebLN for authentication
    async function effect () {
      // this will also enable our WebLN wallet
      await window.webln.enable()
      await window.webln.lnurl(encodedUrl)
    }
    effect()
  }, [encodedUrl])
  // output pubkey and k1
  return (
    
          {text || 'Login'} with Lightning