"use client";
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { useAuth } from "./useAuth.js";
function useUserProfile() {
  const { user: authUser, isLoading: authLoading, signOut } = useAuth();
  const { data: profile, isLoading: profileLoading, error } = useQuery({
    queryKey: ["user-profile"],
    queryFn: async () => {
      const response = await fetch("/api/user/profile");
      if (!response.ok) {
        throw new Error("Failed to fetch profile");
      }
      return response.json();
    },
    enabled: !!authUser,
    staleTime: 1e3 * 60 * 5,
    // Cache for 5 minutes
    gcTime: 1e3 * 60 * 60
    // Keep in cache for 1 hour
  });
  const queryClient = useQueryClient();
  const isLoading = authLoading || profileLoading;
  const user = profile || authUser;
  const hasPassword = (profile == null ? void 0 : profile.authMethod) === "Email";
  const updateProfileMutation = useMutation({
    mutationFn: async (data) => {
      const response = await fetch("/api/user/profile", {
        method: "PATCH",
        headers: {
          "Content-Type": "application/json"
        },
        body: JSON.stringify(data)
      });
      if (!response.ok) {
        throw new Error("Failed to update profile");
      }
      return response.json();
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["user-profile"] });
    }
  });
  const updateProfile = updateProfileMutation.mutateAsync;
  return {
    user,
    profile,
    hasPassword,
    isLoading,
    error,
    signOut,
    updateProfile,
    updateProfileMutation,
    // Keep auth loading separate for cases where we only need basic auth check
    isAuthLoading: authLoading,
    isProfileLoading: profileLoading
  };
}
export {
  useUserProfile
};
