import { useAuthStore } from '@/stores/auth'
import { useAlertStore } from '@/stores/alerts'
import { usePolling } from '@/composables/usePolling'
import { useAlertNotification } from '@/composables/useAlertNotification'

export function useCriticalAlertPolling(intervalMs = 10_000) {
  const authStore = useAuthStore()
  const alertStore = useAlertStore()
  const { notifyNewCriticalAlerts } = useAlertNotification()

  async function poll() {
    if (!authStore.isAuthenticated) {
      alertStore.dismissAllBannerAlerts()
      return
    }
    const newAlerts = await alertStore.pollOpenAlerts()
    notifyNewCriticalAlerts(newAlerts)
  }

  usePolling(poll, intervalMs)
}
