"use client";
import { bb as re, bc as P } from "./index.package-DVXWP15X.js";
function m(i, { strict: e = !0 } = {}) {
  return !i || typeof i != "string" ? !1 : e ? /^0x[0-9a-fA-F]*$/.test(i) : i.startsWith("0x");
}
function z(i) {
  return m(i, { strict: !1 }) ? Math.ceil((i.length - 2) / 2) : i.length;
}
const K = "2.31.0";
let E = {
  getDocsUrl: ({ docsBaseUrl: i, docsPath: e = "", docsSlug: t }) => e ? `${i ?? "https://viem.sh"}${e}${t ? `#${t}` : ""}` : void 0,
  version: `viem@${K}`
};
class l extends Error {
  constructor(e, t = {}) {
    const s = t.cause instanceof l ? t.cause.details : t.cause?.message ? t.cause.message : t.details, n = t.cause instanceof l && t.cause.docsPath || t.docsPath, r = E.getDocsUrl?.({ ...t, docsPath: n }), o = [
      e || "An error occurred.",
      "",
      ...t.metaMessages ? [...t.metaMessages, ""] : [],
      ...r ? [`Docs: ${r}`] : [],
      ...s ? [`Details: ${s}`] : [],
      ...E.version ? [`Version: ${E.version}`] : []
    ].join(`
`);
    super(o, t.cause ? { cause: t.cause } : void 0), Object.defineProperty(this, "details", {
      enumerable: !0,
      configurable: !0,
      writable: !0,
      value: void 0
    }), Object.defineProperty(this, "docsPath", {
      enumerable: !0,
      configurable: !0,
      writable: !0,
      value: void 0
    }), Object.defineProperty(this, "metaMessages", {
      enumerable: !0,
      configurable: !0,
      writable: !0,
      value: void 0
    }), Object.defineProperty(this, "shortMessage", {
      enumerable: !0,
      configurable: !0,
      writable: !0,
      value: void 0
    }), Object.defineProperty(this, "version", {
      enumerable: !0,
      configurable: !0,
      writable: !0,
      value: void 0
    }), Object.defineProperty(this, "name", {
      enumerable: !0,
      configurable: !0,
      writable: !0,
      value: "BaseError"
    }), this.details = s, this.docsPath = n, this.metaMessages = t.metaMessages, this.name = t.name ?? this.name, this.shortMessage = e, this.version = K;
  }
  walk(e) {
    return N(this, e);
  }
}
function N(i, e) {
  return e?.(i) ? i : i && typeof i == "object" && "cause" in i && i.cause !== void 0 ? N(i.cause, e) : e ? null : i;
}
class W extends l {
  constructor({ size: e, targetSize: t, type: s }) {
    super(`${s.charAt(0).toUpperCase()}${s.slice(1).toLowerCase()} size (${e}) exceeds padding size (${t}).`, { name: "SizeExceedsPaddingSizeError" });
  }
}
function x(i, { dir: e, size: t = 32 } = {}) {
  return typeof i == "string" ? oe(i, { dir: e, size: t }) : ce(i, { dir: e, size: t });
}
function oe(i, { dir: e, size: t = 32 } = {}) {
  if (t === null)
    return i;
  const s = i.replace("0x", "");
  if (s.length > t * 2)
    throw new W({
      size: Math.ceil(s.length / 2),
      targetSize: t,
      type: "hex"
    });
  return `0x${s[e === "right" ? "padEnd" : "padStart"](t * 2, "0")}`;
}
function ce(i, { dir: e, size: t = 32 } = {}) {
  if (t === null)
    return i;
  if (i.length > t)
    throw new W({
      size: i.length,
      targetSize: t,
      type: "bytes"
    });
  const s = new Uint8Array(t);
  for (let n = 0; n < t; n++) {
    const r = e === "right";
    s[r ? n : t - n - 1] = i[r ? n : i.length - n - 1];
  }
  return s;
}
class he extends l {
  constructor({ max: e, min: t, signed: s, size: n, value: r }) {
    super(`Number "${r}" is not in safe ${n ? `${n * 8}-bit ${s ? "signed" : "unsigned"} ` : ""}integer range ${e ? `(${t} to ${e})` : `(above ${t})`}`, { name: "IntegerOutOfRangeError" });
  }
}
class ae extends l {
  constructor({ givenSize: e, maxSize: t }) {
    super(`Size cannot exceed ${t} bytes. Given size: ${e} bytes.`, { name: "SizeOverflowError" });
  }
}
function w(i, { size: e }) {
  if (z(i) > e)
    throw new ae({
      givenSize: z(i),
      maxSize: e
    });
}
function I(i, e = {}) {
  const { signed: t } = e;
  e.size && w(i, { size: e.size });
  const s = BigInt(i);
  if (!t)
    return s;
  const n = (i.length - 2) / 2, r = (1n << BigInt(n) * 8n - 1n) - 1n;
  return s <= r ? s : s - BigInt(`0x${"f".padStart(n * 2, "f")}`) - 1n;
}
function fe(i, e = {}) {
  return Number(I(i, e));
}
const de = /* @__PURE__ */ Array.from({ length: 256 }, (i, e) => e.toString(16).padStart(2, "0"));
function $(i, e = {}) {
  return typeof i == "number" || typeof i == "bigint" ? O(i, e) : typeof i == "string" ? xe(i, e) : typeof i == "boolean" ? ue(i, e) : V(i, e);
}
function ue(i, e = {}) {
  const t = `0x${Number(i)}`;
  return typeof e.size == "number" ? (w(t, { size: e.size }), x(t, { size: e.size })) : t;
}
function V(i, e = {}) {
  let t = "";
  for (let n = 0; n < i.length; n++)
    t += de[i[n]];
  const s = `0x${t}`;
  return typeof e.size == "number" ? (w(s, { size: e.size }), x(s, { dir: "right", size: e.size })) : s;
}
function O(i, e = {}) {
  const { signed: t, size: s } = e, n = BigInt(i);
  let r;
  s ? t ? r = (1n << BigInt(s) * 8n - 1n) - 1n : r = 2n ** (BigInt(s) * 8n) - 1n : typeof i == "number" && (r = BigInt(Number.MAX_SAFE_INTEGER));
  const o = typeof r == "bigint" && t ? -r - 1n : 0;
  if (r && n > r || n < o) {
    const h = typeof i == "bigint" ? "n" : "";
    throw new he({
      max: r ? `${r}${h}` : void 0,
      min: `${o}${h}`,
      signed: t,
      size: s,
      value: `${i}${h}`
    });
  }
  const c = `0x${(t && n < 0 ? (1n << BigInt(s * 8)) + BigInt(n) : n).toString(16)}`;
  return s ? x(c, { size: s }) : c;
}
const le = /* @__PURE__ */ new TextEncoder();
function xe(i, e = {}) {
  const t = le.encode(i);
  return V(t, e);
}
const we = /* @__PURE__ */ new TextEncoder();
function ge(i, e = {}) {
  return typeof i == "number" || typeof i == "bigint" ? pe(i, e) : typeof i == "boolean" ? ye(i, e) : m(i) ? X(i, e) : Y(i, e);
}
function ye(i, e = {}) {
  const t = new Uint8Array(1);
  return t[0] = Number(i), typeof e.size == "number" ? (w(t, { size: e.size }), x(t, { size: e.size })) : t;
}
const f = {
  zero: 48,
  nine: 57,
  A: 65,
  F: 70,
  a: 97,
  f: 102
};
function F(i) {
  if (i >= f.zero && i <= f.nine)
    return i - f.zero;
  if (i >= f.A && i <= f.F)
    return i - (f.A - 10);
  if (i >= f.a && i <= f.f)
    return i - (f.a - 10);
}
function X(i, e = {}) {
  let t = i;
  e.size && (w(t, { size: e.size }), t = x(t, { dir: "right", size: e.size }));
  let s = t.slice(2);
  s.length % 2 && (s = `0${s}`);
  const n = s.length / 2, r = new Uint8Array(n);
  for (let o = 0, c = 0; o < n; o++) {
    const h = F(s.charCodeAt(c++)), d = F(s.charCodeAt(c++));
    if (h === void 0 || d === void 0)
      throw new l(`Invalid byte sequence ("${s[c - 2]}${s[c - 1]}" in "${s}").`);
    r[o] = h * 16 + d;
  }
  return r;
}
function pe(i, e) {
  const t = O(i, e);
  return X(t);
}
function Y(i, e = {}) {
  const t = we.encode(i);
  return typeof e.size == "number" ? (w(t, { size: e.size }), x(t, { dir: "right", size: e.size })) : t;
}
function G(i, e) {
  const t = e || "hex", s = re(m(i, { strict: !1 }) ? ge(i) : i);
  return t === "bytes" ? s : $(s);
}
class me extends Map {
  constructor(e) {
    super(), Object.defineProperty(this, "maxSize", {
      enumerable: !0,
      configurable: !0,
      writable: !0,
      value: void 0
    }), this.maxSize = e;
  }
  get(e) {
    const t = super.get(e);
    return super.has(e) && t !== void 0 && (this.delete(e), super.set(e, t)), t;
  }
  set(e, t) {
    if (super.set(e, t), this.maxSize && this.size > this.maxSize) {
      const s = this.keys().next().value;
      s && this.delete(s);
    }
    return this;
  }
}
const b = /* @__PURE__ */ new me(8192);
function Ue(i, e) {
  if (b.has(`${i}.${e}`))
    return b.get(`${i}.${e}`);
  const t = i.substring(2).toLowerCase(), s = G(Y(t), "bytes"), n = t.split("");
  for (let o = 0; o < 40; o += 2)
    s[o >> 1] >> 4 >= 8 && n[o] && (n[o] = n[o].toUpperCase()), (s[o >> 1] & 15) >= 8 && n[o + 1] && (n[o + 1] = n[o + 1].toUpperCase());
  const r = `0x${n.join("")}`;
  return b.set(`${i}.${e}`, r), r;
}
function Se(i) {
  const e = G(`0x${i.substring(4)}`).substring(26);
  return Ue(`0x${e}`);
}
async function Be({ hash: i, signature: e }) {
  const t = m(i) ? i : $(i), { secp256k1: s } = await import("./index.package-DVXWP15X.js").then((o) => o.dj);
  return `0x${(() => {
    if (typeof e == "object" && "r" in e && "s" in e) {
      const { r: d, s: B, v: u, yParity: ie } = e, se = Number(ie ?? u), ne = H(se);
      return new s.Signature(I(d), I(B)).addRecoveryBit(ne);
    }
    const o = m(e) ? e : $(e);
    if (z(o) !== 65)
      throw new Error("invalid signature length");
    const c = fe(`0x${o.slice(130)}`), h = H(c);
    return s.Signature.fromCompact(o.substring(2, 130)).addRecoveryBit(h);
  })().recoverPublicKey(t.substring(2)).toHex(!1)}`;
}
function H(i) {
  if (i === 0 || i === 1)
    return i;
  if (i === 27)
    return 0;
  if (i === 28)
    return 1;
  throw new Error("Invalid yParityOrV value");
}
async function it({ hash: i, signature: e }) {
  return Se(await Be({ hash: i, signature: e }));
}
function Ee(i) {
  const e = i.length;
  let t = 0, s = 0;
  for (; s < e; ) {
    let n = i.charCodeAt(s++);
    if ((n & 4294967168) === 0) {
      t++;
      continue;
    } else if ((n & 4294965248) === 0)
      t += 2;
    else {
      if (n >= 55296 && n <= 56319 && s < e) {
        const r = i.charCodeAt(s);
        (r & 64512) === 56320 && (++s, n = ((n & 1023) << 10) + (r & 1023) + 65536);
      }
      (n & 4294901760) === 0 ? t += 3 : t += 4;
    }
  }
  return t;
}
function be(i, e, t) {
  const s = i.length;
  let n = t, r = 0;
  for (; r < s; ) {
    let o = i.charCodeAt(r++);
    if ((o & 4294967168) === 0) {
      e[n++] = o;
      continue;
    } else if ((o & 4294965248) === 0)
      e[n++] = o >> 6 & 31 | 192;
    else {
      if (o >= 55296 && o <= 56319 && r < s) {
        const c = i.charCodeAt(r);
        (c & 64512) === 56320 && (++r, o = ((o & 1023) << 10) + (c & 1023) + 65536);
      }
      (o & 4294901760) === 0 ? (e[n++] = o >> 12 & 15 | 224, e[n++] = o >> 6 & 63 | 128) : (e[n++] = o >> 18 & 7 | 240, e[n++] = o >> 12 & 63 | 128, e[n++] = o >> 6 & 63 | 128);
    }
    e[n++] = o & 63 | 128;
  }
}
const Te = new TextEncoder(), Ae = 50;
function ze(i, e, t) {
  Te.encodeInto(i, e.subarray(t));
}
function Ie(i, e, t) {
  i.length > Ae ? ze(i, e, t) : be(i, e, t);
}
const $e = 4096;
function q(i, e, t) {
  let s = e;
  const n = s + t, r = [];
  let o = "";
  for (; s < n; ) {
    const c = i[s++];
    if ((c & 128) === 0)
      r.push(c);
    else if ((c & 224) === 192) {
      const h = i[s++] & 63;
      r.push((c & 31) << 6 | h);
    } else if ((c & 240) === 224) {
      const h = i[s++] & 63, d = i[s++] & 63;
      r.push((c & 31) << 12 | h << 6 | d);
    } else if ((c & 248) === 240) {
      const h = i[s++] & 63, d = i[s++] & 63, B = i[s++] & 63;
      let u = (c & 7) << 18 | h << 12 | d << 6 | B;
      u > 65535 && (u -= 65536, r.push(u >>> 10 & 1023 | 55296), u = 56320 | u & 1023), r.push(u);
    } else
      r.push(c);
    r.length >= $e && (o += String.fromCharCode(...r), r.length = 0);
  }
  return r.length > 0 && (o += String.fromCharCode(...r)), o;
}
const ve = new TextDecoder(), ke = 200;
function Me(i, e, t) {
  const s = i.subarray(e, e + t);
  return ve.decode(s);
}
function De(i, e, t) {
  return t > ke ? Me(i, e, t) : q(i, e, t);
}
class U {
  constructor(e, t) {
    this.type = e, this.data = t;
  }
}
class a extends Error {
  constructor(e) {
    super(e);
    const t = Object.create(a.prototype);
    Object.setPrototypeOf(this, t), Object.defineProperty(this, "name", {
      configurable: !0,
      enumerable: !1,
      value: a.name
    });
  }
}
const g = 4294967295;
function Ce(i, e, t) {
  const s = t / 4294967296, n = t;
  i.setUint32(e, s), i.setUint32(e + 4, n);
}
function J(i, e, t) {
  const s = Math.floor(t / 4294967296), n = t;
  i.setUint32(e, s), i.setUint32(e + 4, n);
}
function Z(i, e) {
  const t = i.getInt32(e), s = i.getUint32(e + 4);
  return t * 4294967296 + s;
}
function Le(i, e) {
  const t = i.getUint32(e), s = i.getUint32(e + 4);
  return t * 4294967296 + s;
}
const Pe = -1, Fe = 4294967296 - 1, He = 17179869184 - 1;
function Re({ sec: i, nsec: e }) {
  if (i >= 0 && e >= 0 && i <= He)
    if (e === 0 && i <= Fe) {
      const t = new Uint8Array(4);
      return new DataView(t.buffer).setUint32(0, i), t;
    } else {
      const t = i / 4294967296, s = i & 4294967295, n = new Uint8Array(8), r = new DataView(n.buffer);
      return r.setUint32(0, e << 2 | t & 3), r.setUint32(4, s), n;
    }
  else {
    const t = new Uint8Array(12), s = new DataView(t.buffer);
    return s.setUint32(0, e), J(s, 4, i), t;
  }
}
function _e(i) {
  const e = i.getTime(), t = Math.floor(e / 1e3), s = (e - t * 1e3) * 1e6, n = Math.floor(s / 1e9);
  return {
    sec: t + n,
    nsec: s - n * 1e9
  };
}
function Ke(i) {
  if (i instanceof Date) {
    const e = _e(i);
    return Re(e);
  } else
    return null;
}
function Ne(i) {
  const e = new DataView(i.buffer, i.byteOffset, i.byteLength);
  switch (i.byteLength) {
    case 4:
      return { sec: e.getUint32(0), nsec: 0 };
    case 8: {
      const t = e.getUint32(0), s = e.getUint32(4), n = (t & 3) * 4294967296 + s, r = t >>> 2;
      return { sec: n, nsec: r };
    }
    case 12: {
      const t = Z(e, 4), s = e.getUint32(0);
      return { sec: t, nsec: s };
    }
    default:
      throw new a(`Unrecognized data size for timestamp (expected 4, 8, or 12): ${i.length}`);
  }
}
function We(i) {
  const e = Ne(i);
  return new Date(e.sec * 1e3 + e.nsec / 1e6);
}
const Ve = {
  type: Pe,
  encode: Ke,
  decode: We
};
class S {
  constructor() {
    this.builtInEncoders = [], this.builtInDecoders = [], this.encoders = [], this.decoders = [], this.register(Ve);
  }
  register({ type: e, encode: t, decode: s }) {
    if (e >= 0)
      this.encoders[e] = t, this.decoders[e] = s;
    else {
      const n = -1 - e;
      this.builtInEncoders[n] = t, this.builtInDecoders[n] = s;
    }
  }
  tryToEncode(e, t) {
    for (let s = 0; s < this.builtInEncoders.length; s++) {
      const n = this.builtInEncoders[s];
      if (n != null) {
        const r = n(e, t);
        if (r != null) {
          const o = -1 - s;
          return new U(o, r);
        }
      }
    }
    for (let s = 0; s < this.encoders.length; s++) {
      const n = this.encoders[s];
      if (n != null) {
        const r = n(e, t);
        if (r != null) {
          const o = s;
          return new U(o, r);
        }
      }
    }
    return e instanceof U ? e : null;
  }
  decode(e, t, s) {
    const n = t < 0 ? this.builtInDecoders[-1 - t] : this.decoders[t];
    return n ? n(e, t, s) : new U(t, e);
  }
}
S.defaultCodec = new S();
function Oe(i) {
  return i instanceof ArrayBuffer || typeof SharedArrayBuffer < "u" && i instanceof SharedArrayBuffer;
}
function v(i) {
  return i instanceof Uint8Array ? i : ArrayBuffer.isView(i) ? new Uint8Array(i.buffer, i.byteOffset, i.byteLength) : Oe(i) ? new Uint8Array(i) : Uint8Array.from(i);
}
const Xe = 100, Ye = 2048;
class M {
  constructor(e) {
    this.entered = !1, this.extensionCodec = e?.extensionCodec ?? S.defaultCodec, this.context = e?.context, this.useBigInt64 = e?.useBigInt64 ?? !1, this.maxDepth = e?.maxDepth ?? Xe, this.initialBufferSize = e?.initialBufferSize ?? Ye, this.sortKeys = e?.sortKeys ?? !1, this.forceFloat32 = e?.forceFloat32 ?? !1, this.ignoreUndefined = e?.ignoreUndefined ?? !1, this.forceIntegerToFloat = e?.forceIntegerToFloat ?? !1, this.pos = 0, this.view = new DataView(new ArrayBuffer(this.initialBufferSize)), this.bytes = new Uint8Array(this.view.buffer);
  }
  clone() {
    return new M({
      extensionCodec: this.extensionCodec,
      context: this.context,
      useBigInt64: this.useBigInt64,
      maxDepth: this.maxDepth,
      initialBufferSize: this.initialBufferSize,
      sortKeys: this.sortKeys,
      forceFloat32: this.forceFloat32,
      ignoreUndefined: this.ignoreUndefined,
      forceIntegerToFloat: this.forceIntegerToFloat
    });
  }
  reinitializeState() {
    this.pos = 0;
  }
  /**
   * This is almost equivalent to {@link Encoder#encode}, but it returns an reference of the encoder's internal buffer and thus much faster than {@link Encoder#encode}.
   *
   * @returns Encodes the object and returns a shared reference the encoder's internal buffer.
   */
  encodeSharedRef(e) {
    if (this.entered)
      return this.clone().encodeSharedRef(e);
    try {
      return this.entered = !0, this.reinitializeState(), this.doEncode(e, 1), this.bytes.subarray(0, this.pos);
    } finally {
      this.entered = !1;
    }
  }
  /**
   * @returns Encodes the object and returns a copy of the encoder's internal buffer.
   */
  encode(e) {
    if (this.entered)
      return this.clone().encode(e);
    try {
      return this.entered = !0, this.reinitializeState(), this.doEncode(e, 1), this.bytes.slice(0, this.pos);
    } finally {
      this.entered = !1;
    }
  }
  doEncode(e, t) {
    if (t > this.maxDepth)
      throw new Error(`Too deep objects in depth ${t}`);
    e == null ? this.encodeNil() : typeof e == "boolean" ? this.encodeBoolean(e) : typeof e == "number" ? this.forceIntegerToFloat ? this.encodeNumberAsFloat(e) : this.encodeNumber(e) : typeof e == "string" ? this.encodeString(e) : this.useBigInt64 && typeof e == "bigint" ? this.encodeBigInt64(e) : this.encodeObject(e, t);
  }
  ensureBufferSizeToWrite(e) {
    const t = this.pos + e;
    this.view.byteLength < t && this.resizeBuffer(t * 2);
  }
  resizeBuffer(e) {
    const t = new ArrayBuffer(e), s = new Uint8Array(t), n = new DataView(t);
    s.set(this.bytes), this.view = n, this.bytes = s;
  }
  encodeNil() {
    this.writeU8(192);
  }
  encodeBoolean(e) {
    e === !1 ? this.writeU8(194) : this.writeU8(195);
  }
  encodeNumber(e) {
    !this.forceIntegerToFloat && Number.isSafeInteger(e) ? e >= 0 ? e < 128 ? this.writeU8(e) : e < 256 ? (this.writeU8(204), this.writeU8(e)) : e < 65536 ? (this.writeU8(205), this.writeU16(e)) : e < 4294967296 ? (this.writeU8(206), this.writeU32(e)) : this.useBigInt64 ? this.encodeNumberAsFloat(e) : (this.writeU8(207), this.writeU64(e)) : e >= -32 ? this.writeU8(224 | e + 32) : e >= -128 ? (this.writeU8(208), this.writeI8(e)) : e >= -32768 ? (this.writeU8(209), this.writeI16(e)) : e >= -2147483648 ? (this.writeU8(210), this.writeI32(e)) : this.useBigInt64 ? this.encodeNumberAsFloat(e) : (this.writeU8(211), this.writeI64(e)) : this.encodeNumberAsFloat(e);
  }
  encodeNumberAsFloat(e) {
    this.forceFloat32 ? (this.writeU8(202), this.writeF32(e)) : (this.writeU8(203), this.writeF64(e));
  }
  encodeBigInt64(e) {
    e >= BigInt(0) ? (this.writeU8(207), this.writeBigUint64(e)) : (this.writeU8(211), this.writeBigInt64(e));
  }
  writeStringHeader(e) {
    if (e < 32)
      this.writeU8(160 + e);
    else if (e < 256)
      this.writeU8(217), this.writeU8(e);
    else if (e < 65536)
      this.writeU8(218), this.writeU16(e);
    else if (e < 4294967296)
      this.writeU8(219), this.writeU32(e);
    else
      throw new Error(`Too long string: ${e} bytes in UTF-8`);
  }
  encodeString(e) {
    const s = Ee(e);
    this.ensureBufferSizeToWrite(5 + s), this.writeStringHeader(s), Ie(e, this.bytes, this.pos), this.pos += s;
  }
  encodeObject(e, t) {
    const s = this.extensionCodec.tryToEncode(e, this.context);
    if (s != null)
      this.encodeExtension(s);
    else if (Array.isArray(e))
      this.encodeArray(e, t);
    else if (ArrayBuffer.isView(e))
      this.encodeBinary(e);
    else if (typeof e == "object")
      this.encodeMap(e, t);
    else
      throw new Error(`Unrecognized object: ${Object.prototype.toString.apply(e)}`);
  }
  encodeBinary(e) {
    const t = e.byteLength;
    if (t < 256)
      this.writeU8(196), this.writeU8(t);
    else if (t < 65536)
      this.writeU8(197), this.writeU16(t);
    else if (t < 4294967296)
      this.writeU8(198), this.writeU32(t);
    else
      throw new Error(`Too large binary: ${t}`);
    const s = v(e);
    this.writeU8a(s);
  }
  encodeArray(e, t) {
    const s = e.length;
    if (s < 16)
      this.writeU8(144 + s);
    else if (s < 65536)
      this.writeU8(220), this.writeU16(s);
    else if (s < 4294967296)
      this.writeU8(221), this.writeU32(s);
    else
      throw new Error(`Too large array: ${s}`);
    for (const n of e)
      this.doEncode(n, t + 1);
  }
  countWithoutUndefined(e, t) {
    let s = 0;
    for (const n of t)
      e[n] !== void 0 && s++;
    return s;
  }
  encodeMap(e, t) {
    const s = Object.keys(e);
    this.sortKeys && s.sort();
    const n = this.ignoreUndefined ? this.countWithoutUndefined(e, s) : s.length;
    if (n < 16)
      this.writeU8(128 + n);
    else if (n < 65536)
      this.writeU8(222), this.writeU16(n);
    else if (n < 4294967296)
      this.writeU8(223), this.writeU32(n);
    else
      throw new Error(`Too large map object: ${n}`);
    for (const r of s) {
      const o = e[r];
      this.ignoreUndefined && o === void 0 || (this.encodeString(r), this.doEncode(o, t + 1));
    }
  }
  encodeExtension(e) {
    if (typeof e.data == "function") {
      const s = e.data(this.pos + 6), n = s.length;
      if (n >= 4294967296)
        throw new Error(`Too large extension object: ${n}`);
      this.writeU8(201), this.writeU32(n), this.writeI8(e.type), this.writeU8a(s);
      return;
    }
    const t = e.data.length;
    if (t === 1)
      this.writeU8(212);
    else if (t === 2)
      this.writeU8(213);
    else if (t === 4)
      this.writeU8(214);
    else if (t === 8)
      this.writeU8(215);
    else if (t === 16)
      this.writeU8(216);
    else if (t < 256)
      this.writeU8(199), this.writeU8(t);
    else if (t < 65536)
      this.writeU8(200), this.writeU16(t);
    else if (t < 4294967296)
      this.writeU8(201), this.writeU32(t);
    else
      throw new Error(`Too large extension object: ${t}`);
    this.writeI8(e.type), this.writeU8a(e.data);
  }
  writeU8(e) {
    this.ensureBufferSizeToWrite(1), this.view.setUint8(this.pos, e), this.pos++;
  }
  writeU8a(e) {
    const t = e.length;
    this.ensureBufferSizeToWrite(t), this.bytes.set(e, this.pos), this.pos += t;
  }
  writeI8(e) {
    this.ensureBufferSizeToWrite(1), this.view.setInt8(this.pos, e), this.pos++;
  }
  writeU16(e) {
    this.ensureBufferSizeToWrite(2), this.view.setUint16(this.pos, e), this.pos += 2;
  }
  writeI16(e) {
    this.ensureBufferSizeToWrite(2), this.view.setInt16(this.pos, e), this.pos += 2;
  }
  writeU32(e) {
    this.ensureBufferSizeToWrite(4), this.view.setUint32(this.pos, e), this.pos += 4;
  }
  writeI32(e) {
    this.ensureBufferSizeToWrite(4), this.view.setInt32(this.pos, e), this.pos += 4;
  }
  writeF32(e) {
    this.ensureBufferSizeToWrite(4), this.view.setFloat32(this.pos, e), this.pos += 4;
  }
  writeF64(e) {
    this.ensureBufferSizeToWrite(8), this.view.setFloat64(this.pos, e), this.pos += 8;
  }
  writeU64(e) {
    this.ensureBufferSizeToWrite(8), Ce(this.view, this.pos, e), this.pos += 8;
  }
  writeI64(e) {
    this.ensureBufferSizeToWrite(8), J(this.view, this.pos, e), this.pos += 8;
  }
  writeBigUint64(e) {
    this.ensureBufferSizeToWrite(8), this.view.setBigUint64(this.pos, e), this.pos += 8;
  }
  writeBigInt64(e) {
    this.ensureBufferSizeToWrite(8), this.view.setBigInt64(this.pos, e), this.pos += 8;
  }
}
function st(i, e) {
  return new M(e).encodeSharedRef(i);
}
function T(i) {
  return `${i < 0 ? "-" : ""}0x${Math.abs(i).toString(16).padStart(2, "0")}`;
}
const Ge = 16, qe = 16;
class Je {
  constructor(e = Ge, t = qe) {
    this.hit = 0, this.miss = 0, this.maxKeyLength = e, this.maxLengthPerKey = t, this.caches = [];
    for (let s = 0; s < this.maxKeyLength; s++)
      this.caches.push([]);
  }
  canBeCached(e) {
    return e > 0 && e <= this.maxKeyLength;
  }
  find(e, t, s) {
    const n = this.caches[s - 1];
    e: for (const r of n) {
      const o = r.bytes;
      for (let c = 0; c < s; c++)
        if (o[c] !== e[t + c])
          continue e;
      return r.str;
    }
    return null;
  }
  store(e, t) {
    const s = this.caches[e.length - 1], n = { bytes: e, str: t };
    s.length >= this.maxLengthPerKey ? s[Math.random() * s.length | 0] = n : s.push(n);
  }
  decode(e, t, s) {
    const n = this.find(e, t, s);
    if (n != null)
      return this.hit++, n;
    this.miss++;
    const r = q(e, t, s), o = Uint8Array.prototype.slice.call(e, t, t + s);
    return this.store(o, r), r;
  }
}
const k = "array", p = "map_key", Q = "map_value", Ze = (i) => {
  if (typeof i == "string" || typeof i == "number")
    return i;
  throw new a("The type of key must be string or number but " + typeof i);
};
class Qe {
  constructor() {
    this.stack = [], this.stackHeadPosition = -1;
  }
  get length() {
    return this.stackHeadPosition + 1;
  }
  top() {
    return this.stack[this.stackHeadPosition];
  }
  pushArrayState(e) {
    const t = this.getUninitializedStateFromPool();
    t.type = k, t.position = 0, t.size = e, t.array = new Array(e);
  }
  pushMapState(e) {
    const t = this.getUninitializedStateFromPool();
    t.type = p, t.readCount = 0, t.size = e, t.map = {};
  }
  getUninitializedStateFromPool() {
    if (this.stackHeadPosition++, this.stackHeadPosition === this.stack.length) {
      const e = {
        type: void 0,
        size: 0,
        array: void 0,
        position: 0,
        readCount: 0,
        map: void 0,
        key: null
      };
      this.stack.push(e);
    }
    return this.stack[this.stackHeadPosition];
  }
  release(e) {
    if (this.stack[this.stackHeadPosition] !== e)
      throw new Error("Invalid stack state. Released state is not on top of the stack.");
    if (e.type === k) {
      const s = e;
      s.size = 0, s.array = void 0, s.position = 0, s.type = void 0;
    }
    if (e.type === p || e.type === Q) {
      const s = e;
      s.size = 0, s.map = void 0, s.readCount = 0, s.type = void 0;
    }
    this.stackHeadPosition--;
  }
  reset() {
    this.stack.length = 0, this.stackHeadPosition = -1;
  }
}
const y = -1, D = new DataView(new ArrayBuffer(0)), je = new Uint8Array(D.buffer);
try {
  D.getInt8(0);
} catch (i) {
  if (!(i instanceof RangeError))
    throw new Error("This module is not supported in the current JavaScript engine because DataView does not throw RangeError on out-of-bounds access");
}
const R = new RangeError("Insufficient data"), et = new Je();
class C {
  constructor(e) {
    this.totalPos = 0, this.pos = 0, this.view = D, this.bytes = je, this.headByte = y, this.stack = new Qe(), this.entered = !1, this.extensionCodec = e?.extensionCodec ?? S.defaultCodec, this.context = e?.context, this.useBigInt64 = e?.useBigInt64 ?? !1, this.rawStrings = e?.rawStrings ?? !1, this.maxStrLength = e?.maxStrLength ?? g, this.maxBinLength = e?.maxBinLength ?? g, this.maxArrayLength = e?.maxArrayLength ?? g, this.maxMapLength = e?.maxMapLength ?? g, this.maxExtLength = e?.maxExtLength ?? g, this.keyDecoder = e?.keyDecoder !== void 0 ? e.keyDecoder : et, this.mapKeyConverter = e?.mapKeyConverter ?? Ze;
  }
  clone() {
    return new C({
      extensionCodec: this.extensionCodec,
      context: this.context,
      useBigInt64: this.useBigInt64,
      rawStrings: this.rawStrings,
      maxStrLength: this.maxStrLength,
      maxBinLength: this.maxBinLength,
      maxArrayLength: this.maxArrayLength,
      maxMapLength: this.maxMapLength,
      maxExtLength: this.maxExtLength,
      keyDecoder: this.keyDecoder
    });
  }
  reinitializeState() {
    this.totalPos = 0, this.headByte = y, this.stack.reset();
  }
  setBuffer(e) {
    const t = v(e);
    this.bytes = t, this.view = new DataView(t.buffer, t.byteOffset, t.byteLength), this.pos = 0;
  }
  appendBuffer(e) {
    if (this.headByte === y && !this.hasRemaining(1))
      this.setBuffer(e);
    else {
      const t = this.bytes.subarray(this.pos), s = v(e), n = new Uint8Array(t.length + s.length);
      n.set(t), n.set(s, t.length), this.setBuffer(n);
    }
  }
  hasRemaining(e) {
    return this.view.byteLength - this.pos >= e;
  }
  createExtraByteError(e) {
    const { view: t, pos: s } = this;
    return new RangeError(`Extra ${t.byteLength - s} of ${t.byteLength} byte(s) found at buffer[${e}]`);
  }
  /**
   * @throws {@link DecodeError}
   * @throws {@link RangeError}
   */
  decode(e) {
    if (this.entered)
      return this.clone().decode(e);
    try {
      this.entered = !0, this.reinitializeState(), this.setBuffer(e);
      const t = this.doDecodeSync();
      if (this.hasRemaining(1))
        throw this.createExtraByteError(this.pos);
      return t;
    } finally {
      this.entered = !1;
    }
  }
  *decodeMulti(e) {
    if (this.entered) {
      yield* this.clone().decodeMulti(e);
      return;
    }
    try {
      for (this.entered = !0, this.reinitializeState(), this.setBuffer(e); this.hasRemaining(1); )
        yield this.doDecodeSync();
    } finally {
      this.entered = !1;
    }
  }
  async decodeAsync(e) {
    if (this.entered)
      return this.clone().decodeAsync(e);
    try {
      this.entered = !0;
      let t = !1, s;
      for await (const c of e) {
        if (t)
          throw this.entered = !1, this.createExtraByteError(this.totalPos);
        this.appendBuffer(c);
        try {
          s = this.doDecodeSync(), t = !0;
        } catch (h) {
          if (!(h instanceof RangeError))
            throw h;
        }
        this.totalPos += this.pos;
      }
      if (t) {
        if (this.hasRemaining(1))
          throw this.createExtraByteError(this.totalPos);
        return s;
      }
      const { headByte: n, pos: r, totalPos: o } = this;
      throw new RangeError(`Insufficient data in parsing ${T(n)} at ${o} (${r} in the current buffer)`);
    } finally {
      this.entered = !1;
    }
  }
  decodeArrayStream(e) {
    return this.decodeMultiAsync(e, !0);
  }
  decodeStream(e) {
    return this.decodeMultiAsync(e, !1);
  }
  async *decodeMultiAsync(e, t) {
    if (this.entered) {
      yield* this.clone().decodeMultiAsync(e, t);
      return;
    }
    try {
      this.entered = !0;
      let s = t, n = -1;
      for await (const r of e) {
        if (t && n === 0)
          throw this.createExtraByteError(this.totalPos);
        this.appendBuffer(r), s && (n = this.readArraySize(), s = !1, this.complete());
        try {
          for (; yield this.doDecodeSync(), --n !== 0; )
            ;
        } catch (o) {
          if (!(o instanceof RangeError))
            throw o;
        }
        this.totalPos += this.pos;
      }
    } finally {
      this.entered = !1;
    }
  }
  doDecodeSync() {
    e: for (; ; ) {
      const e = this.readHeadByte();
      let t;
      if (e >= 224)
        t = e - 256;
      else if (e < 192)
        if (e < 128)
          t = e;
        else if (e < 144) {
          const n = e - 128;
          if (n !== 0) {
            this.pushMapState(n), this.complete();
            continue e;
          } else
            t = {};
        } else if (e < 160) {
          const n = e - 144;
          if (n !== 0) {
            this.pushArrayState(n), this.complete();
            continue e;
          } else
            t = [];
        } else {
          const n = e - 160;
          t = this.decodeString(n, 0);
        }
      else if (e === 192)
        t = null;
      else if (e === 194)
        t = !1;
      else if (e === 195)
        t = !0;
      else if (e === 202)
        t = this.readF32();
      else if (e === 203)
        t = this.readF64();
      else if (e === 204)
        t = this.readU8();
      else if (e === 205)
        t = this.readU16();
      else if (e === 206)
        t = this.readU32();
      else if (e === 207)
        this.useBigInt64 ? t = this.readU64AsBigInt() : t = this.readU64();
      else if (e === 208)
        t = this.readI8();
      else if (e === 209)
        t = this.readI16();
      else if (e === 210)
        t = this.readI32();
      else if (e === 211)
        this.useBigInt64 ? t = this.readI64AsBigInt() : t = this.readI64();
      else if (e === 217) {
        const n = this.lookU8();
        t = this.decodeString(n, 1);
      } else if (e === 218) {
        const n = this.lookU16();
        t = this.decodeString(n, 2);
      } else if (e === 219) {
        const n = this.lookU32();
        t = this.decodeString(n, 4);
      } else if (e === 220) {
        const n = this.readU16();
        if (n !== 0) {
          this.pushArrayState(n), this.complete();
          continue e;
        } else
          t = [];
      } else if (e === 221) {
        const n = this.readU32();
        if (n !== 0) {
          this.pushArrayState(n), this.complete();
          continue e;
        } else
          t = [];
      } else if (e === 222) {
        const n = this.readU16();
        if (n !== 0) {
          this.pushMapState(n), this.complete();
          continue e;
        } else
          t = {};
      } else if (e === 223) {
        const n = this.readU32();
        if (n !== 0) {
          this.pushMapState(n), this.complete();
          continue e;
        } else
          t = {};
      } else if (e === 196) {
        const n = this.lookU8();
        t = this.decodeBinary(n, 1);
      } else if (e === 197) {
        const n = this.lookU16();
        t = this.decodeBinary(n, 2);
      } else if (e === 198) {
        const n = this.lookU32();
        t = this.decodeBinary(n, 4);
      } else if (e === 212)
        t = this.decodeExtension(1, 0);
      else if (e === 213)
        t = this.decodeExtension(2, 0);
      else if (e === 214)
        t = this.decodeExtension(4, 0);
      else if (e === 215)
        t = this.decodeExtension(8, 0);
      else if (e === 216)
        t = this.decodeExtension(16, 0);
      else if (e === 199) {
        const n = this.lookU8();
        t = this.decodeExtension(n, 1);
      } else if (e === 200) {
        const n = this.lookU16();
        t = this.decodeExtension(n, 2);
      } else if (e === 201) {
        const n = this.lookU32();
        t = this.decodeExtension(n, 4);
      } else
        throw new a(`Unrecognized type byte: ${T(e)}`);
      this.complete();
      const s = this.stack;
      for (; s.length > 0; ) {
        const n = s.top();
        if (n.type === k)
          if (n.array[n.position] = t, n.position++, n.position === n.size)
            t = n.array, s.release(n);
          else
            continue e;
        else if (n.type === p) {
          if (t === "__proto__")
            throw new a("The key __proto__ is not allowed");
          n.key = this.mapKeyConverter(t), n.type = Q;
          continue e;
        } else if (n.map[n.key] = t, n.readCount++, n.readCount === n.size)
          t = n.map, s.release(n);
        else {
          n.key = null, n.type = p;
          continue e;
        }
      }
      return t;
    }
  }
  readHeadByte() {
    return this.headByte === y && (this.headByte = this.readU8()), this.headByte;
  }
  complete() {
    this.headByte = y;
  }
  readArraySize() {
    const e = this.readHeadByte();
    switch (e) {
      case 220:
        return this.readU16();
      case 221:
        return this.readU32();
      default: {
        if (e < 160)
          return e - 144;
        throw new a(`Unrecognized array type byte: ${T(e)}`);
      }
    }
  }
  pushMapState(e) {
    if (e > this.maxMapLength)
      throw new a(`Max length exceeded: map length (${e}) > maxMapLengthLength (${this.maxMapLength})`);
    this.stack.pushMapState(e);
  }
  pushArrayState(e) {
    if (e > this.maxArrayLength)
      throw new a(`Max length exceeded: array length (${e}) > maxArrayLength (${this.maxArrayLength})`);
    this.stack.pushArrayState(e);
  }
  decodeString(e, t) {
    return !this.rawStrings || this.stateIsMapKey() ? this.decodeUtf8String(e, t) : this.decodeBinary(e, t);
  }
  /**
   * @throws {@link RangeError}
   */
  decodeUtf8String(e, t) {
    if (e > this.maxStrLength)
      throw new a(`Max length exceeded: UTF-8 byte length (${e}) > maxStrLength (${this.maxStrLength})`);
    if (this.bytes.byteLength < this.pos + t + e)
      throw R;
    const s = this.pos + t;
    let n;
    return this.stateIsMapKey() && this.keyDecoder?.canBeCached(e) ? n = this.keyDecoder.decode(this.bytes, s, e) : n = De(this.bytes, s, e), this.pos += t + e, n;
  }
  stateIsMapKey() {
    return this.stack.length > 0 ? this.stack.top().type === p : !1;
  }
  /**
   * @throws {@link RangeError}
   */
  decodeBinary(e, t) {
    if (e > this.maxBinLength)
      throw new a(`Max length exceeded: bin length (${e}) > maxBinLength (${this.maxBinLength})`);
    if (!this.hasRemaining(e + t))
      throw R;
    const s = this.pos + t, n = this.bytes.subarray(s, s + e);
    return this.pos += t + e, n;
  }
  decodeExtension(e, t) {
    if (e > this.maxExtLength)
      throw new a(`Max length exceeded: ext length (${e}) > maxExtLength (${this.maxExtLength})`);
    const s = this.view.getInt8(this.pos + t), n = this.decodeBinary(
      e,
      t + 1
      /* extType */
    );
    return this.extensionCodec.decode(n, s, this.context);
  }
  lookU8() {
    return this.view.getUint8(this.pos);
  }
  lookU16() {
    return this.view.getUint16(this.pos);
  }
  lookU32() {
    return this.view.getUint32(this.pos);
  }
  readU8() {
    const e = this.view.getUint8(this.pos);
    return this.pos++, e;
  }
  readI8() {
    const e = this.view.getInt8(this.pos);
    return this.pos++, e;
  }
  readU16() {
    const e = this.view.getUint16(this.pos);
    return this.pos += 2, e;
  }
  readI16() {
    const e = this.view.getInt16(this.pos);
    return this.pos += 2, e;
  }
  readU32() {
    const e = this.view.getUint32(this.pos);
    return this.pos += 4, e;
  }
  readI32() {
    const e = this.view.getInt32(this.pos);
    return this.pos += 4, e;
  }
  readU64() {
    const e = Le(this.view, this.pos);
    return this.pos += 8, e;
  }
  readI64() {
    const e = Z(this.view, this.pos);
    return this.pos += 8, e;
  }
  readU64AsBigInt() {
    const e = this.view.getBigUint64(this.pos);
    return this.pos += 8, e;
  }
  readI64AsBigInt() {
    const e = this.view.getBigInt64(this.pos);
    return this.pos += 8, e;
  }
  readF32() {
    const e = this.view.getFloat32(this.pos);
    return this.pos += 4, e;
  }
  readF64() {
    const e = this.view.getFloat64(this.pos);
    return this.pos += 8, e;
  }
}
function nt(i, e) {
  return new C(e).decode(i);
}
function L(i) {
  return globalThis.Buffer != null ? new Uint8Array(i.buffer, i.byteOffset, i.byteLength) : i;
}
function j(i = 0) {
  return globalThis.Buffer != null && globalThis.Buffer.allocUnsafe != null ? L(globalThis.Buffer.allocUnsafe(i)) : new Uint8Array(i);
}
function rt(i, e) {
  e || (e = i.reduce((n, r) => n + r.length, 0));
  const t = j(e);
  let s = 0;
  for (const n of i)
    t.set(n, s), s += n.length;
  return L(t);
}
function ee(i, e, t, s) {
  return {
    name: i,
    prefix: e,
    encoder: {
      name: i,
      prefix: e,
      encode: t
    },
    decoder: { decode: s }
  };
}
const _ = ee("utf8", "u", (i) => "u" + new TextDecoder("utf8").decode(i), (i) => new TextEncoder().encode(i.substring(1))), A = ee("ascii", "a", (i) => {
  let e = "a";
  for (let t = 0; t < i.length; t++)
    e += String.fromCharCode(i[t]);
  return e;
}, (i) => {
  i = i.substring(1);
  const e = j(i.length);
  for (let t = 0; t < i.length; t++)
    e[t] = i.charCodeAt(t);
  return e;
}), te = {
  utf8: _,
  "utf-8": _,
  hex: P.base16,
  latin1: A,
  ascii: A,
  binary: A,
  ...P
};
function ot(i, e = "utf8") {
  const t = te[e];
  if (!t)
    throw new Error(`Unsupported encoding "${e}"`);
  return (e === "utf8" || e === "utf-8") && globalThis.Buffer != null && globalThis.Buffer.from != null ? L(globalThis.Buffer.from(i, "utf-8")) : t.decoder.decode(`${t.prefix}${i}`);
}
function ct(i, e = "utf8") {
  const t = te[e];
  if (!t)
    throw new Error(`Unsupported encoding "${e}"`);
  return (e === "utf8" || e === "utf-8") && globalThis.Buffer != null && globalThis.Buffer.from != null ? globalThis.Buffer.from(i.buffer, i.byteOffset, i.byteLength).toString("utf8") : t.encoder.encode(i).substring(1);
}
export {
  rt as c,
  nt as d,
  st as e,
  ot as f,
  it as r,
  ct as t
};
