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 = _constructNamespaces(
    availableAccounts: availableAccounts,
    availableMethods: availableMethods,
    availableEvents: availableEvents,
    namespacesMap: requiredNamespaces,
  );
  final Map<String, Namespace> optionals = _constructNamespaces(
    availableAccounts: availableAccounts,
    availableMethods: availableMethods,
    availableEvents: availableEvents,
    namespacesMap: optionalNamespaces ?? {},
  );

  // Loop through the optional keys and if they exist in the namespaces, then merge them and delete them from optional
  List<String> keys = optionals.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(optionals[key]!.accounts.toSet())
            .toList(),
        methods: namespaces[key]!
            .methods
            .toSet()
            .union(optionals[key]!.methods.toSet())
            .toList(),
        events: namespaces[key]!
            .events
            .toSet()
            .union(optionals[key]!.events.toSet())
            .toList(),
      );
      optionals.remove(key);
    }
  }

  return {
    ...namespaces,
    ...optionals,
  };
}