{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "use-location",
  "type": "registry:hook",
  "files": [
    {
      "path": "registry/default/hooks/use-location.ts",
      "content": "import { useState, useEffect } from \"react\";\n\ninterface Coordinates {\n  lat: number;\n  lon: number;\n}\n\ninterface LocationData {\n  coordinates: Coordinates | null;\n  city: string | null;\n  error: string | null;\n  isLoading: boolean;\n}\n\nexport const DEFAULT_LOCATION: Coordinates = {\n  lat: 19.076, // Mumbai\n  lon: 72.8777,\n};\n\nconst CACHE_KEY = \"wigggle-location-data\";\nconst CACHE_EXPIRY = 3600 * 1000;\n\ninterface CacheData {\n  coordinates: Coordinates;\n  city: string;\n  timestamp: number;\n}\n\nexport function useLocation(): LocationData {\n  const [coordinates, setCoordinates] = useState<Coordinates | null>(null);\n  const [city, setCity] = useState<string | null>(null);\n  const [error, setError] = useState<string | null>(null);\n  const [isLoading, setIsLoading] = useState<boolean>(true);\n\n  useEffect(() => {\n    const checkCacheAndFetch = async () => {\n      // 1. Check LocalStorage Cache\n      const cached = localStorage.getItem(CACHE_KEY);\n      if (cached) {\n        try {\n          const parsedCache: CacheData = JSON.parse(cached);\n          const now = Date.now();\n          if (now - parsedCache.timestamp < CACHE_EXPIRY) {\n            setCoordinates(parsedCache.coordinates);\n            setCity(parsedCache.city);\n            setIsLoading(false);\n            return;\n          }\n        } catch (e) {\n          console.error(\"Failed to parse location cache\", e);\n          localStorage.removeItem(CACHE_KEY);\n        }\n      }\n\n      // 2. Helper to save cache\n      const saveToCache = (coords: Coordinates, cityName: string) => {\n        const cacheData: CacheData = {\n          coordinates: coords,\n          city: cityName,\n          timestamp: Date.now(),\n        };\n        localStorage.setItem(CACHE_KEY, JSON.stringify(cacheData));\n      };\n\n      // 3. Try Browser Geolocation\n      if (!navigator.geolocation) {\n        fallbackToIP(\"Geolocation not supported\");\n        return;\n      }\n\n      navigator.geolocation.getCurrentPosition(\n        async (position) => {\n          const { latitude, longitude } = position.coords;\n          const coords = { lat: latitude, lon: longitude };\n\n          let cityName = \"Unknown Location\";\n          try {\n            const response = await fetch(\n              `https://api.bigdatacloud.net/data/reverse-geocode-client?latitude=${latitude}&longitude=${longitude}&localityLanguage=en`,\n            );\n            const data = await response.json();\n            cityName = data.city || data.locality || \"Unknown Location\";\n          } catch (err) {\n            console.error(\"Failed to fetch city name:\", err);\n          }\n\n          setCoordinates(coords);\n          setCity(cityName);\n          saveToCache(coords, cityName);\n          setIsLoading(false);\n        },\n        (err) => {\n          console.warn(\n            \"Geolocation failed, attempting IP fallback:\",\n            err.message,\n          );\n          fallbackToIP(err.message);\n        },\n      );\n\n      // 4. IP Fallback Strategy\n      async function fallbackToIP(initialError: string) {\n        try {\n          const response = await fetch(\"https://ipwho.is/\");\n          const data = await response.json();\n\n          if (data.success) {\n            const coords = { lat: data.latitude, lon: data.longitude };\n            const cityName = data.city || data.region || \"Unknown Location\";\n\n            setCoordinates(coords);\n            setCity(cityName);\n            setError(null);\n            saveToCache(coords, cityName);\n          } else {\n            throw new Error(data.message || \"IP Geolocation failed\");\n          }\n        } catch (ipErr) {\n          console.error(\"IP Geolocation failed, defaulting to Mumbai:\", ipErr);\n          setError(initialError);\n          setCoordinates(DEFAULT_LOCATION);\n          setCity(\"Mumbai\");\n        } finally {\n          setIsLoading(false);\n        }\n      }\n    };\n\n    checkCacheAndFetch();\n  }, []);\n\n  return { coordinates, city, error, isLoading };\n}\n",
      "type": "registry:hook"
    }
  ]
}