package libp2p

// This file contains all the default configuration options.

import (
	

	
	
	rcmgr 
	
	
	
	
	tls 
	quic 
	
	libp2pwebrtc 
	ws 
	webtransport 
	

	
)

// DefaultSecurity is the default security option.
//
// Useful when you want to extend, but not replace, the supported transport
// security protocols.
var DefaultSecurity = ChainOptions(
	Security(tls.ID, tls.New),
	Security(noise.ID, noise.New),
)

// DefaultMuxers configures libp2p to use the stream connection multiplexers.
//
// Use this option when you want to *extend* the set of multiplexers used by
// libp2p instead of replacing them.
var DefaultMuxers = Muxer(yamux.ID, yamux.DefaultTransport)

// DefaultTransports are the default libp2p transports.
//
// Use this option when you want to *extend* the set of transports used by
// libp2p instead of replacing them.
var DefaultTransports = ChainOptions(
	Transport(tcp.NewTCPTransport),
	Transport(quic.NewTransport),
	Transport(ws.New),
	Transport(webtransport.New),
	Transport(libp2pwebrtc.New),
)

// DefaultPrivateTransports are the default libp2p transports when a PSK is supplied.
//
// Use this option when you want to *extend* the set of transports used by
// libp2p instead of replacing them.
var DefaultPrivateTransports = ChainOptions(
	Transport(tcp.NewTCPTransport),
	Transport(ws.New),
)

// DefaultPeerstore configures libp2p to use the default peerstore.
var DefaultPeerstore Option = func( *Config) error {
	,  := pstoremem.NewPeerstore()
	if  != nil {
		return 
	}
	return .Apply(Peerstore())
}

// RandomIdentity generates a random identity. (default behaviour)
var RandomIdentity = func( *Config) error {
	, ,  := crypto.GenerateEd25519Key(rand.Reader)
	if  != nil {
		return 
	}
	return .Apply(Identity())
}

// DefaultListenAddrs configures libp2p to use default listen address.
var DefaultListenAddrs = func( *Config) error {
	 := []string{
		"/ip4/0.0.0.0/tcp/0",
		"/ip4/0.0.0.0/udp/0/quic-v1",
		"/ip4/0.0.0.0/udp/0/quic-v1/webtransport",
		"/ip4/0.0.0.0/udp/0/webrtc-direct",
		"/ip6/::/tcp/0",
		"/ip6/::/udp/0/quic-v1",
		"/ip6/::/udp/0/quic-v1/webtransport",
		"/ip6/::/udp/0/webrtc-direct",
	}
	 := make([]multiaddr.Multiaddr, 0, len())
	for ,  := range  {
		,  := multiaddr.NewMultiaddr()
		if  != nil {
			return 
		}
		 = append(, )
	}
	return .Apply(ListenAddrs(...))
}

// DefaultEnableRelay enables relay dialing and listening by default.
var DefaultEnableRelay = func( *Config) error {
	return .Apply(EnableRelay())
}

var DefaultResourceManager = func( *Config) error {
	// Default memory limit: 1/8th of total memory, minimum 128MB, maximum 1GB
	 := rcmgr.DefaultLimits
	SetDefaultServiceLimits(&)
	,  := rcmgr.NewResourceManager(rcmgr.NewFixedLimiter(.AutoScale()))
	if  != nil {
		return 
	}

	return .Apply(ResourceManager())
}

// DefaultConnectionManager creates a default connection manager
var DefaultConnectionManager = func( *Config) error {
	,  := connmgr.NewConnManager(160, 192)
	if  != nil {
		return 
	}

	return .Apply(ConnectionManager())
}

// DefaultPrometheusRegisterer configures libp2p to use the default registerer
var DefaultPrometheusRegisterer = func( *Config) error {
	return .Apply(PrometheusRegisterer(prometheus.DefaultRegisterer))
}

var defaultUDPBlackHoleDetector = func( *Config) error {
	// A black hole is a binary property. On a network if UDP dials are blocked, all dials will
	// fail. So a low success rate of 5 out 100 dials is good enough.
	return .Apply(UDPBlackHoleSuccessCounter(&swarm.BlackHoleSuccessCounter{N: 100, MinSuccesses: 5, Name: "UDP"}))
}

var defaultIPv6BlackHoleDetector = func( *Config) error {
	// A black hole is a binary property. On a network if there is no IPv6 connectivity, all
	// dials will fail. So a low success rate of 5 out 100 dials is good enough.
	return .Apply(IPv6BlackHoleSuccessCounter(&swarm.BlackHoleSuccessCounter{N: 100, MinSuccesses: 5, Name: "IPv6"}))
}

// Complete list of default options and when to fallback on them.
//
// Please *DON'T* specify default options any other way. Putting this all here
// makes tracking defaults *much* easier.
var defaults = []struct {
	fallback func(cfg *Config) bool
	opt      Option
}{
	{
		fallback: func( *Config) bool { return .Transports == nil && .ListenAddrs == nil },
		opt:      DefaultListenAddrs,
	},
	{
		fallback: func( *Config) bool { return .Transports == nil && .PSK == nil },
		opt:      DefaultTransports,
	},
	{
		fallback: func( *Config) bool { return .Transports == nil && .PSK != nil },
		opt:      DefaultPrivateTransports,
	},
	{
		fallback: func( *Config) bool { return .Muxers == nil },
		opt:      DefaultMuxers,
	},
	{
		fallback: func( *Config) bool { return !.Insecure && .SecurityTransports == nil },
		opt:      DefaultSecurity,
	},
	{
		fallback: func( *Config) bool { return .PeerKey == nil },
		opt:      RandomIdentity,
	},
	{
		fallback: func( *Config) bool { return .Peerstore == nil },
		opt:      DefaultPeerstore,
	},
	{
		fallback: func( *Config) bool { return !.RelayCustom },
		opt:      DefaultEnableRelay,
	},
	{
		fallback: func( *Config) bool { return .ResourceManager == nil },
		opt:      DefaultResourceManager,
	},
	{
		fallback: func( *Config) bool { return .ConnManager == nil },
		opt:      DefaultConnectionManager,
	},
	{
		fallback: func( *Config) bool { return !.DisableMetrics && .PrometheusRegisterer == nil },
		opt:      DefaultPrometheusRegisterer,
	},
	{
		fallback: func( *Config) bool {
			return !.CustomUDPBlackHoleSuccessCounter && .UDPBlackHoleSuccessCounter == nil
		},
		opt: defaultUDPBlackHoleDetector,
	},
	{
		fallback: func( *Config) bool {
			return !.CustomIPv6BlackHoleSuccessCounter && .IPv6BlackHoleSuccessCounter == nil
		},
		opt: defaultIPv6BlackHoleDetector,
	},
}

// Defaults configures libp2p to use the default options. Can be combined with
// other options to *extend* the default options.
var Defaults Option = func( *Config) error {
	for ,  := range defaults {
		if  := .Apply(.opt);  != nil {
			return 
		}
	}
	return nil
}

// FallbackDefaults applies default options to the libp2p node if and only if no
// other relevant options have been applied. will be appended to the options
// passed into New.
var FallbackDefaults Option = func( *Config) error {
	for ,  := range defaults {
		if !.fallback() {
			continue
		}
		if  := .Apply(.opt);  != nil {
			return 
		}
	}
	return nil
}