'use strict';

var types_generic_withFrom = require('./with-from.cjs');
require('../../utils/types/array.cjs');
var utils_types_bytes = require('../../utils/types/bytes.cjs');
var utils_interfaces = require('../../utils/interfaces.cjs');
var utils_inheritance = require('../../utils/inheritance.cjs');
var utils_encoding_base58 = require('../../utils/encoding/base-58.cjs');
require('multiformats/bases/base58');
require('varint');
require('../../utils/encoding/ss-58.cjs');
var types_generic_typedEnum = require('./typed-enum.cjs');
require('../../utils/types/object.cjs');
require('../../utils/types/iterable.cjs');
require('../../utils/types/set.cjs');
require('crypto');
require('../../utils/types/string.cjs');
require('../../utils/assert.cjs');
require('../../utils/error.cjs');
require('bs58');
require('blake2b');
require('./with-base.cjs');
require('./with-catch-null.cjs');
require('./with-eq.cjs');
require('./any-of.cjs');
require('./with-from-json-bytes.cjs');
require('./typed-null.cjs');
require('./create-placeholder.cjs');

function withBase58(klass) {
  const name = `withBase58(${klass.name})`;
  let res;

  if (!utils_inheritance.isEqualToOrPrototypeOf(types_generic_typedEnum, klass)) {
    const obj = {
      [name]: class extends klass {
        toString() {
          return this.toBase58();
        }

        toJSON() {
          return String(this);
        }

        get value() {
          return this.toBase58();
        }

        static fromBase58(str) {
          return this.from(utils_encoding_base58.decodeFromBase58(str));
        }

        toBase58() {
          return utils_encoding_base58.encodeAsBase58(this.bytes);
        }
      },
    };

    res = obj[name];
  } else {
    const obj = {
      [name]: class extends klass {
        static Class = klass.Class;

        static Variants = !klass.Class
          ? klass.Variants?.map(withBase58)
          : klass.Variants;

        toString() {
          return this.toBase58();
        }

        static fromBase58(str) {
          return this.fromJSON(JSON.parse(utils_types_bytes.u8aToString(utils_encoding_base58.decodeFromBase58(str))));
        }

        toBase58() {
          return utils_encoding_base58.encodeAsBase58(this.bytes);
        }

        get bytes() {
          return utils_types_bytes.stringToU8a(utils_interfaces.maybeToJSONString(this));
        }
      },
    };

    res = obj[name];
  }

  return types_generic_withFrom(res, function from(value, fromFn) {
    return typeof value === 'string' ? this.fromBase58(value) : fromFn(value);
  });
}

module.exports = withBase58;
//# sourceMappingURL=with-base58.cjs.map
