/// 邮箱名
pub(crate) struct Name {
    /// 正常队列名
    pub(crate) name: String,
    /// 重试队列名
    pub(crate) retry: String,
    /// 死信队列名
    pub(crate) dead: String,
}

impl Name {
    pub(crate) fn new(name: &str) -> Self {
        Name {
            name: name.to_string(),
            retry: format!("{}-retry", name),
            dead: format!("{}-dead-letter", name),
        }
    }

    /// 从重试队列名还原邮箱名
    #[allow(unused)]
    pub(crate) fn from_retry(name: &str) -> Self {
        Name::new(Self::decode_retry(name))
    }

    /// 从死信队列名还原邮箱名
    #[allow(unused)]
    pub(crate) fn from_dead_letter(name: &str) -> Self {
        Name::new(Self::decode_dead_letter(name))
    }

    #[allow(unused)]
    pub(crate) fn decode_retry(name: &str) -> &str {
        name.trim_end_matches("-retry")
    }

    #[allow(unused)]
    pub(crate) fn decode_dead_letter(name: &str) -> &str {
        name.trim_end_matches("-dead-letter")
    }

    pub(crate) fn names(&self) -> Vec<&String> {
        vec![&self.name, &self.retry, &self.dead]
    }

    pub(crate) fn is_dead_letter(&self, name: &str) -> bool {
        name == self.dead
    }

    pub(crate) fn group(&self) -> String {
        format!("{}-consumer", self.name)
    }

    pub(crate) fn retry_group(&self) -> String {
        format!("{}-consumer", self.retry)
    }

    pub(crate) fn group_with_id(&self, id: i32) -> String {
        format!("{}-consumer{}", self.name, id)
    }

    pub(crate) fn retry_group_with_id(&self, id: i32) -> String {
        format!("{}-consumer{}", self.retry, id)
    }
}
