text: // Copyright 2017-2021 @polkadot/types authors & contributors
  // SPDX-License-Identifier: Apache-2.0
  import { isBn, isNumber, isString, isU8a } from '@polkadot/util';
  import { decodeAddress } from '@polkadot/util-crypto';
  import { Enum } from "../codec/Enum.js";
  import { GenericAccountId } from "./AccountId.js";
  import { GenericAccountIndex } from "./AccountIndex.js";
  
  function decodeU8a(registry, u8a) {
    if ([0, 32].includes(u8a.length)) {
      return {
        Id: u8a
      };
    } else if (u8a.length === 20) {
      return {
        Address20: u8a
      };
    } else if (u8a.length <= 8) {
      return {
        Index: registry.createType('AccountIndex', u8a).toNumber()
      };
    }
  
    return u8a;
  }
  
  function decodeMultiAny(registry, value) {
    if (value instanceof GenericMultiAddress) {
      return value;
    } else if (value instanceof GenericAccountId) {
      return {
        Id: value
      };
    } else if (value instanceof GenericAccountIndex || isBn(value) || isNumber(value)) {
      return {
        Index: isNumber(value) ? value : value.toNumber()
      };
    } else if (isString(value)) {
      return decodeU8a(registry, decodeAddress(value.toString()));
    } else if (isU8a(value)) {
      return decodeU8a(registry, value);
    }
  
    return value;
  }
  
  export class GenericMultiAddress extends Enum {
    constructor(registry, value) {
      super(registry, {
        Id: 'AccountId',
        Index: 'Compact<AccountIndex>',
        Raw: 'Bytes',
        // eslint-disable-next-line sort-keys
        Address32: 'H256',
        // eslint-disable-next-line sort-keys
        Address20: 'H160'
      }, decodeMultiAny(registry, value));
    }
    /**
     * @description Returns the string representation of the value
     */
  
  
    toString() {
      return this.value.toString();
    }
  
  }