import { Suspense } from "react"

import {
  getPageSettingsBookingCode,
  isBookingWidgetHidden,
} from "@/lib/trpc/memoizedRequests"

import BookingWidgetClient, { BookingWidgetSkeleton } from "./Client"

import type { BookingWidgetProps } from "@/types/components/bookingWidget"

export async function BookingWidget(props: BookingWidgetProps) {
  return (
    <Suspense fallback={<BookingWidgetSkeleton />}>
      <InternalBookingWidget {...props} />
    </Suspense>
  )
}

async function InternalBookingWidget({
  type,
  bookingWidgetSearchParams,
}: BookingWidgetProps) {
  const isHidden = await isBookingWidgetHidden()

  if (isHidden) {
    return null
  }

  let pageSettingsBookingCodePromise: Promise<string> | null = null
  if (!bookingWidgetSearchParams.bookingCode) {
    pageSettingsBookingCodePromise = getPageSettingsBookingCode()
  }

  return (
    <BookingWidgetClient
      type={type}
      bookingWidgetSearchParams={bookingWidgetSearchParams}
      pageSettingsBookingCodePromise={pageSettingsBookingCodePromise}
    />
  )
}
