use std::fmt;
use vector_common::config::ComponentKey;
use super::configurable_component;
use crate::schema;
#[configurable_component]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct OutputId {
    pub component: ComponentKey,
    pub port: Option<String>,
}
impl OutputId {
    pub fn dummy() -> Self {
        Self {
            component: "dummy".into(),
            port: None,
        }
    }
    pub fn with_definitions(
        &self,
        definitions: impl IntoIterator<Item = schema::Definition>,
    ) -> Vec<(OutputId, schema::Definition)> {
        definitions
            .into_iter()
            .map(|definition| (self.clone(), definition))
            .collect()
    }
}
impl fmt::Display for OutputId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.port {
            None => self.component.fmt(f),
            Some(port) => write!(f, "{}.{port}", self.component),
        }
    }
}
impl From<ComponentKey> for OutputId {
    fn from(key: ComponentKey) -> Self {
        Self {
            component: key,
            port: None,
        }
    }
}
impl From<&ComponentKey> for OutputId {
    fn from(key: &ComponentKey) -> Self {
        Self::from(key.clone())
    }
}
impl From<(&ComponentKey, String)> for OutputId {
    fn from((key, name): (&ComponentKey, String)) -> Self {
        Self {
            component: key.clone(),
            port: Some(name),
        }
    }
}
impl From<(String, Option<String>)> for OutputId {
    fn from((component, port): (String, Option<String>)) -> Self {
        Self {
            component: component.into(),
            port,
        }
    }
}
#[cfg(any(test, feature = "test"))]
impl From<&str> for OutputId {
    fn from(s: &str) -> Self {
        assert!(
            !s.contains('.'),
            "Cannot convert dotted paths to strings without more context"
        );
        let component = ComponentKey::from(s);
        component.into()
    }
}