import { useConfig } from "@hooks/useConfig";
import { useQuery } from "@tanstack/react-query";
import { bungeeApiFetcher } from "@utils/fetcher";
import type { RequestStatus } from "./useRequestStatus";

export const useOrderHistory = ({ userAddress }: { userAddress: string }) => {
	const { apiKey, baseUrl } = useConfig();
	const shouldFetch = !!userAddress;

	// biome-ignore format:
	const query = `${baseUrl}/bungee-request/history?sender=${userAddress}`

	const { data, isLoading, refetch } = useQuery({
		queryKey: ["bungee-order-history", apiKey, userAddress],
		queryFn: () => bungeeApiFetcher(query, apiKey),
		enabled: shouldFetch,
		refetchOnWindowFocus: false,
		retry: 2,
		staleTime: 10000,
	});

	return {
		data: data?.result?.data as RequestStatus[],
		isLoading,
		refetch,
	};
};
