import type {
  DestinationMarker,
  MarkerFeature,
  MarkerGeojson,
} from "@/types/components/maps/destinationMarkers"
import type { DestinationPagesHotelData } from "@/types/hotel"

export function mapMarkerDataToGeoJson(markers: DestinationMarker[]) {
  const features = markers.map<MarkerFeature>(
    ({ coordinates, ...properties }) => {
      return {
        type: "Feature",
        id: properties.id,
        geometry: {
          type: "Point",
          coordinates: [coordinates.lng, coordinates.lat],
        },
        properties,
      }
    }
  )

  const geoJson: MarkerGeojson = {
    type: "FeatureCollection",
    features,
  }

  return geoJson
}

export function getHotelMapMarkers(hotels: DestinationPagesHotelData[]) {
  const markers = hotels
    .map(({ hotel, url }) => ({
      id: hotel.id,
      type: hotel.hotelType || "regular",
      name: hotel.name,
      coordinates: hotel.location
        ? {
            lat: hotel.location.latitude,
            lng: hotel.location.longitude,
          }
        : null,
      url: url,
      tripadvisor: hotel.tripadvisor,
      amenities: hotel.detailedFacilities.slice(0, 3),
      image: getImage({ hotel, url }),
    }))

    .filter((item): item is DestinationMarker => !!item.coordinates)

  return markers
}

function getImage(hotel: DestinationPagesHotelData) {
  return {
    src: hotel.hotel.galleryImages?.[0]?.imageSizes.medium,
    alt:
      hotel.hotel.galleryImages?.[0]?.metaData.altText ||
      hotel.hotel.galleryImages?.[0]?.metaData.altText_En,
  }
}
