package shared

import "strings"

// NetworkRegistry provides network configuration lookup by name.
type NetworkRegistry struct {
	networks map[string]Network
}

// NewNetworkRegistry creates a NetworkRegistry from a slice of network configs.
func NewNetworkRegistry(networks []Network) *NetworkRegistry {
	m := make(map[string]Network, len(networks))
	for _, n := range networks {
		if n.Name.IsValid() {
			m[strings.ToLower(n.Name.String())] = n
		}
	}
	return &NetworkRegistry{networks: m}
}

// Network returns the configuration for the named network.
func (r *NetworkRegistry) Network(name string) (Network, bool) {
	if r == nil || r.networks == nil {
		return Network{}, false
	}
	n, ok := r.networks[strings.ToLower(strings.TrimSpace(name))]
	return n, ok
}

// Networks returns all configured networks.
func (r *NetworkRegistry) Networks() map[string]Network {
	if r == nil {
		return nil
	}
	return r.networks
}
