create static method

Future<AmbientAutoNAT> create(
  1. Host h,
  2. List<AutoNATOption> options
)

Implementation

static Future<AmbientAutoNAT> create(Host h, List<AutoNATOption> options) async {
  final dialPolicy = DialPolicyImpl(host: h); // Default dial policy
  final conf = AutoNATConfig(host: h, dialPolicy: dialPolicy);
  applyOptions(conf, options); // Apply user-provided options

  if (conf.addressFunc == null) {
    // In Go: if aa, ok := h.(interface{ AllAddrs() []ma.Multiaddr }); ok { conf.addressFunc = aa.AllAddrs } else { conf.addressFunc = h.Addrs }
    // Assuming Host has `allAddrs` or similar, or fallback to `addrs`
    // For now, directly use h.addrs as AddrFunc
    conf.addressFunc = () => h.addrs;
  }

  AutoNATService? serviceInstance;
  if ((!conf.forceReachability || conf.reachability == Reachability.public) && conf.dialer != null) {
    // The AutoNATService constructor in service.dart expects an AutoNATConfig.
    // We need to ensure the config passed to AutoNATService is consistent.
    // The Go code creates `newAutoNATService(conf)`.
    // We might need to pass `conf` (this config) to the AutoNATService.
    // For now, assuming AutoNATService can be constructed with the main config.
    serviceInstance = AutoNATService(conf);
    serviceInstance.enable();
  }

  final as = AmbientAutoNAT._(h, conf, serviceInstance);

  // Subscribe to event bus events
  // Assuming h.eventBus is the correct way to access the EventBus instance.
  // This might need adjustment if Host interface provides eventBus differently.
  try {
    // Note: Passing instances like EvtLocalAddressesUpdated() to subscribe
    // is unusual. Typically, you pass the Type itself.
    // The event_bus.dart example shows `eventbus.subscribe(EventType)`.
    // Let's assume it means `Type` objects.
    as._eventBusSubscription = h.eventBus.subscribe(
      [EvtLocalAddressesUpdated, EvtPeerIdentificationCompleted], // Pass Types
      // opts: [SubscriptionOptName('autonat')] // Assuming an option for naming if available
      // The `name` parameter was a guess from the placeholder.
      // The actual API for `SubscriptionOpt` would be needed if naming is desired.
      // For now, omitting `opts` if `name` is not a standard option.
    ).stream.listen(as._handleEventBusEvent); // Access .stream property first
    _log('Subscribed to EvtLocalAddressesUpdated and EvtPeerIdentificationCompleted.');
  } catch (e) {
    _log('Failed to subscribe to event bus: $e. Event bus integration might be incomplete.');
    // Proceeding without event bus if subscription fails, functionality will be degraded.
  }

  return as;
}