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