'use strict';

var utils_types_array = require('../../utils/types/array.cjs');
var utils_types_iterable = require('../../utils/types/iterable.cjs');
require('crypto');
var utils_inheritance = require('../../utils/inheritance.cjs');
require('bs58');
require('multiformats/bases/base58');
require('varint');
require('../../utils/encoding/ss-58.cjs');
require('../../utils/types/object.cjs');
require('../../utils/types/set.cjs');
require('blake2b');
require('../../utils/types/bytes.cjs');
require('../../utils/interfaces.cjs');
require('../../utils/assert.cjs');
require('../../utils/error.cjs');
require('../../utils/types/string.cjs');

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

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

        for (const module of utils_types_array.ensureArray(modules)) {
          utils_inheritance.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 ${utils_types_iterable.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];
}

exports.injectModuleRouter = injectModuleRouter;
//# sourceMappingURL=common.cjs.map
