'use strict';

require('../generic/typed-enum.cjs');
var types_generic_typedBytes = require('../generic/typed-bytes.cjs');
require('../generic/typed-string.cjs');
require('../generic/typed-array.cjs');
require('../generic/typed-tuple.cjs');
require('../generic/typed-null.cjs');
require('../../utils/types/array.cjs');
var utils_types_bytes = require('../../utils/types/bytes.cjs');
require('bs58');
require('multiformats/bases/base58');
require('varint');
require('../../utils/encoding/ss-58.cjs');
require('../generic/typed-number.cjs');
require('../generic/typed-set.cjs');
require('../generic/typed-map.cjs');
require('../generic/typed-struct.cjs');
require('uuid');
require('js-sha256');
require('../../utils/types/iterable.cjs');
require('../../utils/types/object.cjs');
require('crypto');
require('../../utils/interfaces.cjs');
require('../../utils/assert.cjs');
require('../../utils/error.cjs');
require('../../utils/inheritance.cjs');
require('../../utils/types/set.cjs');
require('../generic/with-base.cjs');
require('../generic/with-catch-null.cjs');
require('../generic/with-eq.cjs');
require('../generic/any-of.cjs');
require('../generic/with-from.cjs');
require('blake2b');
require('../../utils/types/string.cjs');
require('../generic/with-from-json-bytes.cjs');
require('../generic/create-placeholder.cjs');

class Blob extends types_generic_typedBytes {
  constructor(value) {
    let bytes;
    if (value instanceof Uint8Array) {
      bytes = value;
    } else if (value && typeof value === 'object') {
      if (typeof value[Symbol.iterator] !== 'function') {
        bytes = utils_types_bytes.stringToU8a(JSON.stringify(value));
      } else {
        bytes = utils_types_bytes.u8aToU8a(value);
      }
    } else if (typeof value === 'string') {
      if (!utils_types_bytes.isHex(value)) {
        bytes = utils_types_bytes.stringToU8a(value);
      } else {
        bytes = utils_types_bytes.hexToU8a(value);
      }
    }

    super(bytes);
  }

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

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

  toUTF8String() {
    return utils_types_bytes.u8aToString(this);
  }

  toObject() {
    try {
      return JSON.parse(utils_types_bytes.u8aToString(this.bytes));
    } catch (err) {
      throw new Error(`Underlying value is not a valid JSON: ${err}`);
    }
  }

  toObjectOrBytes() {
    try {
      return this.toObject();
    } catch (err) {
      console.error('Failed to convert blob to JSON:', err);

      return this.bytes;
    }
  }
}

module.exports = Blob;
//# sourceMappingURL=blob.cjs.map
