// Copyright (c) 2020-2021, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0

package srvcache

import (
	"net/url"
)

type servers struct {
	servers []Server
}

// Servers returns a copy of the server list, changes made to server instances
// will not be reflected in the source collection
func (s *servers) Servers() []Server {
	servers := make([]Server, len(s.servers))

	copy(servers, s.servers)

	return servers
}

// Count is the amount of servers stored
func (s *servers) Count() int {
	return len(s.servers)
}

// Each invokes f on each contained Server
func (s *servers) Each(f func(Server)) {
	for _, srv := range s.servers {
		f(srv)
	}
}

// Strings returns a list of urls for each Server
func (s *servers) Strings() (urls []string) {
	urls = make([]string, len(s.servers))

	for i, s := range s.servers {
		urls[i] = s.String()
	}

	return urls
}

// URLs returns a list of *url.URL for each Server
func (s *servers) URLs() (urls []*url.URL, err error) {
	urls = make([]*url.URL, len(s.servers))

	for i, s := range s.servers {
		urls[i], err = s.URL()
		if err != nil {
			return nil, err
		}
	}

	return urls, nil
}

// HostPorts returns a list of host:port strings for each server
func (s *servers) HostPorts() (hps []string) {
	hps = make([]string, len(s.servers))

	for i, s := range s.servers {
		hps[i] = s.HostPort()
	}

	return hps
}
