import lndService from 'ln-service'
import lnd from '../lnd'

const cache = new Map()
const expiresIn = 1000 * 30 // 30 seconds in milliseconds

async function fetchBlockHeight () {
  let blockHeight = 0
  try {
    const height = await lndService.getHeight({ lnd })
    blockHeight = height.current_block_height
  } catch (err) {
    console.error('fetchBlockHeight', err)
  }
  cache.set('block', { height: blockHeight, createdAt: Date.now() })
  return blockHeight
}

async function getBlockHeight () {
  if (cache.has('block')) {
    const { height, createdAt } = cache.get('block')
    const expired = createdAt + expiresIn < Date.now()
    if (expired) fetchBlockHeight().catch(console.error) // update cache
    return height // serve stale block height (this on the SSR critical path)
  } else {
    fetchBlockHeight().catch(console.error)
  }
  return 0
}

export default {
  Query: {
    blockHeight: async (parent, opts, ctx) => {
      return await getBlockHeight()
    }
  }
}
