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, conjure_object::private::DeriveWith)]
#[derive_with(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum EnumSeriesNode {
    Literal(super::LiteralEnumSeriesNode),
    Raw(super::RawEnumSeriesNode),
    Resample(super::EnumResampleSeriesNode),
    NthPointDownsample(super::EnumNthPointDownsampleSeriesNode),
    TimeRangeFilter(super::EnumTimeRangeFilterSeriesNode),
    Union(super::EnumUnionSeriesNode),
    Aggregate(super::AggregateEnumSeriesNode),
    FilterTransformation(super::EnumFilterTransformationSeriesNode),
    ValueMap(super::ValueMapSeriesNode),
    ArraySelect(super::SelectIndexFromEnumArraySeriesNode),
    ExtractFromStruct(super::ExtractEnumFromStructSeriesNode),
    GenericTransform(Box<super::GenericTransformNode>),
    ScalarUdf(super::ScalarUdfSeriesNode),
    /// An unknown variant.
    Unknown(Unknown),
}
impl ser::Serialize for EnumSeriesNode {
    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 {
            EnumSeriesNode::Literal(value) => {
                map.serialize_entry(&"type", &"literal")?;
                map.serialize_entry(&"literal", value)?;
            }
            EnumSeriesNode::Raw(value) => {
                map.serialize_entry(&"type", &"raw")?;
                map.serialize_entry(&"raw", value)?;
            }
            EnumSeriesNode::Resample(value) => {
                map.serialize_entry(&"type", &"resample")?;
                map.serialize_entry(&"resample", value)?;
            }
            EnumSeriesNode::NthPointDownsample(value) => {
                map.serialize_entry(&"type", &"nthPointDownsample")?;
                map.serialize_entry(&"nthPointDownsample", value)?;
            }
            EnumSeriesNode::TimeRangeFilter(value) => {
                map.serialize_entry(&"type", &"timeRangeFilter")?;
                map.serialize_entry(&"timeRangeFilter", value)?;
            }
            EnumSeriesNode::Union(value) => {
                map.serialize_entry(&"type", &"union")?;
                map.serialize_entry(&"union", value)?;
            }
            EnumSeriesNode::Aggregate(value) => {
                map.serialize_entry(&"type", &"aggregate")?;
                map.serialize_entry(&"aggregate", value)?;
            }
            EnumSeriesNode::FilterTransformation(value) => {
                map.serialize_entry(&"type", &"filterTransformation")?;
                map.serialize_entry(&"filterTransformation", value)?;
            }
            EnumSeriesNode::ValueMap(value) => {
                map.serialize_entry(&"type", &"valueMap")?;
                map.serialize_entry(&"valueMap", value)?;
            }
            EnumSeriesNode::ArraySelect(value) => {
                map.serialize_entry(&"type", &"arraySelect")?;
                map.serialize_entry(&"arraySelect", value)?;
            }
            EnumSeriesNode::ExtractFromStruct(value) => {
                map.serialize_entry(&"type", &"extractFromStruct")?;
                map.serialize_entry(&"extractFromStruct", value)?;
            }
            EnumSeriesNode::GenericTransform(value) => {
                map.serialize_entry(&"type", &"genericTransform")?;
                map.serialize_entry(&"genericTransform", value)?;
            }
            EnumSeriesNode::ScalarUdf(value) => {
                map.serialize_entry(&"type", &"scalarUdf")?;
                map.serialize_entry(&"scalarUdf", value)?;
            }
            EnumSeriesNode::Unknown(value) => {
                map.serialize_entry(&"type", &value.type_)?;
                map.serialize_entry(&value.type_, &value.value)?;
            }
        }
        map.end()
    }
}
impl<'de> de::Deserialize<'de> for EnumSeriesNode {
    fn deserialize<D>(d: D) -> Result<EnumSeriesNode, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_map(Visitor_)
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = EnumSeriesNode;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("union EnumSeriesNode")
    }
    fn visit_map<A>(self, mut map: A) -> Result<EnumSeriesNode, 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_::Literal, Some(Variant_::Literal)) => {
                        let value = map.next_value()?;
                        EnumSeriesNode::Literal(value)
                    }
                    (Variant_::Raw, Some(Variant_::Raw)) => {
                        let value = map.next_value()?;
                        EnumSeriesNode::Raw(value)
                    }
                    (Variant_::Resample, Some(Variant_::Resample)) => {
                        let value = map.next_value()?;
                        EnumSeriesNode::Resample(value)
                    }
                    (
                        Variant_::NthPointDownsample,
                        Some(Variant_::NthPointDownsample),
                    ) => {
                        let value = map.next_value()?;
                        EnumSeriesNode::NthPointDownsample(value)
                    }
                    (Variant_::TimeRangeFilter, Some(Variant_::TimeRangeFilter)) => {
                        let value = map.next_value()?;
                        EnumSeriesNode::TimeRangeFilter(value)
                    }
                    (Variant_::Union, Some(Variant_::Union)) => {
                        let value = map.next_value()?;
                        EnumSeriesNode::Union(value)
                    }
                    (Variant_::Aggregate, Some(Variant_::Aggregate)) => {
                        let value = map.next_value()?;
                        EnumSeriesNode::Aggregate(value)
                    }
                    (
                        Variant_::FilterTransformation,
                        Some(Variant_::FilterTransformation),
                    ) => {
                        let value = map.next_value()?;
                        EnumSeriesNode::FilterTransformation(value)
                    }
                    (Variant_::ValueMap, Some(Variant_::ValueMap)) => {
                        let value = map.next_value()?;
                        EnumSeriesNode::ValueMap(value)
                    }
                    (Variant_::ArraySelect, Some(Variant_::ArraySelect)) => {
                        let value = map.next_value()?;
                        EnumSeriesNode::ArraySelect(value)
                    }
                    (Variant_::ExtractFromStruct, Some(Variant_::ExtractFromStruct)) => {
                        let value = map.next_value()?;
                        EnumSeriesNode::ExtractFromStruct(value)
                    }
                    (Variant_::GenericTransform, Some(Variant_::GenericTransform)) => {
                        let value = map.next_value()?;
                        EnumSeriesNode::GenericTransform(value)
                    }
                    (Variant_::ScalarUdf, Some(Variant_::ScalarUdf)) => {
                        let value = map.next_value()?;
                        EnumSeriesNode::ScalarUdf(value)
                    }
                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
                        if type_ == b {
                            let value = map.next_value()?;
                            EnumSeriesNode::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_::Literal => {
                        let value = map.next_value()?;
                        EnumSeriesNode::Literal(value)
                    }
                    Variant_::Raw => {
                        let value = map.next_value()?;
                        EnumSeriesNode::Raw(value)
                    }
                    Variant_::Resample => {
                        let value = map.next_value()?;
                        EnumSeriesNode::Resample(value)
                    }
                    Variant_::NthPointDownsample => {
                        let value = map.next_value()?;
                        EnumSeriesNode::NthPointDownsample(value)
                    }
                    Variant_::TimeRangeFilter => {
                        let value = map.next_value()?;
                        EnumSeriesNode::TimeRangeFilter(value)
                    }
                    Variant_::Union => {
                        let value = map.next_value()?;
                        EnumSeriesNode::Union(value)
                    }
                    Variant_::Aggregate => {
                        let value = map.next_value()?;
                        EnumSeriesNode::Aggregate(value)
                    }
                    Variant_::FilterTransformation => {
                        let value = map.next_value()?;
                        EnumSeriesNode::FilterTransformation(value)
                    }
                    Variant_::ValueMap => {
                        let value = map.next_value()?;
                        EnumSeriesNode::ValueMap(value)
                    }
                    Variant_::ArraySelect => {
                        let value = map.next_value()?;
                        EnumSeriesNode::ArraySelect(value)
                    }
                    Variant_::ExtractFromStruct => {
                        let value = map.next_value()?;
                        EnumSeriesNode::ExtractFromStruct(value)
                    }
                    Variant_::GenericTransform => {
                        let value = map.next_value()?;
                        EnumSeriesNode::GenericTransform(value)
                    }
                    Variant_::ScalarUdf => {
                        let value = map.next_value()?;
                        EnumSeriesNode::ScalarUdf(value)
                    }
                    Variant_::Unknown(type_) => {
                        let value = map.next_value()?;
                        EnumSeriesNode::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_ {
    Literal,
    Raw,
    Resample,
    NthPointDownsample,
    TimeRangeFilter,
    Union,
    Aggregate,
    FilterTransformation,
    ValueMap,
    ArraySelect,
    ExtractFromStruct,
    GenericTransform,
    ScalarUdf,
    Unknown(Box<str>),
}
impl Variant_ {
    fn as_str(&self) -> &'static str {
        match *self {
            Variant_::Literal => "literal",
            Variant_::Raw => "raw",
            Variant_::Resample => "resample",
            Variant_::NthPointDownsample => "nthPointDownsample",
            Variant_::TimeRangeFilter => "timeRangeFilter",
            Variant_::Union => "union",
            Variant_::Aggregate => "aggregate",
            Variant_::FilterTransformation => "filterTransformation",
            Variant_::ValueMap => "valueMap",
            Variant_::ArraySelect => "arraySelect",
            Variant_::ExtractFromStruct => "extractFromStruct",
            Variant_::GenericTransform => "genericTransform",
            Variant_::ScalarUdf => "scalarUdf",
            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 {
            "literal" => Variant_::Literal,
            "raw" => Variant_::Raw,
            "resample" => Variant_::Resample,
            "nthPointDownsample" => Variant_::NthPointDownsample,
            "timeRangeFilter" => Variant_::TimeRangeFilter,
            "union" => Variant_::Union,
            "aggregate" => Variant_::Aggregate,
            "filterTransformation" => Variant_::FilterTransformation,
            "valueMap" => Variant_::ValueMap,
            "arraySelect" => Variant_::ArraySelect,
            "extractFromStruct" => Variant_::ExtractFromStruct,
            "genericTransform" => Variant_::GenericTransform,
            "scalarUdf" => Variant_::ScalarUdf,
            value => Variant_::Unknown(value.to_string().into_boxed_str()),
        };
        Ok(v)
    }
}
///An unknown variant of the EnumSeriesNode 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_
    }
}
