import { hexSchema as G, ensureArray as S, getTokenBalance as x, isEqualAddress as m, estimatedFeesToMaxFeeTotalV2 as H } from "@argent/x-shared";
import { useCallback as I } from "react";
const b = async ({ feeTokenAddress: t, calls: o, selectedAccount: r, transactionReviewService: i, appDomain: e, accountDeployTransaction: a, isDelayedTransaction: u }) => {
  const l = {
    type: "INVOKE",
    calls: S(o)
  };
  if (!r || !t)
    return;
  const s = a ? [a, l] : [l];
  return { result: await i.simulateAndReview(r, s, t, e, a, u) };
}, M = async (t, o, r, i) => {
  var g, y, F, w, T, p;
  let e = !1;
  const a = (g = o.transactions) == null ? void 0 : g.find((n) => {
    var A, B;
    return (B = (A = n == null ? void 0 : n.simulation) == null ? void 0 : A.summary) == null ? void 0 : B.find((R) => R.sent);
  });
  if (!a || !((y = a.simulation) != null && y.summary))
    return e;
  const u = await x(r, t, i) ?? 0n, l = a.simulation.summary.filter((n) => n.sent).map((n) => n.token.address), s = (F = o.enrichedFeeEstimation) == null ? void 0 : F.find((n) => m(n.transactions.feeTokenAddress, r)), c = (s == null ? void 0 : s.transactions.feeTokenAddress) && l.includes(s.transactions.feeTokenAddress), d = (s == null ? void 0 : s.type) === "native" && !(s != null && s.transactions.l1Gas.gasConsumed || s != null && s.transactions.l2Gas.gasConsumed || s != null && s.transactions.l1DataGas.gasConsumed), k = (s == null ? void 0 : s.type) === "paymaster" && !((w = s.transactions) != null && w.maxFee);
  if (!c || d && k)
    return e;
  const h = H(s), v = (T = a.simulation.summary.find((n) => n.sent && m(n.token.address, r))) == null ? void 0 : T.value, C = (p = a.simulation.summary.find((n) => !n.sent && m(n.token.address, r))) == null ? void 0 : p.value;
  if (!h || !v)
    return e;
  const f = BigInt(v) - BigInt(C || 0);
  if (!u)
    return e;
  const N = h + f;
  return u >= N || (e = !0), e;
}, K = () => I(async ({ feeTokenAddress: o, calls: r, selectedAccount: i, transactionReviewService: e, appDomain: a, accountDeployPayload: u, provider: l, isDelayedTransaction: s }) => {
  if (!i)
    return;
  const c = await b({
    feeTokenAddress: o,
    calls: r,
    selectedAccount: i,
    transactionReviewService: e,
    appDomain: a,
    accountDeployTransaction: O(u),
    isDelayedTransaction: s
  });
  if (!c || !c.result || !o)
    return;
  const d = await M(i.address, c.result, o, l);
  return { ...c.result, isSendingMoreThanBalanceAndGas: d };
}, []), O = (t) => t ? {
  type: "DEPLOY_ACCOUNT",
  calldata: t.calldata,
  classHash: G.parse(t.class_hash),
  salt: G.parse(t.salt),
  signature: []
} : void 0;
export {
  O as buildAccountDeployTransaction,
  M as checkGasFeeBalance,
  b as getReviewForTransactions,
  K as useTransactionReviewV2webFetcher
};
