{"version":3,"file":"use-geolocation.cjs","names":[],"sources":["../../src/hooks/use-geolocation.ts"],"sourcesContent":["import { useEffect, useState } from \"react\";\n\nexport interface GeolocationState {\n    loading: boolean;\n    error: GeolocationPositionError | null;\n    coords: GeolocationCoordinates | null;\n    timestamp: number | null;\n}\n\nexport interface UseGeolocationOptions extends PositionOptions {\n    /** Use `watchPosition` instead of one-shot `getCurrentPosition`. Default: false. */\n    watch?: boolean;\n    /** Disable the hook without unmounting. Default: false. */\n    disabled?: boolean;\n}\n\n/**\n * React hook around the Geolocation API.\n *\n * The effect depends on the individual `PositionOptions` fields instead of the\n * rest object, which is a new reference on every render and would restart the\n * watch continuously. `exhaustive-deps` is silenced there because those fields\n * are the whole of what the effect reads from it.\n *\n * @param options - Geolocation options plus `watch` and `disabled` switches.\n * @returns The latest position, error and loading state.\n */\nexport function useGeolocation(options: UseGeolocationOptions = {}): GeolocationState {\n    const { watch = false, disabled = false, ...positionOptions } = options;\n    const [state, setState] = useState<GeolocationState>({\n        loading: !disabled,\n        error: null,\n        coords: null,\n        timestamp: null,\n    });\n\n    useEffect(() => {\n        if (disabled || typeof navigator === \"undefined\" || !navigator.geolocation) {\n            setState((prev) => ({ ...prev, loading: false }));\n            return;\n        }\n\n        const onSuccess = (position: GeolocationPosition): void => {\n            setState({\n                loading: false,\n                error: null,\n                coords: position.coords,\n                timestamp: position.timestamp,\n            });\n        };\n        const onError = (error: GeolocationPositionError): void => {\n            setState((prev) => ({ ...prev, loading: false, error }));\n        };\n\n        if (watch) {\n            const watchId = navigator.geolocation.watchPosition(\n                onSuccess,\n                onError,\n                positionOptions,\n            );\n            return () => navigator.geolocation.clearWatch(watchId);\n        }\n\n        navigator.geolocation.getCurrentPosition(onSuccess, onError, positionOptions);\n        return undefined;\n        // eslint-disable-next-line react-hooks/exhaustive-deps\n    }, [\n        disabled,\n        watch,\n        positionOptions.enableHighAccuracy,\n        positionOptions.maximumAge,\n        positionOptions.timeout,\n    ]);\n\n    return state;\n}\n"],"mappings":"uBA2BA,SAAgB,EAAe,EAAiC,CAAC,EAAqB,CAClF,GAAM,CAAE,QAAQ,GAAO,WAAW,GAAO,GAAG,GAAoB,EAC1D,CAAC,EAAO,IAAA,EAAY,EAAA,SAAA,CAA2B,CACjD,QAAS,CAAC,EACV,MAAO,KACP,OAAQ,KACR,UAAW,IACf,CAAC,EAwCD,OAtCA,EAAA,EAAA,UAAA,KAAgB,CACZ,GAAI,GAAY,OAAO,UAAc,KAAe,CAAC,UAAU,YAAa,CACxE,EAAU,IAAU,CAAE,GAAG,EAAM,QAAS,EAAM,EAAE,EAChD,MACJ,CAEA,IAAM,EAAa,GAAwC,CACvD,EAAS,CACL,QAAS,GACT,MAAO,KACP,OAAQ,EAAS,OACjB,UAAW,EAAS,SACxB,CAAC,CACL,EACM,EAAW,GAA0C,CACvD,EAAU,IAAU,CAAE,GAAG,EAAM,QAAS,GAAO,OAAM,EAAE,CAC3D,EAEA,GAAI,EAAO,CACP,IAAM,EAAU,UAAU,YAAY,cAClC,EACA,EACA,CACJ,EACA,UAAa,UAAU,YAAY,WAAW,CAAO,CACzD,CAEA,UAAU,YAAY,mBAAmB,EAAW,EAAS,CAAe,CAGhF,EAAG,CACC,EACA,EACA,EAAgB,mBAChB,EAAgB,WAChB,EAAgB,OACpB,CAAC,EAEM,CACX"}