"use client";

import { useEffect } from "react";
import { useRouter } from "next/navigation";

/**
 * Client component to handle logout state check
 * This checks sessionStorage and cookies for logout flags
 * and redirects if logout is in progress
 */
export function HomeLogoutCheck() {
  const router = useRouter();

  useEffect(() => {
    // Check if logout is in progress - if so, redirect immediately
    const justLoggedOut = sessionStorage.getItem('just_logged_out') === 'true';
    const logoutCookie = document.cookie.split(';').some(c => c.trim().startsWith('logout_in_progress=true'));
    
    if (justLoggedOut || logoutCookie) {
      // Clear the flags and redirect
      sessionStorage.removeItem('just_logged_out');
      document.cookie = 'logout_in_progress=; path=/; expires=Thu, 01 Jan 1970 00:00:00 UTC';
      router.push('/signin?logout=true');
    }
  }, [router]);

  return null;
}
