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, booking }: BookingWidgetProps) {
  const isHidden = await isBookingWidgetHidden()

  if (isHidden) {
    return null
  }

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

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