import '../generic/typed-enum.js';
import TypedBytes from '../generic/typed-bytes.js';
import '../generic/typed-string.js';
import '../generic/typed-array.js';
import '../generic/typed-tuple.js';
import '../generic/typed-null.js';
import '../../utils/types/array.js';
import { stringToU8a, u8aToU8a, isHex, hexToU8a, u8aToString } from '../../utils/types/bytes.js';
import 'bs58';
import 'multiformats/bases/base58';
import 'varint';
import '../../utils/encoding/ss-58.js';
import '../generic/typed-number.js';
import '../generic/typed-set.js';
import '../generic/typed-map.js';
import '../generic/typed-struct.js';
import 'uuid';
import 'js-sha256';
import '../../utils/types/iterable.js';
import '../../utils/types/object.js';
import 'crypto';
import '../../utils/interfaces.js';
import '../../utils/assert.js';
import '../../utils/error.js';
import '../../utils/inheritance.js';
import '../../utils/types/set.js';
import '../generic/with-base.js';
import '../generic/with-catch-null.js';
import '../generic/with-eq.js';
import '../generic/any-of.js';
import '../generic/with-from.js';
import 'blake2b';
import '../../utils/types/string.js';
import '../generic/with-from-json-bytes.js';
import '../generic/create-placeholder.js';

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

    super(bytes);
  }

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

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

  toUTF8String() {
    return u8aToString(this);
  }

  toObject() {
    try {
      return JSON.parse(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;
    }
  }
}

export { Blob as default };
//# sourceMappingURL=blob.js.map
