{"version":3,"sources":["../../../../src/sensors/useGeolocation/core.ts"],"sourcesContent":["import { batch, observable } from \"@legendapp/state\";\nimport {\n  createSupported,\n  onMount,\n  peek,\n  type DeepMaybeObservable,\n  type Pausable,\n  type ReadonlyObservable,\n  type Supportable,\n} from \"@usels/core\";\nimport { defaultNavigator } from \"@shared/configurable\";\n\nexport interface UseGeolocationOptions {\n  /** Use high accuracy mode */\n  enableHighAccuracy?: boolean;\n  /** Maximum age of a cached position in ms */\n  maximumAge?: number;\n  /** Timeout for position request in ms */\n  timeout?: number;\n  /** Start watching immediately on mount (default: true) */\n  immediate?: boolean;\n}\n\nexport interface UseGeolocationCoords {\n  readonly accuracy: number;\n  readonly latitude: number;\n  readonly longitude: number;\n  readonly altitude: number | null;\n  readonly altitudeAccuracy: number | null;\n  readonly heading: number | null;\n  readonly speed: number | null;\n}\n\nexport interface UseGeolocationReturn extends Supportable, Pausable {\n  /** Current position coordinates */\n  coords$: ReadonlyObservable<UseGeolocationCoords>;\n  /** Timestamp of last successful position update */\n  locatedAt$: ReadonlyObservable<number | null>;\n  /** Last geolocation error */\n  error$: ReadonlyObservable<GeolocationPositionError | null>;\n  // isActive$, pause, resume → Pausable\n}\n\n/**\n * Framework-agnostic reactive wrapper around\n * [Geolocation.watchPosition()](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition).\n *\n * Exposes current coordinates, last update timestamp, and any error as\n * observables, plus `pause()` / `resume()` controls for the underlying\n * watcher. When `immediate` is true (default), the watcher is started in\n * `onMount`.\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function createGeolocation(\n  options?: DeepMaybeObservable<UseGeolocationOptions>\n): UseGeolocationReturn {\n  const opts$ = observable(options);\n\n  const isSupported$ = createSupported(\n    () => !!defaultNavigator && \"geolocation\" in defaultNavigator\n  );\n\n  const coords$ = observable<UseGeolocationCoords>({\n    accuracy: 0,\n    latitude: Infinity,\n    longitude: Infinity,\n    altitude: null,\n    altitudeAccuracy: null,\n    heading: null,\n    speed: null,\n  });\n\n  const locatedAt$ = observable<number | null>(null);\n  const error$ = observable<GeolocationPositionError | null>(null);\n  const isActive$ = observable(false);\n\n  let watcherId: number | undefined;\n\n  const pause = () => {\n    if (watcherId !== undefined && defaultNavigator?.geolocation) {\n      defaultNavigator.geolocation.clearWatch(watcherId);\n      watcherId = undefined;\n    }\n    isActive$.set(false);\n  };\n\n  const resume = () => {\n    if (!isSupported$.peek() || !defaultNavigator) return;\n    pause();\n    const raw = opts$.peek();\n    watcherId = defaultNavigator.geolocation.watchPosition(\n      (position) => {\n        batch(() => {\n          locatedAt$.set(position.timestamp);\n          coords$.set({\n            accuracy: position.coords.accuracy,\n            latitude: position.coords.latitude,\n            longitude: position.coords.longitude,\n            altitude: position.coords.altitude,\n            altitudeAccuracy: position.coords.altitudeAccuracy,\n            heading: position.coords.heading,\n            speed: position.coords.speed,\n          });\n          error$.set(null);\n        });\n      },\n      (err) => {\n        error$.set(err);\n      },\n      {\n        enableHighAccuracy: (peek(raw?.enableHighAccuracy) as boolean | undefined) ?? true,\n        maximumAge: (peek(raw?.maximumAge) as number | undefined) ?? 30000,\n        timeout: (peek(raw?.timeout) as number | undefined) ?? 27000,\n      }\n    );\n    isActive$.set(true);\n  };\n\n  onMount(() => {\n    const immediate = (peek(opts$.peek()?.immediate) as boolean | undefined) ?? true;\n    if (immediate) {\n      resume();\n    }\n    return () => {\n      pause();\n    };\n  });\n\n  return {\n    isSupported$,\n    coords$: coords$ as ReadonlyObservable<UseGeolocationCoords>,\n    locatedAt$: locatedAt$ as ReadonlyObservable<number | null>,\n    error$: error$ as ReadonlyObservable<GeolocationPositionError | null>,\n    isActive$: isActive$ as ReadonlyObservable<boolean>,\n    resume,\n    pause,\n  };\n}\n"],"mappings":"AAAA,SAAS,OAAO,kBAAkB;AAClC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAKK;AACP,SAAS,wBAAwB;AAAA;AA2C1B,SAAS,kBACd,SACsB;AACtB,QAAM,QAAQ,WAA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