"use client";
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { TeamsAPI } from "../../lib/teams/teams.api.js";
import { teamsKeys } from "../../lib/teams/teams.keys.js";
function useUserTeams() {
  const queryClient = useQueryClient();
  const query = useQuery({
    queryKey: teamsKeys.user(),
    queryFn: TeamsAPI.getUserTeams,
    staleTime: 5 * 60 * 1e3
    // 5 minutes
  });
  const createMutation = useMutation({
    mutationFn: TeamsAPI.createTeam,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: teamsKeys.user() });
      queryClient.invalidateQueries({ queryKey: teamsKeys.admin() });
    }
  });
  return {
    /** User's teams */
    teams: query.data ?? [],
    /** Loading state */
    isLoading: query.isLoading,
    /** Error if any */
    error: query.error,
    /** Refetch teams */
    refetch: query.refetch,
    /** Create a new team */
    createTeam: createMutation.mutate,
    /** Create a new team (async) */
    createTeamAsync: createMutation.mutateAsync,
    /** Team creation in progress */
    isCreating: createMutation.isPending,
    /** Team creation error */
    createError: createMutation.error
  };
}
export {
  useUserTeams
};
