import { useInfiniteQuery as I } from "@tanstack/react-query";
import { G as x } from "../config-C5f_rI5O.js";
import { feeServiceApi as y } from "../network.js";
import { getTransactionHistoryQueryKey as S } from "../utils/transactions/getTransactionHistoryQueryKey.js";
import { getTransactionHashes as c } from "../utils/transactions/getTransactionHashes.js";
import { getOptimisticTransactions as g, removeOptimisticTransaction as G } from "../utils/transactions/addOptimisticTransaction.js";
const w = 7, _ = 5e3, p = [
  "PENDING",
  "PROCESSING",
  "WAITING_FOR_FUNDS"
], v = () => {
  const {
    apiKey: s,
    connectedWallets: { default: e }
  } = x(), f = S(e), {
    data: m,
    isLoading: d,
    isError: P,
    refetch: l,
    hasNextPage: h,
    fetchNextPage: u,
    isFetchingNextPage: T,
    isFetchNextPageError: N
  } = I({
    queryKey: f,
    queryFn: ({ pageParam: t }) => {
      if (!s)
        throw new Error("An API key is required to fetch transactions");
      return y.get(
        `/api/transactions/${s}`,
        {
          params: {
            page: t,
            perPage: w,
            walletAddress: e
          }
        }
      );
    },
    enabled: !!s && !!e,
    getNextPageParam: (t) => t.data.nextPage,
    initialPageParam: 1,
    refetchInterval: (t) => (t.state.data?.pages.flatMap((n) => n.data.data) ?? []).some((n) => p.includes(n.status)) || !!e && g(e).length > 0 ? _ : !1
  }), r = m?.pages.flatMap((t) => t.data.data) ?? [], i = e ? g(e) : [], E = new Set(r.flatMap(c));
  i.forEach((t) => {
    c(t).filter((a) => E.has(a)).forEach(G);
  });
  const o = [
    ...i,
    ...r
  ].sort(
    (t, a) => new Date(a.createdAt).getTime() - new Date(t.createdAt).getTime()
  ), A = o.filter(
    (t) => p.includes(t.status)
  ).length;
  return {
    transactions: o,
    pendingTransactionsCount: A,
    isLoading: d,
    isError: P,
    refetch: l,
    hasNextPage: h,
    fetchNextPage: u,
    isFetchingNextPage: T,
    isFetchNextPageError: N
  };
};
export {
  v as useTransactions
};
//# sourceMappingURL=useTransactions.js.map
