import { useFetch as I } from "../../../../hooks/useFetch.js";
import { useState as h, useRef as l, useCallback as x, useMemo as D, useEffect as p } from "../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import { getTransactionsFilterQueryParams as L, compareTransactionsFilters as M } from "../components/utils.js";
import { createAbortable as O } from "../../../../primitives/async/abortable/main.js";
import { useConfigContext as P } from "../../../../core/ConfigContext/preact/context.js";
import { isFunction as S } from "../../../../utils/value/is.js";
const N = ({ applicableFilters: T, fetchEnabled: j, filters: t, getQueryParams: d, now: F }) => {
  const [b, g] = h(!1), [c, y] = h(() => performance.now()), [k, v] = h(), R = l(O()).current, i = l(t), { getTransactionTotals: f } = P().endpoints, a = S(f), n = a && j, m = n && k !== c, E = x(async () => {
    if (m) {
      const { signal: o } = R.refresh(!0);
      try {
        const u = d(L(t, F)), G = await f({ signal: o }, { query: u });
        if (!o.aborted) return G?.data;
      } catch (u) {
        if (!o.aborted) throw u;
      }
    }
  }, [R, t, d, f, F, m]), { data: s, error: A, isFetching: e } = I({
    fetchOptions: { enabled: m },
    queryFn: E
  }), C = l(e), r = !e && n, w = D(() => Array.isArray(s) ? s : [], [s]), q = x(() => {
    r && g(!0);
  }, [r]);
  return p(() => {
    if (i.current === t) return;
    M(t, i.current, T) && (y(performance.now()), i.current = t);
  }, [t, T]), p(() => {
    b && (g(!1), y(performance.now()));
  }, [b]), p(() => {
    C.current && !e && v(c), C.current = e;
  }, [e, c]), D(
    () => ({
      totals: w,
      error: A,
      canRefresh: r,
      refresh: q,
      isAvailable: a,
      isWaiting: e || a && !n && !s
    }),
    [n, a, r, s, A, e, q, w]
  );
};
export {
  N as default
};
