import { useState, useEffect } from 'react'
import client from '../api/client'

// Polls /api/orders/pending-prints every 8s and returns the count of pending print jobs.
export default function useFailedPrintCount() {
  const [count, setCount] = useState(0)

  useEffect(() => {
    let cancelled = false

    async function refresh() {
      try {
        const res = await client.get('/api/orders/pending-prints')
        if (!cancelled) setCount(res.data?.count ?? 0)
      } catch {
        // Network error — leave count as-is
      }
    }

    refresh()
    const id = setInterval(refresh, 8000)
    return () => {
      cancelled = true
      clearInterval(id)
    }
  }, [])

  return count
}
