import { notFound } from "next/navigation" import { getLocations } from "@/lib/trpc/memoizedRequests" import { generateChildrenString, getHotelReservationQueryParams, } from "@/components/HotelReservation/SelectRate/RoomSelection/utils" import type { SelectHotelSearchParams } from "@/types/components/hotelReservation/selectHotel/selectHotelSearchParams" import type { Child, SelectRateSearchParams, } from "@/types/components/hotelReservation/selectRate/selectRate" import type { Location } from "@/types/trpc/routers/hotel/locations" interface HotelSearchDetails { city: Location hotel: Location urlSearchParams?: URLSearchParams adultsInRoom: number childrenInRoom?: string childrenInRoomArray?: Child[] } export async function getHotelSearchDetails({ searchParams, }: { searchParams: | (SelectHotelSearchParams & { [key: string]: string }) | (SelectRateSearchParams & { [key: string]: string }) }): Promise { const locations = await getLocations() if (!locations || "error" in locations) return null const city = locations.data.find( (location) => location.name.toLowerCase() === searchParams.city?.toLowerCase() ) const hotel = locations.data.find( (location) => "operaId" in location && location.operaId == searchParams.hotel ) if (!city && !hotel) return notFound() const urlSearchParams = new URLSearchParams(searchParams) const searchParamsObject = getHotelReservationQueryParams(urlSearchParams) let adultsInRoom = 1 let childrenInRoom: string | undefined = undefined let childrenInRoomArray: Child[] | undefined = undefined if (searchParamsObject.room && searchParamsObject.room.length > 0) { adultsInRoom = searchParamsObject.room[0].adults // TODO: Handle multiple rooms childrenInRoom = searchParamsObject.room[0].child ? generateChildrenString(searchParamsObject.room[0].child) : undefined // TODO: Handle multiple rooms childrenInRoomArray = searchParamsObject.room[0].child ? searchParamsObject.room[0].child : undefined // TODO: Handle multiple rooms } return { city: city!, hotel: hotel!, urlSearchParams, adultsInRoom, childrenInRoom, childrenInRoomArray, } }