'use strict';

var utils_interfaces = require('../../utils/interfaces.cjs');
var types_generic_withBase = require('./with-base.cjs');
var types_generic_withCatchNull = require('./with-catch-null.cjs');
var types_generic_withEq = require('./with-eq.cjs');
require('../../utils/assert.cjs');
require('../../utils/error.cjs');
require('../../utils/types/array.cjs');
require('../../utils/inheritance.cjs');
require('../../utils/types/object.cjs');
require('../../utils/types/iterable.cjs');
require('../../utils/types/set.cjs');
require('../../utils/types/bytes.cjs');
require('crypto');
require('../../utils/types/string.cjs');
require('bs58');
require('multiformats/bases/base58');
require('varint');
require('../../utils/encoding/ss-58.cjs');
require('blake2b');
require('./with-from-json-bytes.cjs');
require('./any-of.cjs');
require('./with-from.cjs');

let TypedNumber$1 = class TypedNumber extends types_generic_withBase(class NumberBase {}) {
  constructor(value) {
    super();

    const num = Number(value);
    if (!Number.isFinite(num)) {
      throw new Error(
        `Invalid number provided: \`${value}\`, parsed as \`${num}\` by \`${this.constructor.name}\``,
      );
    } else if (!Number.isSafeInteger(num)) {
      throw new Error(
        `Number must be a safe integer, received: \`${num}\` by \`${this.constructor.name}\``,
      );
    }

    this.value = num;
  }

  inc() {
    return new this.constructor(++this.value);
  }

  dec() {
    return new this.constructor(--this.value);
  }

  toJSON() {
    return this.value;
  }

  apply(fn) {
    return fn(this.value);
  }

  static from(value) {
    if (value instanceof this) {
      return value;
    } else if (typeof value === 'object') {
      return this.fromApi(value);
    } else {
      return new this(value);
    }
  }

  static fromJSON(value) {
    return new this(value);
  }

  static fromApi(value) {
    return new this(utils_interfaces.maybeToNumber(value.value ?? value));
  }

  valueOf() {
    return this.value;
  }

  eq(other) {
    return this.value === other.value;
  }
};

var TypedNumber = types_generic_withEq(types_generic_withCatchNull(TypedNumber$1));

module.exports = TypedNumber;
//# sourceMappingURL=typed-number.cjs.map
