package registry

import "context"

const (
	StatusHealthy = "healthy"
	StatusDead    = "dead"
)

type (
	Selector struct {
		registry Registry
	}
)

func (selector *Selector) Select(ctx context.Context, name string) (node *ServiceNode, err error) {
	var (
		nodes []*ServiceNode
	)
	if nodes, err = selector.registry.Get(name); err != nil {
		return
	}
	for _, node = range nodes {
		if node.Status == StatusHealthy {
			break
		}
	}
	return
}

func NewSelector(registry Registry) *Selector {
	return &Selector{
		registry: registry,
	}
}
