use serde::{Deserialize, Serialize};
use validator::Validate;

/// Built-in `Info` implementation that extracts email addresses from page text.
#[derive(Clone, Debug, Default, Serialize, Deserialize, Validate, schemars::JsonSchema)]
pub struct Emails {
    /// Unique email addresses found in source text.
    pub emails: Vec<String>,
}

impl crate::Info for Emails {
    fn json_schema() -> serde_json::Value {
        serde_json::to_value(schemars::schema_for!(Self)).unwrap_or_default()
    }

    fn post_process(
        &mut self,
        original_text: &str,
    ) -> Result<(), crate::extractor::ExtractorError> {
        let additional = crate::info::extract_all_regex(original_text, crate::info::EMAIL_REGEX)
            .unwrap_or_default();
        self.emails.extend(additional);
        self.emails.sort_unstable();
        self.emails.dedup();
        Ok(())
    }
}
