import { gql } from 'graphql-tag'
import { useMemo } from 'react'
import Button from 'react-bootstrap/Button'
import InputGroup from 'react-bootstrap/InputGroup'
import { getGetServerSideProps } from '../../api/ssrApollo'
import { Form, Input, SubmitButton } from '../../components/form'
import { CenterLayout } from '../../components/layout'
import { useMutation, useQuery } from '@apollo/client'
import Link from 'next/link'
import { amountSchema } from '../../lib/validate'
import Countdown from 'react-countdown'
import { numWithUnits } from '../../lib/format'
import PageLoading from '../../components/page-loading'
import { useShowModal } from '../../components/modal'
import dynamic from 'next/dynamic'
import { SSR } from '../../lib/constants'
const GrowthPieChart = dynamic(() => import('../../components/charts').then(mod => mod.GrowthPieChart), {
  loading: () => 
Loading...
})
const REWARDS = gql`
{
  rewards {
    total
    sources {
      name
      value
    }
  }
}
`
function midnight (tz) {
  function tzOffset (tz) {
    const date = new Date()
    date.setMilliseconds(0)
    const targetDate = new Date(date.toLocaleString('en-US', { timeZone: tz }))
    const targetOffsetHours = (date.getTime() - targetDate.getTime()) / 1000 / 60 / 60
    return targetOffsetHours
  }
  const date = new Date()
  date.setHours(24, 0, 0, 0)
  return date.getTime() + tzOffset(tz) * 60 * 60 * 1000
}
export const getServerSideProps = getGetServerSideProps({ query: REWARDS })
export function RewardLine ({ total }) {
  const threshold = useMemo(() => midnight('America/Chicago'))
  return (
    <>
      {numWithUnits(total)} in rewards
      {threshold &&
          {props.formatted.hours}:{props.formatted.minutes}:{props.formatted.seconds}}
        />}
    >
  )
}
export default function Rewards ({ ssrData }) {
  const { data } = useQuery(REWARDS, SSR ? {} : { pollInterval: 1000, nextFetchPolicy: 'cache-and-network' })
  if (!data && !ssrData) return 
  const { rewards: { total, sources } } = data || ssrData
  return (
    
      
        
          
        
        
          learn about rewards
        
      
      
        
      
      
    
  )
}
export function DonateButton () {
  const showModal = useShowModal()
  const [donateToRewards] = useMutation(
    gql`
      mutation donateToRewards($sats: Int!) {
        donateToRewards(sats: $sats)
      }`)
  return (
    <>
      
    >
  )
}