/* eslint-disable @typescript-eslint/no-misused-spread */ import { NitroModules } from "react-native-nitro-modules" import type { Places, AutocompleteOptions as NativeOptions, GetPlaceOptions } from "./specs/Places.nitro" import { CircularLocationBounds, RectangularLocationBounds } from "./locationBounds" const native = NitroModules.createHybridObject("Places") export type LocationBounds = CircularLocationBounds | RectangularLocationBounds export type AutocompleteOptions = Omit & { locationBias?: LocationBounds locationRestriction?: LocationBounds } export type { GetPlaceOptions } const toNative = (options?: AutocompleteOptions): NativeOptions | undefined => { if (!options) { return options } return { ...options, locationBias: options.locationBias ? { ...options.locationBias } : undefined, locationRestriction: options.locationRestriction ? { ...options.locationRestriction } : undefined, } } /** Returns a fresh session token (v4-style UUID) to reuse across one search. */ const createSessionToken = (): string => { return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, c => { const r = (Math.random() * 16) | 0 const v = c === "x" ? r : (r & 0x3) | 0x8 return v.toString(16) }) } let autocompleteWithDetailsWarned = false const places = { autocomplete: (query: string, options?: AutocompleteOptions) => native.autocomplete(query, toNative(options)), getPlace: (placeId: string, options?: GetPlaceOptions) => native.getPlace(placeId, options), /** * @deprecated `autocompleteWithDetails` is deprecated and will be removed in a * future version. Fetch details only for the place the user selects: call * `autocomplete` and then `getPlace` on the chosen prediction. Fetching * details for every prediction is expensive and cannot share a single billing * session (Google allows one place-details request per session). */ autocompleteWithDetails: (query: string, options?: AutocompleteOptions) => { if (__DEV__ && !autocompleteWithDetailsWarned) { autocompleteWithDetailsWarned = true console.warn("[react-native-google-places] autocompleteWithDetails is deprecated and will be removed in newer versions") } return native.autocompleteWithDetails(query, toNative(options)) }, createSessionToken, } export { CircularLocationBounds, RectangularLocationBounds } export type { AddressComponent, PlaceAutocompleteResult, PlaceDetails } from "./specs/Places.nitro" export default places