"use client"

import { useEffect, useRef } from "react"
import { useMutation } from "convex/react"
import { api } from "@/convex/_generated/api"

export function useDefaultQueues(tenantId?: string | null) {
  const ensureDefaults = useMutation(api.bootstrap.ensureDefaults)
  const pendingRef = useRef(false)

  useEffect(() => {
    if (!tenantId) return
    if (pendingRef.current) return
    pendingRef.current = true
    ensureDefaults({ tenantId })
      .catch(() => {
        pendingRef.current = false
      })
  }, [ensureDefaults, tenantId])
}
