import { safeTry } from "@scandic-hotels/common/utils/safeTry" import { SEARCH_TYPE_REDEMPTION } from "@scandic-hotels/trpc/constants/booking" import { type HotelLocation, isHotelLocation, type Location, } from "@scandic-hotels/trpc/types/locations" import { getLocations } from "../trpc/memoizedRequests/getLocations" import type { Lang } from "@scandic-hotels/common/constants/language" import type { Child } from "@scandic-hotels/trpc/types/child" import type { BookingSearchType } from "./searchType" interface HotelSearchDetails { city: Location | null cityIdentifier: string | undefined hotel: HotelLocation | null redemption?: boolean } export async function getHotelSearchDetails(params: { hotelId?: string city?: string rooms?: { adults: number childrenInRoom?: Child[] }[] searchType?: BookingSearchType isAlternativeHotels?: boolean lang: Lang }): Promise { const [locations, error] = await safeTry(getLocations(params.lang)) if (!locations || error) { return null } const hotel = params.hotelId ? ((locations.find( (location) => isHotelLocation(location) && "operaId" in location && location.operaId === params.hotelId ) as HotelLocation | undefined) ?? null) : null if (params.isAlternativeHotels && !hotel) { return null } const cityIdentifier = params.isAlternativeHotels ? hotel?.relationships.city.cityIdentifier : params.city const city = cityIdentifier ? (locations.find( (location) => "cityIdentifier" in location && location.cityIdentifier?.toLowerCase() === cityIdentifier.toLowerCase() ) ?? null) : null if (!city && !hotel) return null if (params.isAlternativeHotels && (!city || !hotel)) return null return { city, cityIdentifier, hotel, redemption: params.searchType === SEARCH_TYPE_REDEMPTION, } }