use crate::{
    discovery::{Discovery, DiscoveryError, DiscoveryResult},
    rpc::balancer::{RpcEndpoint, RpcEndpointSelector},
};

/// Resolves one RPC endpoint from discovery and selector state.
pub async fn resolve_service_endpoint<D, S>(
    discovery: &D,
    selector: &S,
    service: &str,
) -> DiscoveryResult<RpcEndpoint>
where
    D: Discovery + ?Sized,
    S: RpcEndpointSelector + ?Sized,
{
    let instances = discovery.discover(service).await?;
    selector
        .select(service, &instances)
        .ok_or_else(|| DiscoveryError::NoInstances {
            service: service.to_string(),
        })
}
