{"version":3,"sources":["../../src/use-geolocation/index.ts"],"sourcesContent":["import { useEffect, useRef } from 'react'\nimport { usePausable } from '../use-pausable'\nimport { useSetState } from '../use-set-state'\nimport { useStableFn } from '../use-stable-fn'\nimport { useSupported } from '../use-supported'\n\nimport type { Pausable } from '../use-pausable'\n\nexport interface UseGeolocationOptions extends PositionOptions {\n  /**\n   * Whether to start watching the geolocation immediately.\n   *\n   * @defaultValue true\n   */\n  immediate?: boolean\n}\n\nexport interface UseGeolocationReturns extends Pausable {\n  /**\n   * Whether the geolocation API is supported.\n   */\n  isSupported: boolean\n  /**\n   * The current latitude.\n   */\n  latitude: number | null\n  /**\n   * The current longitude.\n   */\n  longitude: number | null\n  /**\n   * The current geolocation state.\n   */\n  isLocating: boolean\n  /**\n   * The timestamp when the geolocation was last updated.\n   */\n  locatedAt: number | null\n  /**\n   * The error object if any.\n   */\n  error: GeolocationPositionError | null\n  /**\n   * The current geolocation coordinates.\n   */\n  coords: Omit<GeolocationPosition['coords'], 'latitude' | 'longitude'> & {\n    latitude: number | null\n    longitude: number | null\n  }\n}\n\n/**\n * A React Hook that provides a simple way to get the user's [geolocation](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation).\n */\nexport function useGeolocation(options: UseGeolocationOptions = {}): UseGeolocationReturns {\n  const { enableHighAccuracy = true, maximumAge = 30000, timeout = 27000, immediate = true } = options\n\n  const pausable = usePausable(\n    false,\n    (ref) => {\n      ref.current = false\n      if (watcherRef.current) {\n        setState({ isLocating: false })\n        navigator.geolocation.clearWatch(watcherRef.current)\n        watcherRef.current = null\n      }\n    },\n    (ref) => {\n      ref.current = false\n      setState({ isLocating: true })\n\n      watcherRef.current =\n        navigator?.geolocation.watchPosition(\n          (e) => updatePosition(e),\n          (error) => setState({ isLocating: false, error }),\n          {\n            enableHighAccuracy,\n            maximumAge,\n            timeout,\n          },\n        ) || null\n\n      if (watcherRef.current) ref.current = true\n    },\n  )\n\n  const isSupported = useSupported(() => 'geolocation' in navigator)\n\n  const [state, setState] = useSetState({\n    isLocating: false,\n    locatedAt: null as number | null,\n    error: null as GeolocationPositionError | null,\n    coords: {\n      accuracy: 0,\n      latitude: null,\n      longitude: null,\n      altitude: null,\n      altitudeAccuracy: null,\n      heading: null,\n      speed: null,\n    } as Omit<GeolocationPosition['coords'], 'latitude' | 'longitude'> & {\n      latitude: number | null\n      longitude: number | null\n    },\n  })\n\n  const watcherRef = useRef<number | null>(null)\n\n  const updatePosition = useStableFn((position: GeolocationPosition) => {\n    setState({\n      isLocating: false,\n      locatedAt: position.timestamp,\n      error: null,\n      coords: position.coords,\n    })\n  })\n\n  useEffect(() => {\n    if (isSupported) {\n      immediate && pausable.resume()\n      return pausable.pause\n    }\n  }, [isSupported, immediate])\n\n  return {\n    ...state,\n    ...pausable,\n    isSupported,\n    latitude: state.coords.latitude,\n    longitude: state.coords.longitude,\n  }\n}\n"],"names":["useGeolocation","options","enableHighAccuracy","maximumAge","timeout","immediate","pausable","usePausable","ref","current","watcherRef","setState","isLocating","navigator","geolocation","clearWatch","watchPosition","e","updatePosition","error","isSupported","useSupported","useSetState","locatedAt","coords","accuracy","latitude","longitude","altitude","altitudeAccuracy","heading","speed","state","useRef","useStableFn","position","timestamp","useEffect","resume","pause"],"mappings":";;;;+BAsDgBA;;;eAAAA;;;qBAtDkB;2BACN;2BACA;2BACA;4BACC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkDtB,SAASA;QAAeC,UAAAA,iEAAiC,CAAC;IAC/D,kCAA6FA,QAArFC,oBAAAA,8DAAqB,0DAAgED,QAA1DE,YAAAA,8CAAa,gDAA6CF,QAAtCG,SAAAA,wCAAU,+CAA4BH,QAArBI,WAAAA,4CAAY;IAEpF,IAAMC,WAAWC,IAAAA,wBAAW,EAC1B,OACA,SAACC;QACCA,IAAIC,OAAO,GAAG;QACd,IAAIC,WAAWD,OAAO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