import { getSearchTokens } from "./getSearchTokens"

import type { Location } from "@/types/trpc/routers/hotel/locations"
import type { AutoCompleteLocation } from "../schema"

export function mapLocationToAutoCompleteLocation(
  location: (Location & { url?: string }) | null | undefined
): AutoCompleteLocation | null {
  if (!location) return null

  return {
    id: location.id,
    name: location.name,
    type: location.type,
    url: location.url,
    searchTokens: getSearchTokens(location),
    destination:
      location.type === "hotels"
        ? location.relationships.city.name
        : location.country,
    cityIdentifier:
      location.type === "cities" ? location.cityIdentifier : undefined,
  }
}
