import { type Transaction, getTransactionsHistory } from "@/utils/requests/transactions";
import { LOG } from "@logger";
import { useQuery } from "@tanstack/react-query";
import { useMemo } from "react";

const log = LOG.extend("useTransactionsHistory");

const useTransactionsHistory = () => {
	log.verbose("useTransactionsHistory");

	const { data, isLoading, error, refetch } = useQuery({
		queryKey: ["transactionsHistory"],
		queryFn: getTransactionsHistory,
	});

	const transactionsHistory: Transaction[] = useMemo(() => data || [], [data]);

	return {
		transactionsHistory,
		isLoading,
		error,
		refetch,
	};
};

export default useTransactionsHistory;
