import * as Location from "expo-location"; import { router } from "expo-router"; import { useMemo, useState } from "react"; import { Alert, Linking, ScrollView, View } from "react-native"; import { useTranslation } from "react-i18next"; import { Badge } from "@/components/ui/Badge"; import { Button } from "@/components/ui/Button"; import { Card } from "@/components/ui/Card"; import { AppHeader } from "@/components/ui/Header"; import { Screen } from "@/components/ui/Screen"; import { Text } from "@/components/ui/Text"; import { branches } from "@/lib/data/branches"; import { routes } from "@/lib/routes"; import { useLocationStore } from "@/lib/stores/locationStore"; import { colors, gutter } from "@/lib/theme"; function dist(a: { lat: number; lng: number }, b: { lat: number; lng: number }) { const toRad = (d: number) => (d * Math.PI) / 180; const R = 6371; const dLat = toRad(b.lat - a.lat); const dLng = toRad(b.lng - a.lng); const x = Math.sin(dLat / 2) ** 2 + Math.cos(toRad(a.lat)) * Math.cos(toRad(b.lat)) * Math.sin(dLng / 2) ** 2; return 2 * R * Math.asin(Math.sqrt(x)); } export default function BranchesScreen() { const { t } = useTranslation(); const setBranchId = useLocationStore((s) => s.setBranchId); const setFulfillment = useLocationStore((s) => s.setFulfillment); const setCoords = useLocationStore((s) => s.setCoords); const branchId = useLocationStore((s) => s.branchId); const [coords, setLocal] = useState<{ lat: number; lng: number } | null>(null); const sorted = useMemo(() => { if (!coords) return branches; return [...branches].sort((a, b) => dist(coords, a) - dist(coords, b)); }, [coords]); const useMyLocation = async () => { const { status } = await Location.requestForegroundPermissionsAsync(); if (status !== "granted") { Alert.alert("Location", "Permission denied"); return; } const pos = await Location.getCurrentPositionAsync({}); const c = { lat: pos.coords.latitude, lng: pos.coords.longitude }; setLocal(c); setCoords(c.lat, c.lng); }; return (