import { a as p } from "../../index-DEq3oXQF.js";
import s from "zod";
import { base64 as f } from "@scure/base";
import { nearClient as d } from "./nearClient.js";
function l(n, e) {
  const t = s.object({ result: s.array(s.number()) }).parse(n), o = new Uint8Array(t.result), a = new TextDecoder().decode(o);
  return e.parse(JSON.parse(a));
}
const g = async ({
  contractId: n,
  accountId: e
}) => {
  try {
    const t = { account_id: e }, o = p.from(JSON.stringify(t)).toString("base64"), r = await d.query({
      request_type: "call_function",
      method_name: "storage_balance_of",
      account_id: n,
      args_base64: o,
      finality: "optimistic"
    }), a = l(
      r,
      s.union([s.null(), s.object({ total: s.string() })])
    );
    return BigInt(a?.total ?? "0");
  } catch (t) {
    throw new Error("Error fetching balance", { cause: t });
  }
}, h = async ({
  contractId: n
}) => {
  const e = await d.query({
    request_type: "call_function",
    method_name: "storage_balance_bounds",
    account_id: n,
    args_base64: f.encode(new TextEncoder().encode(JSON.stringify({}))),
    finality: "optimistic"
  }), t = l(
    e,
    s.object({ min: s.string(), max: s.string() })
  );
  return BigInt(t.min);
}, m = `30${"0".repeat(12)}`, y = `50${"0".repeat(12)}`, _ = async ({
  wallet: n,
  sender: e,
  recipient: t,
  amount: o
}) => {
  const r = await n.signAndSendTransactions({
    transactions: [
      {
        signerId: e,
        receiverId: t,
        actions: [
          {
            type: "Transfer",
            params: {
              deposit: o
            }
          }
        ]
      }
    ]
  });
  return r?.[0] ? {
    hash: r[0].transaction?.hash ?? "",
    transactionLink: `https://nearblocks.io/txns/${r[0].transaction?.hash}`
  } : {
    hash: "",
    transactionLink: ""
  };
};
async function w(n, e, t, o, r) {
  if (e === "native-near") {
    if (!r || !n)
      throw new Error("No signed account found. Please connect your wallet.");
    return await _({
      amount: o,
      recipient: t,
      wallet: n,
      sender: r
    });
  }
  const a = [], [c, i] = await Promise.all(
    [
      h({
        contractId: e
      }),
      g({
        contractId: e,
        accountId: t
      })
    ]
  );
  (i === BigInt("0") || i < c) && a.push({
    type: "FunctionCall",
    params: {
      methodName: "storage_deposit",
      args: { account_id: t },
      gas: m,
      deposit: c.toString()
    }
  }), a.push({
    type: "FunctionCall",
    params: {
      methodName: "ft_transfer",
      args: {
        receiver_id: t,
        amount: o
      },
      gas: y,
      deposit: "1"
    }
  });
  const u = await n.signAndSendTransactions({
    transactions: [
      {
        receiverId: e,
        actions: a
      }
    ]
  });
  return u?.[0] ? {
    hash: u[0].transaction?.hash ?? "",
    transactionLink: `https://nearblocks.io/txns/${u[0].transaction?.hash}`
  } : null;
}
function E({
  provider: n,
  accountId: e
}) {
  async function t({
    address: r,
    amount: a,
    tokenAddress: c
  }) {
    if (!n)
      throw new Error("No NEAR wallet provider configured.");
    if (!c)
      throw new Error(
        "Token address is required for NEAR transfers. Or no intents account id."
      );
    const i = n();
    return await w(
      i,
      c,
      r,
      a,
      e
    );
  }
  return {
    make: async (r) => await t(r) ?? null
  };
}
export {
  E as useMakeNearTransfer
};
//# sourceMappingURL=index.js.map
