"use client";

import { useLoginModal } from "@/app/LoginModalContext";
import { useAuthContext } from "@/app/AuthContext";

export function useAuthAction() {
  const { openLoginModal } = useLoginModal();
  const { didPromise } = useAuthContext();

  return <Args extends unknown[], R>(action: (...args: Args) => Promise<R>) =>
    async (...args: Args): Promise<R | undefined> => {
      const did = await didPromise;
      if (did) {
        return action(...args);
      } else {
        openLoginModal();
        return undefined;
      }
    };
}
