import { maybeToNumber } from '../../utils/interfaces.js';
import withBase from './with-base.js';
import withCatchNull from './with-catch-null.js';
import withEq from './with-eq.js';
import '../../utils/assert.js';
import '../../utils/error.js';
import '../../utils/types/array.js';
import '../../utils/inheritance.js';
import '../../utils/types/object.js';
import '../../utils/types/iterable.js';
import '../../utils/types/set.js';
import '../../utils/types/bytes.js';
import 'crypto';
import '../../utils/types/string.js';
import 'bs58';
import 'multiformats/bases/base58';
import 'varint';
import '../../utils/encoding/ss-58.js';
import 'blake2b';
import './with-from-json-bytes.js';
import './any-of.js';
import './with-from.js';

class TypedNumber extends 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(maybeToNumber(value.value ?? value));
  }

  valueOf() {
    return this.value;
  }

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

var TypedNumber$1 = withEq(withCatchNull(TypedNumber));

export { TypedNumber$1 as default };
//# sourceMappingURL=typed-number.js.map
