import { Loader2 } from "lucide-react";
import { useEffect, useState } from "react";

interface FullScreenLoaderProps {
  show: boolean;
  text?: string;
}

export default function FullScreenLoader({ show, text = "Carregando..." }: FullScreenLoaderProps) {
  const [visible, setVisible] = useState(show);

  useEffect(() => {
    if (show) setVisible(true);
    else {
      // adiciona um pequeno atraso para suavizar o fade-out
      const timeout = setTimeout(() => setVisible(false), 200);
      return () => clearTimeout(timeout);
    }
  }, [show]);

  if (!visible && !show) return null;

  return (
    <div
      className={`fixed inset-0 z-[9999] flex items-center justify-center bg-background/80 backdrop-blur-sm transition-opacity duration-200 ${
        show ? "opacity-100" : "opacity-0 pointer-events-none"
      }`}
    >
      <div className="flex flex-col items-center gap-3 p-6 rounded-2xl bg-card shadow-lg border border-border transition-all duration-200">
        <Loader2 className="h-10 w-10 animate-spin text-primary" />
        <p className="text-card-foreground text-sm font-medium">{text}</p>
      </div>
    </div>
  );
}
