import React, { useEffect } from 'react'
import { ActivityIndicator, 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`).
* While that is in flight we show a spinner; once a region exists the app
* renders. When the location is unusable — device location switched off,
* permission denied, or coordinates outside every region bound — the user is
* asked to choose a region manually.
*/
const LocationTestWrapper: React.FC = ({ children }) => {
const {
regionId,
clearRegion,
hydrated,
isResolvingRegion,
} = useRegion()
const { data: regions } = trpc.common.listRegions.useQuery()
// 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.
if (isResolvingRegion) {
return
}
// Location unavailable / denied / out of area -> pick an address or add one.
return (
)
}
export default LocationTestWrapper