export function createDataResponse( { countryParam, cityParam, hotels, }: { countryParam: string cityParam?: string hotels: Array<{ name: string distanceToCentre?: number | undefined relationships: { city: { name: string } } images?: { large?: string } | undefined }> }, options?: { includeCity: boolean } ) { const { includeCity = false } = options || {} return { country: countryParam.toLowerCase(), city: cityParam?.toLowerCase(), hotels: hotels .map((h) => ({ name: h.name, city: includeCity ? h.relationships.city.name : undefined, metersToCityCentre: h.distanceToCentre, images: { tiny: createImageUrl({ src: h.images?.large, width: 300 }), small: createImageUrl({ src: h.images?.large, width: 500 }), medium: createImageUrl({ src: h.images?.large, width: 1080 }), large: createImageUrl({ src: h.images?.large, width: 1920 }), }, })) .toSorted((a, b) => a.name.localeCompare(b.name)) .toSorted((a, b) => { return ( (a.metersToCityCentre ?? Infinity) - (b.metersToCityCentre ?? Infinity) ) }) .toSorted((a, b) => { if (!includeCity) return 0 if (!a.city || !b.city) return 0 return a.city.localeCompare(b.city) }), } } function createImageUrl({ src, width, }: { src: string | null | undefined width: number }) { if (!src) return undefined return `https://img.scandichotels.com/.netlify/images?url=${encodeURIComponent(src)}&w=${width}&q=90` }