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