import { getHotelsByCityIdentifier } from "@/lib/trpc/memoizedRequests"

import { getIntl } from "@/i18n"
import DestinationDataProvider from "@/providers/DestinationDataProvider"

import type { SortItem } from "@/types/components/destinationFilterAndSort"
import { SortOption } from "@/types/enums/destinationFilterAndSort"

interface HotelDataContainerProps extends React.PropsWithChildren {
  cityIdentifier: string
}

export function preload(cityIdentifier: string) {
  void getHotelsByCityIdentifier(cityIdentifier)
}

export default async function HotelDataContainer({
  cityIdentifier,
  children,
}: HotelDataContainerProps) {
  const intl = await getIntl()
  const hotels = await getHotelsByCityIdentifier(cityIdentifier)

  const sortItems: SortItem[] = [
    {
      label: intl.formatMessage({
        defaultMessage: "Distance to city center",
      }),
      value: SortOption.Distance,
      isDefault: true,
    },
    {
      label: intl.formatMessage({
        defaultMessage: "Name",
      }),
      value: SortOption.Name,
    },
    {
      label: intl.formatMessage({
        defaultMessage: "TripAdvisor rating",
      }),
      value: SortOption.TripAdvisorRating,
    },
  ]

  return (
    <DestinationDataProvider allHotels={hotels} sortItems={sortItems}>
      {children}
    </DestinationDataProvider>
  )
}
