import { type HotelListingHotelData, type HotelSortItem, HotelSortOption, } from "@scandic-hotels/trpc/types/hotel" const HOTEL_SORTING_STRATEGIES: Partial< Record< HotelSortOption, (a: HotelListingHotelData, b: HotelListingHotelData) => number > > = { [HotelSortOption.Name]: function (a, b) { return a.hotel.name.localeCompare(b.hotel.name) }, [HotelSortOption.TripAdvisorRating]: function (a, b) { return (b.hotel.tripadvisor ?? 0) - (a.hotel.tripadvisor ?? 0) }, } export function getFilteredHotels( hotels: HotelListingHotelData[], filters: string[] ) { if (filters.length) { return hotels.filter(({ hotel }) => filters.every((filter) => { const matchesFacility = hotel.detailedFacilities.some( (facility) => facility.slug === filter ) const matchesCountry = hotel.countryCode.toLowerCase() === filter.toLowerCase() return matchesFacility || matchesCountry }) ) } return hotels } export function getSortedHotels( hotels: HotelListingHotelData[], sortOption: HotelSortOption ) { const sortFn = HOTEL_SORTING_STRATEGIES[sortOption] return sortFn ? [...hotels].sort(sortFn) : hotels } export function isValidSortOption( value: string, sortItems: HotelSortItem[] ): value is HotelSortOption { return sortItems.map((item) => item.value).includes(value as HotelSortOption) }