import type { HotelPin } from "@/types/components/hotelReservation/selectHotel/map"
import type { HotelResponse } from "@/components/HotelReservation/SelectHotel/helpers"

export function getVisibleHotelPins(
  map: google.maps.Map | null,
  filteredHotelPins: HotelPin[]
) {
  if (!map || !filteredHotelPins) return []

  const bounds = map.getBounds()
  if (!bounds) return []

  return filteredHotelPins.filter((pin) => {
    const { lat, lng } = pin.coordinates
    return bounds.contains({ lat, lng })
  })
}

export function getVisibleHotels(
  hotels: HotelResponse[],
  filteredHotelPins: HotelPin[],
  map: google.maps.Map | null
) {
  const visibleHotelPins = getVisibleHotelPins(map, filteredHotelPins)
  const visibleHotels = hotels.filter((hotel) =>
    visibleHotelPins.some((pin) => pin.operaId === hotel.hotel.operaId)
  )
  return visibleHotels
}
