var j = Object.defineProperty;
var $ = (n) => {
  throw TypeError(n);
};
var q = (n, t, i) => t in n ? j(n, t, { enumerable: !0, configurable: !0, writable: !0, value: i }) : n[t] = i;
var A = (n, t, i) => q(n, typeof t != "symbol" ? t + "" : t, i), E = (n, t, i) => t.has(n) || $("Cannot " + i);
var s = (n, t, i) => (E(n, t, "read from private field"), i ? i.call(n) : t.get(n)), F = (n, t, i) => t.has(n) ? $("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(n) : t.set(n, i), y = (n, t, i, e) => (E(n, t, "write to private field"), e ? e.call(n, i) : t.set(n, i), i), u = (n, t, i) => (E(n, t, "access private method"), i);
import { bk as D, bf as G, bg as b, bj as w, b7 as H } from "./address-CBux52Na.mjs";
import { g as z, f as J, b as K, h as L, m as k } from "./contract-BVaUamai.mjs";
const Q = BigInt(-1), m = BigInt(0), I = BigInt(1), W = BigInt(5), x = {};
let N = "0000";
for (; N.length < 80; )
  N += N;
function v(n) {
  let t = N;
  for (; t.length < n; )
    t += t;
  return BigInt("1" + t.substring(0, n));
}
function B(n, t, i) {
  const e = BigInt(t.width);
  if (t.signed) {
    const r = I << e - I;
    w(i == null || n >= -r && n < r, "overflow", "NUMERIC_FAULT", {
      operation: i,
      fault: "overflow",
      value: n
    }), n > m ? n = L(k(n, e), e) : n = -L(k(-n, e), e);
  } else {
    const r = I << e;
    w(i == null || n >= 0 && n < r, "overflow", "NUMERIC_FAULT", {
      operation: i,
      fault: "overflow",
      value: n
    }), n = (n % r + r) % r & r - I;
  }
  return n;
}
function C(n) {
  typeof n == "number" && (n = `fixed128x${n}`);
  let t = !0, i = 128, e = 18;
  if (typeof n == "string") {
    if (n !== "fixed") if (n === "ufixed")
      t = !1;
    else {
      const a = n.match(/^(u?)fixed([0-9]+)x([0-9]+)$/);
      b(a, "invalid fixed format", "format", n), t = a[1] !== "u", i = parseInt(a[2]), e = parseInt(a[3]);
    }
  } else if (n) {
    const a = n, h = (d, S, Z) => a[d] == null ? Z : (b(typeof a[d] === S, "invalid fixed format (" + d + " not " + S + ")", "format." + d, a[d]), a[d]);
    t = h("signed", "boolean", t), i = h("width", "number", i), e = h("decimals", "number", e);
  }
  b(i % 8 === 0, "invalid FixedNumber width (not byte aligned)", "format.width", i), b(e <= 80, "invalid FixedNumber decimals (too large)", "format.decimals", e);
  const r = (t ? "" : "u") + "fixed" + String(i) + "x" + String(e);
  return { signed: t, width: i, decimals: e, name: r };
}
function X(n, t) {
  let i = "";
  n < m && (i = "-", n *= Q);
  let e = n.toString();
  if (t === 0)
    return i + e;
  for (; e.length <= t; )
    e = N + e;
  const r = e.length - t;
  for (e = e.substring(0, r) + "." + e.substring(r); e[0] === "0" && e[1] !== "."; )
    e = e.substring(1);
  for (; e[e.length - 1] === "0" && e[e.length - 2] !== "."; )
    e = e.substring(0, e.length - 1);
  return i + e;
}
var g, l, f, o, U, c, M, R, V, _;
const p = class p {
  // Use this when changing this file to get some typing info,
  // but then switch to any to mask the internal type
  //constructor(guard: any, value: bigint, format: _FixedFormat) {
  /**
   *  @private
   */
  constructor(t, i, e) {
    F(this, o);
    /**
     *  The specific fixed-point arithmetic field for this value.
     */
    A(this, "format");
    F(this, g);
    // The actual value (accounting for decimals)
    F(this, l);
    // A base-10 value to multiple values by to maintain the magnitude
    F(this, f);
    /**
     *  This is a property so console.log shows a human-meaningful value.
     *
     *  @private
     */
    A(this, "_value");
    D(t, x, "FixedNumber"), y(this, l, i), y(this, g, e);
    const r = X(i, e.decimals);
    G(this, { format: e.name, _value: r }), y(this, f, v(e.decimals));
  }
  /**
   *  If true, negative values are permitted, otherwise only
   *  positive values and zero are allowed.
   */
  get signed() {
    return s(this, g).signed;
  }
  /**
   *  The number of bits available to store the value.
   */
  get width() {
    return s(this, g).width;
  }
  /**
   *  The number of decimal places in the fixed-point arithment field.
   */
  get decimals() {
    return s(this, g).decimals;
  }
  /**
   *  The value as an integer, based on the smallest unit the
   *  [[decimals]] allow.
   */
  get value() {
    return s(this, l);
  }
  /**
   *  Returns a new [[FixedNumber]] with the result of %%this%% added
   *  to %%other%%, ignoring overflow.
   */
  addUnsafe(t) {
    return u(this, o, M).call(this, t);
  }
  /**
   *  Returns a new [[FixedNumber]] with the result of %%this%% added
   *  to %%other%%. A [[NumericFaultError]] is thrown if overflow
   *  occurs.
   */
  add(t) {
    return u(this, o, M).call(this, t, "add");
  }
  /**
   *  Returns a new [[FixedNumber]] with the result of %%other%% subtracted
   *  from %%this%%, ignoring overflow.
   */
  subUnsafe(t) {
    return u(this, o, R).call(this, t);
  }
  /**
   *  Returns a new [[FixedNumber]] with the result of %%other%% subtracted
   *  from %%this%%. A [[NumericFaultError]] is thrown if overflow
   *  occurs.
   */
  sub(t) {
    return u(this, o, R).call(this, t, "sub");
  }
  /**
   *  Returns a new [[FixedNumber]] with the result of %%this%% multiplied
   *  by %%other%%, ignoring overflow and underflow (precision loss).
   */
  mulUnsafe(t) {
    return u(this, o, V).call(this, t);
  }
  /**
   *  Returns a new [[FixedNumber]] with the result of %%this%% multiplied
   *  by %%other%%. A [[NumericFaultError]] is thrown if overflow
   *  occurs.
   */
  mul(t) {
    return u(this, o, V).call(this, t, "mul");
  }
  /**
   *  Returns a new [[FixedNumber]] with the result of %%this%% multiplied
   *  by %%other%%. A [[NumericFaultError]] is thrown if overflow
   *  occurs or if underflow (precision loss) occurs.
   */
  mulSignal(t) {
    u(this, o, U).call(this, t);
    const i = s(this, l) * s(t, l);
    return w(i % s(this, f) === m, "precision lost during signalling mul", "NUMERIC_FAULT", {
      operation: "mulSignal",
      fault: "underflow",
      value: this
    }), u(this, o, c).call(this, i / s(this, f), "mulSignal");
  }
  /**
   *  Returns a new [[FixedNumber]] with the result of %%this%% divided
   *  by %%other%%, ignoring underflow (precision loss). A
   *  [[NumericFaultError]] is thrown if overflow occurs.
   */
  divUnsafe(t) {
    return u(this, o, _).call(this, t);
  }
  /**
   *  Returns a new [[FixedNumber]] with the result of %%this%% divided
   *  by %%other%%, ignoring underflow (precision loss). A
   *  [[NumericFaultError]] is thrown if overflow occurs.
   */
  div(t) {
    return u(this, o, _).call(this, t, "div");
  }
  /**
   *  Returns a new [[FixedNumber]] with the result of %%this%% divided
   *  by %%other%%. A [[NumericFaultError]] is thrown if underflow
   *  (precision loss) occurs.
   */
  divSignal(t) {
    w(s(t, l) !== m, "division by zero", "NUMERIC_FAULT", {
      operation: "div",
      fault: "divide-by-zero",
      value: this
    }), u(this, o, U).call(this, t);
    const i = s(this, l) * s(this, f);
    return w(i % s(t, l) === m, "precision lost during signalling div", "NUMERIC_FAULT", {
      operation: "divSignal",
      fault: "underflow",
      value: this
    }), u(this, o, c).call(this, i / s(t, l), "divSignal");
  }
  /**
   *  Returns a comparison result between %%this%% and %%other%%.
   *
   *  This is suitable for use in sorting, where ``-1`` implies %%this%%
   *  is smaller, ``1`` implies %%this%% is larger and ``0`` implies
   *  both are equal.
   */
  cmp(t) {
    let i = this.value, e = t.value;
    const r = this.decimals - t.decimals;
    return r > 0 ? e *= v(r) : r < 0 && (i *= v(-r)), i < e ? -1 : i > e ? 1 : 0;
  }
  /**
   *  Returns true if %%other%% is equal to %%this%%.
   */
  eq(t) {
    return this.cmp(t) === 0;
  }
  /**
   *  Returns true if %%other%% is less than to %%this%%.
   */
  lt(t) {
    return this.cmp(t) < 0;
  }
  /**
   *  Returns true if %%other%% is less than or equal to %%this%%.
   */
  lte(t) {
    return this.cmp(t) <= 0;
  }
  /**
   *  Returns true if %%other%% is greater than to %%this%%.
   */
  gt(t) {
    return this.cmp(t) > 0;
  }
  /**
   *  Returns true if %%other%% is greater than or equal to %%this%%.
   */
  gte(t) {
    return this.cmp(t) >= 0;
  }
  /**
   *  Returns a new [[FixedNumber]] which is the largest **integer**
   *  that is less than or equal to %%this%%.
   *
   *  The decimal component of the result will always be ``0``.
   */
  floor() {
    let t = s(this, l);
    return s(this, l) < m && (t -= s(this, f) - I), t = s(this, l) / s(this, f) * s(this, f), u(this, o, c).call(this, t, "floor");
  }
  /**
   *  Returns a new [[FixedNumber]] which is the smallest **integer**
   *  that is greater than or equal to %%this%%.
   *
   *  The decimal component of the result will always be ``0``.
   */
  ceiling() {
    let t = s(this, l);
    return s(this, l) > m && (t += s(this, f) - I), t = s(this, l) / s(this, f) * s(this, f), u(this, o, c).call(this, t, "ceiling");
  }
  /**
   *  Returns a new [[FixedNumber]] with the decimal component
   *  rounded up on ties at %%decimals%% places.
   */
  round(t) {
    if (t == null && (t = 0), t >= this.decimals)
      return this;
    const i = this.decimals - t, e = W * v(i - 1);
    let r = this.value + e;
    const a = v(i);
    return r = r / a * a, B(r, s(this, g), "round"), new p(x, r, s(this, g));
  }
  /**
   *  Returns true if %%this%% is equal to ``0``.
   */
  isZero() {
    return s(this, l) === m;
  }
  /**
   *  Returns true if %%this%% is less than ``0``.
   */
  isNegative() {
    return s(this, l) < m;
  }
  /**
   *  Returns the string representation of %%this%%.
   */
  toString() {
    return this._value;
  }
  /**
   *  Returns a float approximation.
   *
   *  Due to IEEE 754 precission (or lack thereof), this function
   *  can only return an approximation and most values will contain
   *  rounding errors.
   */
  toUnsafeFloat() {
    return parseFloat(this.toString());
  }
  /**
   *  Return a new [[FixedNumber]] with the same value but has had
   *  its field set to %%format%%.
   *
   *  This will throw if the value cannot fit into %%format%%.
   */
  toFormat(t) {
    return p.fromString(this.toString(), t);
  }
  /**
   *  Creates a new [[FixedNumber]] for %%value%% divided by
   *  %%decimal%% places with %%format%%.
   *
   *  This will throw a [[NumericFaultError]] if %%value%% (once adjusted
   *  for %%decimals%%) cannot fit in %%format%%, either due to overflow
   *  or underflow (precision loss).
   */
  static fromValue(t, i, e) {
    const r = i == null ? 0 : z(i), a = C(e);
    let h = J(t, "value");
    const d = r - a.decimals;
    if (d > 0) {
      const S = v(d);
      w(h % S === m, "value loses precision for format", "NUMERIC_FAULT", {
        operation: "fromValue",
        fault: "underflow",
        value: t
      }), h /= S;
    } else d < 0 && (h *= v(-d));
    return B(h, a, "fromValue"), new p(x, h, a);
  }
  /**
   *  Creates a new [[FixedNumber]] for %%value%% with %%format%%.
   *
   *  This will throw a [[NumericFaultError]] if %%value%% cannot fit
   *  in %%format%%, either due to overflow or underflow (precision loss).
   */
  static fromString(t, i) {
    const e = t.match(/^(-?)([0-9]*)\.?([0-9]*)$/);
    b(e && e[2].length + e[3].length > 0, "invalid FixedNumber string value", "value", t);
    const r = C(i);
    let a = e[2] || "0", h = e[3] || "";
    for (; h.length < r.decimals; )
      h += N;
    w(h.substring(r.decimals).match(/^0*$/), "too many decimals for format", "NUMERIC_FAULT", {
      operation: "fromString",
      fault: "underflow",
      value: t
    }), h = h.substring(0, r.decimals);
    const d = BigInt(e[1] + a + h);
    return B(d, r, "fromString"), new p(x, d, r);
  }
  /**
   *  Creates a new [[FixedNumber]] with the big-endian representation
   *  %%value%% with %%format%%.
   *
   *  This will throw a [[NumericFaultError]] if %%value%% cannot fit
   *  in %%format%% due to overflow.
   */
  static fromBytes(t, i) {
    let e = K(H(t, "value"));
    const r = C(i);
    return r.signed && (e = L(e, r.width)), B(e, r, "fromBytes"), new p(x, e, r);
  }
};
g = new WeakMap(), l = new WeakMap(), f = new WeakMap(), o = new WeakSet(), U = function(t) {
  b(this.format === t.format, "incompatible format; use fixedNumber.toFormat", "other", t);
}, c = function(t, i) {
  return t = B(t, s(this, g), i), new p(x, t, s(this, g));
}, M = function(t, i) {
  return u(this, o, U).call(this, t), u(this, o, c).call(this, s(this, l) + s(t, l), i);
}, R = function(t, i) {
  return u(this, o, U).call(this, t), u(this, o, c).call(this, s(this, l) - s(t, l), i);
}, V = function(t, i) {
  return u(this, o, U).call(this, t), u(this, o, c).call(this, s(this, l) * s(t, l) / s(this, f), i);
}, _ = function(t, i) {
  return w(s(t, l) !== m, "division by zero", "NUMERIC_FAULT", {
    operation: "div",
    fault: "divide-by-zero",
    value: this
  }), u(this, o, U).call(this, t), u(this, o, c).call(this, s(this, l) * s(this, f) / s(t, l), i);
};
let T = p;
const P = [
  "wei",
  "kwei",
  "mwei",
  "gwei",
  "szabo",
  "finney",
  "ether"
];
function Y(n, t) {
  let i = 18;
  if (typeof t == "string") {
    const e = P.indexOf(t);
    b(e >= 0, "invalid unit", "unit", t), i = 3 * e;
  } else t != null && (i = z(t, "unit"));
  return T.fromValue(n, i, { decimals: i, width: 512 }).toString();
}
function et(n, t) {
  b(typeof n == "string", "value must be a string", "value", n);
  let i = 18;
  if (typeof t == "string") {
    const e = P.indexOf(t);
    b(e >= 0, "invalid unit", "unit", t), i = 3 * e;
  } else t != null && (i = z(t, "unit"));
  return T.fromString(n, { decimals: i, width: 512 }).value;
}
function nt(n) {
  return Y(n, 18);
}
export {
  nt as a,
  Y as f,
  et as p
};
