import { useQuery } from "@tanstack/react-query";

import { useConfig } from "@hooks/useConfig";
import { bungeeApiFetcher } from "@utils/fetcher";

export const useSubmit = ({
	signature,
	requestData,
}: { signature: string; requestData: string }) => {
	const { apiKey, baseUrl } = useConfig();
	const shouldFetch = !!(signature && requestData);

	// biome-ignore format:
	const query = `${baseUrl}/bungee-auto/submit`

	const { data, isLoading, isError } = useQuery({
		queryKey: ["bungee-submit-api", apiKey, signature, requestData],
		queryFn: () => bungeeApiFetcher(query, apiKey, requestData, "POST"),
		enabled: shouldFetch,
		refetchOnWindowFocus: false,
		refetchOnMount: false,
		retry: 1,
	});

	return {
		data: data?.result as SubmitResponse,
		isLoading,
		isError,
	};
};

export type SubmitResponse = {
	requestHash: string;
	// currently im not using the submit data anywhere
};
