import S from "../../../../../hooks/useBalanceAccounts.js";
import { createDuplexTransactionNavigator as y } from "./transactionNavigator/createDuplexTransactionNavigator.js";
import { useRef as m, useState as l, useMemo as h, useCallback as C, useEffect as s } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import { useFetch as R } from "../../../../../hooks/useFetch.js";
import { useConfigContext as x } from "../../../../../core/ConfigContext/preact/context.js";
import { EMPTY_OBJECT as B } from "../../../../../utils/value/constants.js";
import { isFunction as E } from "../../../../../utils/value/is.js";
const v = (e) => {
  const { onNavigation: n, reset: a, ...r } = e;
  return r;
}, J = (e) => {
  const n = m(y()).current, [a, r] = l(e), [t, N] = l(), [b, A] = l(() => v(n)), { balanceAccounts: T } = S(), { getTransaction: i } = x().endpoints, {
    data: u,
    error: f,
    isFetching: o
  } = R(
    h(() => {
      const c = E(i) && !!a, g = { transactionId: a };
      return {
        fetchOptions: { enabled: c },
        queryFn: () => i(B, { path: g })
      };
    }, [i, a])
  ), p = m(o), d = m(a), F = h(() => {
    if (!t) return;
    const c = T?.find((g) => g.id === t.balanceAccountId);
    return { ...t, balanceAccount: c };
  }, [T, t]), I = C(() => r(void 0), []);
  return s(() => {
    switch (t?.id === e && t?.category) {
      case "Refund": {
        n.reset(t?.id, t?.refundMetadata?.originalPaymentId), n.onNavigation = ({ to: c }) => r(c);
        break;
      }
    }
    A(v(n));
  }, [e, t, n]), s(() => {
    p.current !== o && ((p.current = o) || (!u || f ? r(d.current) : (N(u), d.current = a)));
  }, [u, f, o, a]), s(() => {
    a || r(d.current);
  }, [t, a]), s(() => () => {
    n.onNavigation = null, n.reset();
  }, [n]), {
    error: f,
    fetchingTransaction: o,
    refreshTransaction: I,
    transaction: F,
    transactionNavigator: b
  };
};
export {
  J as default,
  J as useTransaction
};
