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 Duration {
    Reference(super::Reference),
    Nanoseconds(super::DurationNanoseconds),
    Milliseconds(super::DurationMilliseconds),
    Seconds(super::DurationSeconds),
    Minutes(super::DurationMinutes),
    Hours(super::DurationHours),
    Days(super::DurationDays),
    Add(super::DurationAdd),
    Subtract(super::DurationSubtract),
    Negate(super::DurationNegate),
    /// An unknown variant.
    Unknown(Unknown),
}
impl ser::Serialize for Duration {
    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 {
            Duration::Reference(value) => {
                map.serialize_entry(&"type", &"reference")?;
                map.serialize_entry(&"reference", value)?;
            }
            Duration::Nanoseconds(value) => {
                map.serialize_entry(&"type", &"nanoseconds")?;
                map.serialize_entry(&"nanoseconds", value)?;
            }
            Duration::Milliseconds(value) => {
                map.serialize_entry(&"type", &"milliseconds")?;
                map.serialize_entry(&"milliseconds", value)?;
            }
            Duration::Seconds(value) => {
                map.serialize_entry(&"type", &"seconds")?;
                map.serialize_entry(&"seconds", value)?;
            }
            Duration::Minutes(value) => {
                map.serialize_entry(&"type", &"minutes")?;
                map.serialize_entry(&"minutes", value)?;
            }
            Duration::Hours(value) => {
                map.serialize_entry(&"type", &"hours")?;
                map.serialize_entry(&"hours", value)?;
            }
            Duration::Days(value) => {
                map.serialize_entry(&"type", &"days")?;
                map.serialize_entry(&"days", value)?;
            }
            Duration::Add(value) => {
                map.serialize_entry(&"type", &"add")?;
                map.serialize_entry(&"add", value)?;
            }
            Duration::Subtract(value) => {
                map.serialize_entry(&"type", &"subtract")?;
                map.serialize_entry(&"subtract", value)?;
            }
            Duration::Negate(value) => {
                map.serialize_entry(&"type", &"negate")?;
                map.serialize_entry(&"negate", value)?;
            }
            Duration::Unknown(value) => {
                map.serialize_entry(&"type", &value.type_)?;
                map.serialize_entry(&value.type_, &value.value)?;
            }
        }
        map.end()
    }
}
impl<'de> de::Deserialize<'de> for Duration {
    fn deserialize<D>(d: D) -> Result<Duration, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_map(Visitor_)
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = Duration;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("union Duration")
    }
    fn visit_map<A>(self, mut map: A) -> Result<Duration, 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_::Reference, Some(Variant_::Reference)) => {
                        let value = map.next_value()?;
                        Duration::Reference(value)
                    }
                    (Variant_::Nanoseconds, Some(Variant_::Nanoseconds)) => {
                        let value = map.next_value()?;
                        Duration::Nanoseconds(value)
                    }
                    (Variant_::Milliseconds, Some(Variant_::Milliseconds)) => {
                        let value = map.next_value()?;
                        Duration::Milliseconds(value)
                    }
                    (Variant_::Seconds, Some(Variant_::Seconds)) => {
                        let value = map.next_value()?;
                        Duration::Seconds(value)
                    }
                    (Variant_::Minutes, Some(Variant_::Minutes)) => {
                        let value = map.next_value()?;
                        Duration::Minutes(value)
                    }
                    (Variant_::Hours, Some(Variant_::Hours)) => {
                        let value = map.next_value()?;
                        Duration::Hours(value)
                    }
                    (Variant_::Days, Some(Variant_::Days)) => {
                        let value = map.next_value()?;
                        Duration::Days(value)
                    }
                    (Variant_::Add, Some(Variant_::Add)) => {
                        let value = map.next_value()?;
                        Duration::Add(value)
                    }
                    (Variant_::Subtract, Some(Variant_::Subtract)) => {
                        let value = map.next_value()?;
                        Duration::Subtract(value)
                    }
                    (Variant_::Negate, Some(Variant_::Negate)) => {
                        let value = map.next_value()?;
                        Duration::Negate(value)
                    }
                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
                        if type_ == b {
                            let value = map.next_value()?;
                            Duration::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_::Reference => {
                        let value = map.next_value()?;
                        Duration::Reference(value)
                    }
                    Variant_::Nanoseconds => {
                        let value = map.next_value()?;
                        Duration::Nanoseconds(value)
                    }
                    Variant_::Milliseconds => {
                        let value = map.next_value()?;
                        Duration::Milliseconds(value)
                    }
                    Variant_::Seconds => {
                        let value = map.next_value()?;
                        Duration::Seconds(value)
                    }
                    Variant_::Minutes => {
                        let value = map.next_value()?;
                        Duration::Minutes(value)
                    }
                    Variant_::Hours => {
                        let value = map.next_value()?;
                        Duration::Hours(value)
                    }
                    Variant_::Days => {
                        let value = map.next_value()?;
                        Duration::Days(value)
                    }
                    Variant_::Add => {
                        let value = map.next_value()?;
                        Duration::Add(value)
                    }
                    Variant_::Subtract => {
                        let value = map.next_value()?;
                        Duration::Subtract(value)
                    }
                    Variant_::Negate => {
                        let value = map.next_value()?;
                        Duration::Negate(value)
                    }
                    Variant_::Unknown(type_) => {
                        let value = map.next_value()?;
                        Duration::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_ {
    Reference,
    Nanoseconds,
    Milliseconds,
    Seconds,
    Minutes,
    Hours,
    Days,
    Add,
    Subtract,
    Negate,
    Unknown(Box<str>),
}
impl Variant_ {
    fn as_str(&self) -> &'static str {
        match *self {
            Variant_::Reference => "reference",
            Variant_::Nanoseconds => "nanoseconds",
            Variant_::Milliseconds => "milliseconds",
            Variant_::Seconds => "seconds",
            Variant_::Minutes => "minutes",
            Variant_::Hours => "hours",
            Variant_::Days => "days",
            Variant_::Add => "add",
            Variant_::Subtract => "subtract",
            Variant_::Negate => "negate",
            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 {
            "reference" => Variant_::Reference,
            "nanoseconds" => Variant_::Nanoseconds,
            "milliseconds" => Variant_::Milliseconds,
            "seconds" => Variant_::Seconds,
            "minutes" => Variant_::Minutes,
            "hours" => Variant_::Hours,
            "days" => Variant_::Days,
            "add" => Variant_::Add,
            "subtract" => Variant_::Subtract,
            "negate" => Variant_::Negate,
            value => Variant_::Unknown(value.to_string().into_boxed_str()),
        };
        Ok(v)
    }
}
///An unknown variant of the Duration 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_
    }
}
