"use client"

import { BookingStatusEnum } from "@/constants/booking"
import { trpc } from "@/lib/trpc/client"

export function useHandleBookingStatus(
  confirmationNumber: string | null,
  expectedStatus: BookingStatusEnum,
  maxRetries: number,
  retryInterval: number
) {
  const query = trpc.booking.status.useQuery(
    { confirmationNumber: confirmationNumber ?? "" },
    {
      enabled: !!confirmationNumber,
      refetchInterval: (query) => {
        if (query.state.error || query.state.dataUpdateCount >= maxRetries) {
          return false
        }

        if (query.state.data?.reservationStatus === expectedStatus) {
          return false
        }

        return retryInterval
      },
      refetchIntervalInBackground: true,
      refetchOnWindowFocus: false,
      refetchOnMount: false,
      retry: false,
    }
  )

  return query
}
