import { Navigate, Outlet } from 'react-router-dom';

function getTokenFromCookie(): string | undefined {
  const match = document.cookie.match(/(?:^|;\s*)auth-token=([^;]*)/);
  return match ? decodeURIComponent(match[1]) : undefined;
}

export function ProtectedRoute() {
  const token =
    localStorage.getItem('ema-backoffice-auth-token') ?? getTokenFromCookie();
  if (!token) return <Navigate to="/login" replace />;
  return <Outlet />;
}
