import { Navigate, Outlet, useLocation } from 'react-router-dom' import { RouteLoader } from '@meshkee/dashboard-ui' import { useAuth } from '../../context/AuthContext' import { CheckoutStepper, type CheckoutStepId } from '../../components/checkout/CheckoutStepper' import { loginPathWithRedirect, safeRedirectPath } from '../../lib/redirect' /** Cart and checkout login are public. Delivery, payment, and result require auth. */ const PUBLIC_STEPS = new Set(['cart', 'login']) function stepFromPath(pathname: string): CheckoutStepId { if (pathname.endsWith('/login')) return 'login' if (pathname.endsWith('/delivery')) return 'delivery' if (pathname.endsWith('/payment')) return 'payment' if ( pathname.endsWith('/success') || pathname.endsWith('/failed') || pathname.endsWith('/result') ) { return 'payment' } return 'cart' } export function CheckoutFlow() { const { user, isLoading } = useAuth() const location = useLocation() const step = stepFromPath(location.pathname) const isAuthenticated = Boolean(user) if (isLoading) { return } if (!isAuthenticated && !PUBLIC_STEPS.has(step)) { const returnTo = `${location.pathname}${location.search}` return ( ) } if (isAuthenticated && step === 'login') { const params = new URLSearchParams(location.search) const fromQuery = safeRedirectPath(params.get('redirect'), '') const fromState = (location.state as { from?: string } | null)?.from const destination = fromQuery || fromState || '/checkout/delivery' return } const hideStepper = location.pathname === '/checkout/success' || location.pathname === '/checkout/failed' || location.pathname === '/checkout/result' return ( <> {step !== 'login' && !hideStepper && ( )} ) }