import React, { useEffect } from 'react'
import { ActivityIndicator, Linking, Platform, View } from 'react-native'
import { MyText, theme, tw } from 'common-ui'
import type { ReactParentComponent } from '@packages/shared'
import { trpc } from '../src/trpc-client'
import { useRegion } from '@/src/hooks/useRegion'
import LocationFallbackFlow from '@/src/components/location/LocationFallbackFlow'
const LoadingScreen: React.FC<{ label?: string }> = ({ label }) => (
{label ? {label} : null}
)
/**
* Region gate + location fallback.
*
* The browsing region is derived from the device location (see `useRegion`),
* anchored on the user's default address: when the location resolves to the
* default's region (or to no serviceable region) we use the default; only a
* different serviceable region makes us ask. While that decision is in flight we
* show a spinner; once a region exists the app renders, otherwise the user is
* asked to choose an address.
*/
const LocationTestWrapper: React.FC = ({ children }) => {
const {
regionId,
clearRegion,
hydrated,
isResolvingRegion,
locationStatus,
} = useRegion()
const { data: regions } = trpc.common.listRegions.useQuery()
// Tell the user *why* the location isn't usable before asking for an address.
const locationNotice = (() => {
switch (locationStatus) {
case 'denied':
return 'Location access is not granted. Allow location access for accurate delivery, or add a delivery address below.'
case 'services-off':
return 'Your device location is turned off. Turn it on, or add a delivery address below.'
case 'unavailable':
case 'error':
return 'We could not get your location. Please try again, or add a delivery address below.'
default:
return undefined
}
})()
// Deep-link to the OS location settings when the app can't fix it itself
// (location services off, or permission denied — iOS won't re-prompt).
const openLocationSettings = () => {
void (async () => {
try {
if (Platform.OS === 'android') {
await Linking.sendIntent('android.settings.LOCATION_SOURCE_SETTINGS')
} else {
await Linking.openSettings()
}
} catch {
try {
await Linking.openSettings()
} catch {
// Give up silently — the notice still explains the situation.
}
}
})()
}
// Validate the saved region against the live list. A stale/unknown id must
// lead to selection, never a silent checkout fallback. Membership-only: no
// setRegion here, so this effect can never re-trigger itself.
useEffect(() => {
if (!hydrated || !regions || regionId == null) return
if (!regions.some((region) => region.id === regionId)) {
clearRegion()
}
}, [hydrated, regions, regionId, clearRegion])
// A known region short-circuits every check below.
if (regionId != null) {
return <>{children}>
}
// Wait for the persisted region to load before deciding: location tracking
// must not start while a stored choice may still appear.
if (!hydrated) {
return
}
// Still deriving the region from device location / the default address.
if (isResolvingRegion) {
return
}
// No region resolved -> pick an address or add one.
return (
)
}
export default LocationTestWrapper