import {
  getDestinationCityPagesByCountry,
  getHotelsByCountry,
} from "@/lib/trpc/memoizedRequests"

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

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

interface CityDataContainerProps extends React.PropsWithChildren {
  country: Country
}

export function preload(country: Country) {
  void getHotelsByCountry(country)
  void getDestinationCityPagesByCountry(country)
}

export default async function CityDataContainer({
  country,
  children,
}: CityDataContainerProps) {
  const intl = await getIntl()
  const [hotels, cities] = await Promise.all([
    getHotelsByCountry(country),
    getDestinationCityPagesByCountry(country),
  ])

  const sortItems: SortItem[] = [
    {
      label: intl.formatMessage({
        defaultMessage: "Recommended",
      }),
      value: SortOption.Recommended,
      isDefault: true,
    },
    {
      label: intl.formatMessage({
        defaultMessage: "Name",
      }),
      value: SortOption.Name,
    },
  ]

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