import type { HotelData } from "@/types/components/hotelReservation/selectHotel/hotelCardListingProps" import { SortOrder } from "@/types/components/hotelReservation/selectHotel/hotelSorter" export function getSortedHotels({ hotels, sortBy, }: { hotels: HotelData[] sortBy: string }) { const getPricePerNight = (hotel: HotelData): number => hotel.price?.member?.localPrice?.pricePerNight ?? hotel.price?.public?.localPrice?.pricePerNight ?? Infinity const sortingStrategies: Record< string, (a: HotelData, b: HotelData) => number > = { [SortOrder.Name]: (a: HotelData, b: HotelData) => a.hotelData.name.localeCompare(b.hotelData.name), [SortOrder.TripAdvisorRating]: (a: HotelData, b: HotelData) => (b.hotelData.ratings?.tripAdvisor.rating ?? 0) - (a.hotelData.ratings?.tripAdvisor.rating ?? 0), [SortOrder.Price]: (a: HotelData, b: HotelData) => getPricePerNight(a) - getPricePerNight(b), [SortOrder.Distance]: (a: HotelData, b: HotelData) => a.hotelData.location.distanceToCentre - b.hotelData.location.distanceToCentre, } return [...hotels].sort( sortingStrategies[sortBy] ?? sortingStrategies[SortOrder.Distance] ) }