import { useCallback } from "react";
import { useExecuteReCaptcha } from "react-grecaptcha-v3";

export const useRecaptcha = () => {
  const executeRecaptcha = useExecuteReCaptcha();

  const verifyAction = useCallback(
    async (action: string) => {
      if (!executeRecaptcha) {
        console.error("reCAPTCHA is not available yet.");
        return null;
      }
      try {
        const token = await executeRecaptcha(action);
        return token;
      } catch (error) {
        console.error("reCAPTCHA execution failed:", error);
        return null;
      }
    },
    [executeRecaptcha]
  );

  return { verifyAction };
};
