import os

filepath = 'src/app/map/MapUI.tsx'
with open(filepath, 'r') as f:
    content = f.read()

# I will add a useEffect that calls handleLocateMe exactly once when the component mounts.
# We need to be careful where we insert it. Let's find the `useEffect` section.
# I will insert it right after `const [searchedLocation, setSearchedLocation] = useState<[number, number] | null>(null);`

auto_locate_effect = """
 useEffect(() => {
  if (navigator.geolocation && !searchedLocation) {
   navigator.geolocation.getCurrentPosition(
    (position) => {
     setSearchedLocation([position.coords.latitude, position.coords.longitude]);
    },
    (error) => {
     console.log("Auto-location failed or denied:", error);
    }
   );
  }
 }, []);
"""

content = content.replace(
    'const [searchedLocation, setSearchedLocation] = useState<[number, number] | null>(null);',
    'const [searchedLocation, setSearchedLocation] = useState<[number, number] | null>(null);\n' + auto_locate_effect
)

with open(filepath, 'w') as f:
    f.write(content)
print("Auto-locate added.")
