use conjure_object::serde::{ser, de};
use conjure_object::serde::ser::SerializeMap as SerializeMap_;
use conjure_object::private::{UnionField_, UnionTypeField_};
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum EventOrigin {
    /// This event was created in this workbook
    Workbook(super::WorkbookEventOrigin),
    /// This event was created in this template
    Template(super::TemplateEventOrigin),
    /// This event was created programmatically via the API
    Api(super::ApiEventOrigin),
    /// This event was created automatically from a checklist execution.
    DataReview(super::DataReviewEventOrigin),
    /// This event was created automatically from a procedure execution.
    Procedure(super::ProcedureEventOrigin),
    /// This event was created automatically from the execution of a streaming checklist.
    StreamingChecklist(super::StreamingChecklistEventOrigin),
    /// An unknown variant.
    Unknown(Unknown),
}
impl ser::Serialize for EventOrigin {
    fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
    where
        S: ser::Serializer,
    {
        let mut map = s.serialize_map(Some(2))?;
        match self {
            EventOrigin::Workbook(value) => {
                map.serialize_entry(&"type", &"workbook")?;
                map.serialize_entry(&"workbook", value)?;
            }
            EventOrigin::Template(value) => {
                map.serialize_entry(&"type", &"template")?;
                map.serialize_entry(&"template", value)?;
            }
            EventOrigin::Api(value) => {
                map.serialize_entry(&"type", &"api")?;
                map.serialize_entry(&"api", value)?;
            }
            EventOrigin::DataReview(value) => {
                map.serialize_entry(&"type", &"dataReview")?;
                map.serialize_entry(&"dataReview", value)?;
            }
            EventOrigin::Procedure(value) => {
                map.serialize_entry(&"type", &"procedure")?;
                map.serialize_entry(&"procedure", value)?;
            }
            EventOrigin::StreamingChecklist(value) => {
                map.serialize_entry(&"type", &"streamingChecklist")?;
                map.serialize_entry(&"streamingChecklist", value)?;
            }
            EventOrigin::Unknown(value) => {
                map.serialize_entry(&"type", &value.type_)?;
                map.serialize_entry(&value.type_, &value.value)?;
            }
        }
        map.end()
    }
}
impl<'de> de::Deserialize<'de> for EventOrigin {
    fn deserialize<D>(d: D) -> Result<EventOrigin, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_map(Visitor_)
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = EventOrigin;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("union EventOrigin")
    }
    fn visit_map<A>(self, mut map: A) -> Result<EventOrigin, A::Error>
    where
        A: de::MapAccess<'de>,
    {
        let v = match map.next_key::<UnionField_<Variant_>>()? {
            Some(UnionField_::Type) => {
                let variant = map.next_value()?;
                let key = map.next_key()?;
                match (variant, key) {
                    (Variant_::Workbook, Some(Variant_::Workbook)) => {
                        let value = map.next_value()?;
                        EventOrigin::Workbook(value)
                    }
                    (Variant_::Template, Some(Variant_::Template)) => {
                        let value = map.next_value()?;
                        EventOrigin::Template(value)
                    }
                    (Variant_::Api, Some(Variant_::Api)) => {
                        let value = map.next_value()?;
                        EventOrigin::Api(value)
                    }
                    (Variant_::DataReview, Some(Variant_::DataReview)) => {
                        let value = map.next_value()?;
                        EventOrigin::DataReview(value)
                    }
                    (Variant_::Procedure, Some(Variant_::Procedure)) => {
                        let value = map.next_value()?;
                        EventOrigin::Procedure(value)
                    }
                    (
                        Variant_::StreamingChecklist,
                        Some(Variant_::StreamingChecklist),
                    ) => {
                        let value = map.next_value()?;
                        EventOrigin::StreamingChecklist(value)
                    }
                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
                        if type_ == b {
                            let value = map.next_value()?;
                            EventOrigin::Unknown(Unknown { type_, value })
                        } else {
                            return Err(
                                de::Error::invalid_value(de::Unexpected::Str(&type_), &&*b),
                            )
                        }
                    }
                    (variant, Some(key)) => {
                        return Err(
                            de::Error::invalid_value(
                                de::Unexpected::Str(key.as_str()),
                                &variant.as_str(),
                            ),
                        );
                    }
                    (variant, None) => {
                        return Err(de::Error::missing_field(variant.as_str()));
                    }
                }
            }
            Some(UnionField_::Value(variant)) => {
                let value = match &variant {
                    Variant_::Workbook => {
                        let value = map.next_value()?;
                        EventOrigin::Workbook(value)
                    }
                    Variant_::Template => {
                        let value = map.next_value()?;
                        EventOrigin::Template(value)
                    }
                    Variant_::Api => {
                        let value = map.next_value()?;
                        EventOrigin::Api(value)
                    }
                    Variant_::DataReview => {
                        let value = map.next_value()?;
                        EventOrigin::DataReview(value)
                    }
                    Variant_::Procedure => {
                        let value = map.next_value()?;
                        EventOrigin::Procedure(value)
                    }
                    Variant_::StreamingChecklist => {
                        let value = map.next_value()?;
                        EventOrigin::StreamingChecklist(value)
                    }
                    Variant_::Unknown(type_) => {
                        let value = map.next_value()?;
                        EventOrigin::Unknown(Unknown {
                            type_: type_.clone(),
                            value,
                        })
                    }
                };
                if map.next_key::<UnionTypeField_>()?.is_none() {
                    return Err(de::Error::missing_field("type"));
                }
                let type_variant = map.next_value::<Variant_>()?;
                if variant != type_variant {
                    return Err(
                        de::Error::invalid_value(
                            de::Unexpected::Str(type_variant.as_str()),
                            &variant.as_str(),
                        ),
                    );
                }
                value
            }
            None => return Err(de::Error::missing_field("type")),
        };
        if map.next_key::<UnionField_<Variant_>>()?.is_some() {
            return Err(de::Error::invalid_length(3, &"type and value fields"));
        }
        Ok(v)
    }
}
#[derive(PartialEq)]
enum Variant_ {
    Workbook,
    Template,
    Api,
    DataReview,
    Procedure,
    StreamingChecklist,
    Unknown(Box<str>),
}
impl Variant_ {
    fn as_str(&self) -> &'static str {
        match *self {
            Variant_::Workbook => "workbook",
            Variant_::Template => "template",
            Variant_::Api => "api",
            Variant_::DataReview => "dataReview",
            Variant_::Procedure => "procedure",
            Variant_::StreamingChecklist => "streamingChecklist",
            Variant_::Unknown(_) => "unknown variant",
        }
    }
}
impl<'de> de::Deserialize<'de> for Variant_ {
    fn deserialize<D>(d: D) -> Result<Variant_, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_str(VariantVisitor_)
    }
}
struct VariantVisitor_;
impl<'de> de::Visitor<'de> for VariantVisitor_ {
    type Value = Variant_;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("string")
    }
    fn visit_str<E>(self, value: &str) -> Result<Variant_, E>
    where
        E: de::Error,
    {
        let v = match value {
            "workbook" => Variant_::Workbook,
            "template" => Variant_::Template,
            "api" => Variant_::Api,
            "dataReview" => Variant_::DataReview,
            "procedure" => Variant_::Procedure,
            "streamingChecklist" => Variant_::StreamingChecklist,
            value => Variant_::Unknown(value.to_string().into_boxed_str()),
        };
        Ok(v)
    }
}
///An unknown variant of the EventOrigin union.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Unknown {
    type_: Box<str>,
    value: conjure_object::Any,
}
impl Unknown {
    /// Returns the unknown variant's type name.
    #[inline]
    pub fn type_(&self) -> &str {
        &self.type_
    }
}
