import { ensureArray } from '../../utils/types/array.js';
import { fmtIterable } from '../../utils/types/iterable.js';
import 'crypto';
import { ensureInstanceOf } from '../../utils/inheritance.js';
import 'bs58';
import 'multiformats/bases/base58';
import 'varint';
import '../../utils/encoding/ss-58.js';
import '../../utils/types/object.js';
import '../../utils/types/set.js';
import 'blake2b';
import '../../utils/types/bytes.js';
import '../../utils/interfaces.js';
import '../../utils/assert.js';
import '../../utils/error.js';
import '../../utils/types/string.js';

function injectModuleRouter(klass) {
  const name = `withModuleRouter(${klass.name})`;

  const classes = {
    [name]: class extends klass {
      constructor(modules) {
        super();

        for (const module of ensureArray(modules)) {
          ensureInstanceOf(module, klass);
        }

        this.modules = modules;
      }

      moduleById(id) {
        for (const module of this.modules) {
          if (module.supportsIdentifier(id)) {
            return module;
          }
        }

        throw new Error(
          `Identifier \`${id}\` is not supported by any of ${fmtIterable(
            this.modules.map((module) => module.constructor.name),
          )}`,
        );
      }

      dispatchById(id, fn) {
        return fn(this.moduleById(id));
      }

      supportsIdentifier(id) {
        return this.modules.some((module) => module.supportsIdentifier(id));
      }

      methods() {
        return this.modules.flatMap((module) => module.methods());
      }
    },
  };

  return classes[name];
}

export { injectModuleRouter };
//# sourceMappingURL=common.js.map
