import { cf as f, a1 as u, V as g, cg as w, ch as P, aK as v, ci as y, aN as C } from "./platform-Cil_UtAQ.mjs";
import { W as T } from "./wallet-CMTEE6Oa.mjs";
class d extends f {
  /**
   *  The Signer being managed.
   */
  signer;
  #s;
  #e;
  /**
   *  Creates a new **NonceManager** to manage %%signer%%.
   */
  constructor(s) {
    super(s.provider), u(this, { signer: s }), this.#s = null, this.#e = 0;
  }
  async getAddress() {
    return this.signer.getAddress();
  }
  connect(s) {
    return new d(this.signer.connect(s));
  }
  async getNonce(s) {
    if (s === "pending") {
      this.#s == null && (this.#s = super.getNonce("pending"));
      const e = this.#e;
      return await this.#s + e;
    }
    return super.getNonce(s);
  }
  /**
   *  Manually increment the nonce. This may be useful when managng
   *  offline transactions.
   */
  increment() {
    this.#e++;
  }
  /**
   *  Resets the nonce, causing the **NonceManager** to reload the current
   *  nonce from the blockchain on the next transaction.
   */
  reset() {
    this.#e = 0, this.#s = null;
  }
  async sendTransaction(s) {
    const e = this.getNonce("pending");
    return this.increment(), s = await this.signer.populateTransaction(s), s.nonce = await e, await this.signer.sendTransaction(s);
  }
  signTransaction(s) {
    return this.signer.signTransaction(s);
  }
  signMessage(s) {
    return this.signer.signMessage(s);
  }
  signTypedData(s, e, t) {
    return this.signer.signTypedData(s, e, t);
  }
}
async function x(n, s, e) {
  const t = typeof s == "string" ? new T(s, n) : s, i = e?.chain ?? (await g.chainFromRpc(n))[1], r = new d(t);
  if (r.provider === null)
    try {
      r.connect(n);
    } catch (a) {
      console.error("Cannot connect to network for signer", a);
    }
  return new N(i, await t.getAddress(), r, e);
}
class N extends w {
  opts;
  constructor(s, e, t, i) {
    super(s, e, t), this.opts = i;
  }
  chain() {
    return this._chain;
  }
  address() {
    return this._address;
  }
  async sign(s) {
    const e = this.chain(), t = [];
    let i = 500000n, r = 100000000000n, a = 1500000000n, c = 100000000n;
    if (this.opts?.overrides === void 0 && e !== "Celo") {
      const o = await this._signer.provider.getFeeData();
      r = o.gasPrice ?? r, a = o.maxFeePerGas ?? a, c = o.maxPriorityFeePerGas ?? c;
    }
    this.opts?.gasLimit !== void 0 && (i = this.opts.gasLimit), this.opts?.maxGasLimit !== void 0 && (i = i > this.opts?.maxGasLimit ? this.opts?.maxGasLimit : i);
    const h = { gasLimit: i, maxFeePerGas: a, maxPriorityFeePerGas: c };
    for (const o of s) {
      const { transaction: m, description: p } = o;
      this.opts?.debug && console.log(`Signing: ${p} for ${this.address()}`);
      const l = {
        ...m,
        ...h,
        from: this.address(),
        nonce: await this._signer.getNonce(),
        // Override any existing values with those passed in the constructor
        ...this.opts?.overrides
      };
      t.push(await this._signer.signTransaction(l));
    }
    return t;
  }
}
const A = {
  Address: v,
  Platform: g,
  getSigner: x,
  protocols: {
    WormholeCore: () => import("./index-BKgliEPZ.mjs"),
    TokenBridge: () => import("./index-BCKX-m7h.mjs"),
    PorticoBridge: () => import("./index-Cl9Df47-.mjs"),
    CircleBridge: () => import("./index-_IZO0QRl.mjs"),
    TBTCBridge: () => import("./index-BuHdMHLz.mjs")
  },
  getChain: (n, s, e) => new P(s, new g(n, y(n, C, {
    [s]: e
  })))
};
export {
  A as default
};
