'use client';

import { useEffect, useState } from 'react';
import { isAuthenticated } from '@/lib/auth';

export function AuthGuard({ children }: { children: React.ReactNode }): React.JSX.Element {
  const [checked, setChecked] = useState(false);

  useEffect(() => {
    if (!isAuthenticated()) {
      window.location.href = '/login';
      return;
    }
    setChecked(true);
  }, []);

  if (!checked) {
    return (
      <div className="flex h-screen items-center justify-center">
        <div className="text-[#6B7280]">Загрузка...</div>
      </div>
    );
  }

  return <>{children}</>;
}
