"use client";
import { useQueryClient } from "@tanstack/react-query";
import { useCallback } from "react";
import { TEAMS_QUERY_KEY, fetchUserTeams } from "../contexts/TeamContext.js";
const USER_PROFILE_QUERY_KEY = ["user-profile"];
async function fetchUserProfile() {
  const response = await fetch("/api/user/profile");
  if (!response.ok) {
    throw new Error("Failed to fetch profile");
  }
  return response.json();
}
function usePrefetchSettings() {
  const queryClient = useQueryClient();
  const prefetchProfile = useCallback(() => {
    queryClient.prefetchQuery({
      queryKey: USER_PROFILE_QUERY_KEY,
      queryFn: fetchUserProfile,
      staleTime: 1e3 * 60 * 5
      // 5 minutes
    });
  }, [queryClient]);
  const prefetchTeams = useCallback(() => {
    queryClient.prefetchQuery({
      queryKey: TEAMS_QUERY_KEY,
      queryFn: fetchUserTeams,
      staleTime: 1e3 * 60 * 5
      // 5 minutes
    });
  }, [queryClient]);
  return {
    prefetchProfile,
    prefetchTeams
  };
}
function usePrefetchEntity(entitySlug) {
  const queryClient = useQueryClient();
  return useCallback(() => {
    queryClient.prefetchQuery({
      queryKey: ["entity", entitySlug, "list"],
      queryFn: async () => {
        const response = await fetch(`/api/v1/${entitySlug}?limit=20`);
        if (!response.ok) {
          throw new Error(`Failed to fetch ${entitySlug}`);
        }
        return response.json();
      },
      staleTime: 1e3 * 60 * 2
      // 2 minutes for entity lists
    });
  }, [queryClient, entitySlug]);
}
export {
  usePrefetchEntity,
  usePrefetchSettings
};
