var F = Object.defineProperty;
var M = (t, e, r) => e in t ? F(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
var y = (t, e, r) => M(t, typeof e != "symbol" ? e + "" : e, r);
import { ac as R, Z as j, _ as k, a3 as U, $ as B } from "./address-CBux52Na.mjs";
class rt extends Error {
  constructor(r) {
    super(`Minimum transfer amount is ${R(r)}`);
    y(this, "min");
    this.min = r;
  }
  minAmount() {
    return this.min;
  }
}
class nt extends Error {
  constructor(r) {
    super("Unable to fetch a quote");
    y(this, "internalError");
    this.internalError = r;
  }
}
class it extends Error {
  constructor(e) {
    super(e);
  }
}
function D(t) {
  if (t.length >= 255)
    throw new TypeError("Alphabet too long");
  const e = new Uint8Array(256);
  for (let n = 0; n < e.length; n++)
    e[n] = 255;
  for (let n = 0; n < t.length; n++) {
    const o = t.charAt(n), f = o.charCodeAt(0);
    if (e[f] !== 255)
      throw new TypeError(o + " is ambiguous");
    e[f] = n;
  }
  const r = t.length, i = t.charAt(0), l = Math.log(r) / Math.log(256), u = Math.log(256) / Math.log(r);
  function m(n) {
    if (n instanceof Uint8Array || (ArrayBuffer.isView(n) ? n = new Uint8Array(n.buffer, n.byteOffset, n.byteLength) : Array.isArray(n) && (n = Uint8Array.from(n))), !(n instanceof Uint8Array))
      throw new TypeError("Expected Uint8Array");
    if (n.length === 0)
      return "";
    let o = 0, f = 0, s = 0;
    const h = n.length;
    for (; s !== h && n[s] === 0; )
      s++, o++;
    const d = (h - s) * u + 1 >>> 0, a = new Uint8Array(d);
    for (; s !== h; ) {
      let p = n[s], c = 0;
      for (let w = d - 1; (p !== 0 || c < f) && w !== -1; w--, c++)
        p += 256 * a[w] >>> 0, a[w] = p % r >>> 0, p = p / r >>> 0;
      if (p !== 0)
        throw new Error("Non-zero carry");
      f = c, s++;
    }
    let g = d - f;
    for (; g !== d && a[g] === 0; )
      g++;
    let I = i.repeat(o);
    for (; g < d; ++g)
      I += t.charAt(a[g]);
    return I;
  }
  function E(n) {
    if (typeof n != "string")
      throw new TypeError("Expected String");
    if (n.length === 0)
      return new Uint8Array();
    let o = 0, f = 0, s = 0;
    for (; n[o] === i; )
      f++, o++;
    const h = (n.length - o) * l + 1 >>> 0, d = new Uint8Array(h);
    for (; o < n.length; ) {
      const p = n.charCodeAt(o);
      if (p > 255)
        return;
      let c = e[p];
      if (c === 255)
        return;
      let w = 0;
      for (let T = h - 1; (c !== 0 || w < s) && T !== -1; T--, w++)
        c += r * d[T] >>> 0, d[T] = c % 256 >>> 0, c = c / 256 >>> 0;
      if (c !== 0)
        throw new Error("Non-zero carry");
      s = w, o++;
    }
    let a = h - s;
    for (; a !== h && d[a] === 0; )
      a++;
    const g = new Uint8Array(f + (h - a));
    let I = f;
    for (; a !== h; )
      g[I++] = d[a++];
    return g;
  }
  function S(n) {
    const o = E(n);
    if (o)
      return o;
    throw new Error("Non-base" + r + " character");
  }
  return {
    encode: m,
    decodeUnsafe: E,
    decode: S
  };
}
var P = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
const $ = D(P), V = (t) => $.encode(t), q = (t) => $.decode(t);
function ot(t) {
  return Uint8Array.from(atob(t), (e) => e.charCodeAt(0));
}
const z = 8192;
function G(t) {
  if (t.length < z)
    return btoa(String.fromCharCode(...t));
  let e = "";
  for (var r = 0; r < t.length; r += z) {
    const i = t.slice(r, r + z);
    e += String.fromCharCode(...i);
  }
  return btoa(e);
}
function st(t) {
  var l;
  const e = t.startsWith("0x") ? t.slice(2) : t, r = e.length % 2 === 0 ? e : `0${e}`, i = ((l = r.match(/[0-9a-fA-F]{2}/g)) == null ? void 0 : l.map((u) => parseInt(u, 16))) ?? [];
  if (i.length !== r.length / 2)
    throw new Error(`Invalid hex string ${t}`);
  return Uint8Array.from(i);
}
function H(t) {
  return t.reduce((e, r) => e + r.toString(16).padStart(2, "0"), "");
}
function at(t, e) {
  switch (e) {
    case "base58":
      return V(t);
    case "base64":
      return G(t);
    case "hex":
      return H(t);
    default:
      throw new Error("Unsupported encoding, supported values are: base64, hex");
  }
}
function W(t, e = ["<", ">"]) {
  const [r, i] = e, l = [];
  let u = "", m = 0;
  for (let E = 0; E < t.length; E++) {
    const S = t[E];
    if (S === r && m++, S === i && m--, m === 0 && S === ",") {
      l.push(u.trim()), u = "";
      continue;
    }
    u += S;
  }
  return l.push(u.trim()), l;
}
const X = 32;
function ct(t) {
  try {
    return q(t).length === X;
  } catch {
    return !1;
  }
}
const N = 32;
function _(t) {
  return Z(t) && J(t) === N;
}
function lt(t) {
  return _(t);
}
function K(t) {
  return t.includes("::") ? L(t) : t;
}
function L(t) {
  const [e, r] = t.split("::"), i = t.slice(e.length + r.length + 4), l = i.includes("<") ? i.slice(0, i.indexOf("<")) : i, u = i.includes("<") ? W(i.slice(i.indexOf("<") + 1, i.lastIndexOf(">"))).map(
    (m) => K(m.trim())
  ) : [];
  return {
    address: C(e),
    module: r,
    name: l,
    typeParams: u
  };
}
function C(t, e = !1) {
  let r = t.toLowerCase();
  return !e && r.startsWith("0x") && (r = r.slice(2)), `0x${r.padStart(N * 2, "0")}`;
}
function ut(t, e = !1) {
  return C(t, e);
}
function Z(t) {
  return /^(0x|0X)?[a-fA-F0-9]+$/.test(t) && t.length % 2 === 0;
}
function J(t) {
  return /^(0x|0X)/.test(t) ? (t.length - 2) / 2 : t.length / 2;
}
const x = "::", Q = "0x2::sui::SUI", O = (t) => /^(0x)?[0-9a-fA-F]+::\w+::\w+$/.test(t), Y = (t) => t.replace(/(0x)(0*)/g, "0x");
function v(t) {
  return t = t.startsWith("0x") ? t.slice(2) : t, t = t.length % 2 === 0 ? t : "0" + t, `0x${t.length === 64 ? t : U.encode(B.zpad(U.decode(t), 32))}`;
}
const ft = (t) => {
  const e = t.split(x);
  if (e.length !== 3)
    throw new Error(`Invalid Sui type: ${t}`);
  return [C(e[0]), e[1], e[2]].join(x);
}, ht = (t) => new b(t).getCoinType(), dt = (t) => new b(t).getPackageId(), gt = (t) => {
  const e = Y(t).match(/0x2::table::Table<(.*)>/);
  if (!e)
    throw new Error(`Invalid table type: ${t}`);
  if (e.length < 2)
    throw new Error(`Invalid table type: ${t}`);
  const [r] = e[1].split(",");
  if (!r || !O(r))
    throw new Error(`Invalid key type: ${r}`);
  return r;
}, A = class A {
  constructor(e) {
    // Full 32 bytes of Address
    y(this, "address");
    // Optional module and contract name
    y(this, "module");
    if (A.instanceof(e))
      this.address = e.address, this.module = e.module;
    else if (k.instanceof(e))
      this.address = e.toUint8Array();
    else if (typeof e == "string") {
      if (O(e)) {
        const r = e.split(x);
        this.module = r.slice(1).join(x), e = r[0];
      }
      if (e = v(e), !U.valid(e))
        throw new Error("Invalid Sui address: " + e);
      this.address = U.decode(e);
    } else
      this.address = e;
  }
  unwrap() {
    const e = this.getPackageId(), r = this.module ? x + this.module : "";
    return `${e}${r}`;
  }
  toString() {
    return this.unwrap();
  }
  toNative() {
    return this;
  }
  toUint8Array() {
    return this.address;
  }
  toUniversalAddress() {
    return new k(this.toUint8Array());
  }
  getPackageId() {
    return v(U.encode(this.address));
  }
  getCoinType() {
    if (this.module === "sui::SUI")
      return Q;
    if (!this.module)
      throw new Error("No module present in Sui token address");
    return this.unwrap();
  }
  static instanceof(e) {
    return e.constructor.platform === A.platform;
  }
  equals(e) {
    return A.instanceof(e) ? e.unwrap() === this.unwrap() : this.toUniversalAddress().equals(e);
  }
};
y(A, "byteSize", 32), y(A, "platform", "Sui");
let b = A;
j("Sui", b);
export {
  rt as M,
  it as R,
  b as S,
  nt as U,
  ct as a,
  _ as b,
  N as c,
  H as d,
  st as e,
  ot as f,
  at as g,
  q as h,
  O as i,
  V as j,
  ut as k,
  lt as l,
  ft as m,
  C as n,
  dt as o,
  Q as p,
  gt as q,
  Y as r,
  W as s,
  G as t,
  ht as u,
  L as v,
  x as w
};
