constructNamespaces static method

Map<String, Namespace> constructNamespaces({
  1. required Set<String> availableAccounts,
  2. required Set<String> availableMethods,
  3. required Set<String> availableEvents,
  4. required Map<String, RequiredNamespace> requiredNamespaces,
  5. Map<String, RequiredNamespace>? optionalNamespaces,
})

Using the availabe accounts, methods, and events, construct the namespaces If optional namespaces are provided, then they will be added to the namespaces as well

Implementation

static Map<String, Namespace> constructNamespaces({
  required Set<String> availableAccounts,
  required Set<String> availableMethods,
  required Set<String> availableEvents,
  required Map<String, RequiredNamespace> requiredNamespaces,
  Map<String, RequiredNamespace>? optionalNamespaces,
}) {
  final Map<String, Namespace> namespaces = _constructNamespacesFromRequired(
    availableAccounts: availableAccounts,
    availableMethods: availableMethods,
    availableEvents: availableEvents,
    requiredNamespaces: requiredNamespaces,
  );
  final Map<String, Namespace> optional = optionalNamespaces == null
      ? {}
      : _constructNamespacesFromRequired(
          availableAccounts: availableAccounts,
          availableMethods: availableMethods,
          availableEvents: availableEvents,
          requiredNamespaces: optionalNamespaces,
        );

  // Loop through the optional keys and if they exist in the namespaces, then merge them and delete them from optional
  List<String> keys = optional.keys.toList();
  for (int i = 0; i < keys.length; i++) {
    String key = keys[i];
    if (namespaces.containsKey(key)) {
      namespaces[key] = Namespace(
        accounts: namespaces[key]!
            .accounts
            .toSet()
            .union(optional[key]!.accounts.toSet())
            .toList(),
        methods: namespaces[key]!
            .methods
            .toSet()
            .union(optional[key]!.methods.toSet())
            .toList(),
        events: namespaces[key]!
            .events
            .toSet()
            .union(optional[key]!.events.toSet())
            .toList(),
      );
      optional.remove(key);
    }
  }

  return {
    ...namespaces,
    ...optional,
  };
}