"use client";
import { useEffect, useState, useCallback } from "react";
const LAST_AUTH_METHOD_KEY = "last-auth-method";
function useLastAuthMethod() {
  const [lastMethod, setLastMethod] = useState(null);
  const [isReady, setIsReady] = useState(false);
  useEffect(() => {
    const timer = setTimeout(() => {
      try {
        const stored = localStorage.getItem(LAST_AUTH_METHOD_KEY);
        if (stored === "email" || stored === "google") {
          setLastMethod(stored);
        }
      } catch (error) {
        console.warn("Failed to read from localStorage:", error);
        setLastMethod(null);
      }
      setIsReady(true);
    }, 150);
    return () => clearTimeout(timer);
  }, []);
  const saveAuthMethod = useCallback((method) => {
    try {
      localStorage.setItem(LAST_AUTH_METHOD_KEY, method);
      setLastMethod(method);
    } catch (error) {
      console.warn("Failed to save to localStorage:", error);
      setLastMethod(method);
    }
  }, []);
  const clearAuthMethod = useCallback(() => {
    try {
      localStorage.removeItem(LAST_AUTH_METHOD_KEY);
    } catch (error) {
      console.warn("Failed to clear localStorage:", error);
    }
    setLastMethod(null);
  }, []);
  return {
    lastMethod,
    saveAuthMethod,
    clearAuthMethod,
    isReady
  };
}
export {
  useLastAuthMethod
};
