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 EnumSeries {
    #[deprecated(
        note = "AggregateEnumSeries is deprecated. Use `enumAggregation` with `aggregation.GroupByAggregationBuilder` instead.\n"
    )]
    Aggregate(super::AggregateEnumSeries),
    Raw(super::Reference),
    Channel(Box<super::ChannelSeries>),
    Derived(Box<super::DerivedSeries>),
    Resample(super::EnumResampleSeries),
    NthPointDownsample(super::EnumNthPointDownsampleSeries),
    TimeRangeFilter(super::EnumTimeRangeFilterSeries),
    TimeShift(super::EnumTimeShiftSeries),
    Union(super::EnumUnionSeries),
    FilterTransformation(super::EnumFilterTransformationSeries),
    ValueMap(super::ValueMapSeries),
    Select1dArrayIndex(super::SelectIndexFrom1dEnumArraySeries),
    ExtractFromStruct(super::ExtractEnumFromStructSeries),
    EventAggregation(super::EventsEnumSeries),
    EnumAggregation(super::EnumAggregation),
    SelectEnum(super::SelectSeries),
    FilterByTag(super::EnumTagFilterSeries),
    SelectTags(super::EnumSelectTagsSeries),
    ScalarUdf(super::ScalarUdfSeries),
    ToStartOfInterval(super::EnumToStartOfIntervalSeries),
    /// An unknown variant.
    Unknown(Unknown),
}
impl ser::Serialize for EnumSeries {
    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 {
            #[allow(deprecated)]
            EnumSeries::Aggregate(value) => {
                map.serialize_entry(&"type", &"aggregate")?;
                map.serialize_entry(&"aggregate", value)?;
            }
            EnumSeries::Raw(value) => {
                map.serialize_entry(&"type", &"raw")?;
                map.serialize_entry(&"raw", value)?;
            }
            EnumSeries::Channel(value) => {
                map.serialize_entry(&"type", &"channel")?;
                map.serialize_entry(&"channel", value)?;
            }
            EnumSeries::Derived(value) => {
                map.serialize_entry(&"type", &"derived")?;
                map.serialize_entry(&"derived", value)?;
            }
            EnumSeries::Resample(value) => {
                map.serialize_entry(&"type", &"resample")?;
                map.serialize_entry(&"resample", value)?;
            }
            EnumSeries::NthPointDownsample(value) => {
                map.serialize_entry(&"type", &"nthPointDownsample")?;
                map.serialize_entry(&"nthPointDownsample", value)?;
            }
            EnumSeries::TimeRangeFilter(value) => {
                map.serialize_entry(&"type", &"timeRangeFilter")?;
                map.serialize_entry(&"timeRangeFilter", value)?;
            }
            EnumSeries::TimeShift(value) => {
                map.serialize_entry(&"type", &"timeShift")?;
                map.serialize_entry(&"timeShift", value)?;
            }
            EnumSeries::Union(value) => {
                map.serialize_entry(&"type", &"union")?;
                map.serialize_entry(&"union", value)?;
            }
            EnumSeries::FilterTransformation(value) => {
                map.serialize_entry(&"type", &"filterTransformation")?;
                map.serialize_entry(&"filterTransformation", value)?;
            }
            EnumSeries::ValueMap(value) => {
                map.serialize_entry(&"type", &"valueMap")?;
                map.serialize_entry(&"valueMap", value)?;
            }
            EnumSeries::Select1dArrayIndex(value) => {
                map.serialize_entry(&"type", &"select1dArrayIndex")?;
                map.serialize_entry(&"select1dArrayIndex", value)?;
            }
            EnumSeries::ExtractFromStruct(value) => {
                map.serialize_entry(&"type", &"extractFromStruct")?;
                map.serialize_entry(&"extractFromStruct", value)?;
            }
            EnumSeries::EventAggregation(value) => {
                map.serialize_entry(&"type", &"eventAggregation")?;
                map.serialize_entry(&"eventAggregation", value)?;
            }
            EnumSeries::EnumAggregation(value) => {
                map.serialize_entry(&"type", &"enumAggregation")?;
                map.serialize_entry(&"enumAggregation", value)?;
            }
            EnumSeries::SelectEnum(value) => {
                map.serialize_entry(&"type", &"selectEnum")?;
                map.serialize_entry(&"selectEnum", value)?;
            }
            EnumSeries::FilterByTag(value) => {
                map.serialize_entry(&"type", &"filterByTag")?;
                map.serialize_entry(&"filterByTag", value)?;
            }
            EnumSeries::SelectTags(value) => {
                map.serialize_entry(&"type", &"selectTags")?;
                map.serialize_entry(&"selectTags", value)?;
            }
            EnumSeries::ScalarUdf(value) => {
                map.serialize_entry(&"type", &"scalarUdf")?;
                map.serialize_entry(&"scalarUdf", value)?;
            }
            EnumSeries::ToStartOfInterval(value) => {
                map.serialize_entry(&"type", &"toStartOfInterval")?;
                map.serialize_entry(&"toStartOfInterval", value)?;
            }
            EnumSeries::Unknown(value) => {
                map.serialize_entry(&"type", &value.type_)?;
                map.serialize_entry(&value.type_, &value.value)?;
            }
        }
        map.end()
    }
}
impl<'de> de::Deserialize<'de> for EnumSeries {
    fn deserialize<D>(d: D) -> Result<EnumSeries, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_map(Visitor_)
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = EnumSeries;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("union EnumSeries")
    }
    fn visit_map<A>(self, mut map: A) -> Result<EnumSeries, 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) {
                    #[allow(deprecated)]
                    (Variant_::Aggregate, Some(Variant_::Aggregate)) => {
                        let value = map.next_value()?;
                        EnumSeries::Aggregate(value)
                    }
                    (Variant_::Raw, Some(Variant_::Raw)) => {
                        let value = map.next_value()?;
                        EnumSeries::Raw(value)
                    }
                    (Variant_::Channel, Some(Variant_::Channel)) => {
                        let value = map.next_value()?;
                        EnumSeries::Channel(value)
                    }
                    (Variant_::Derived, Some(Variant_::Derived)) => {
                        let value = map.next_value()?;
                        EnumSeries::Derived(value)
                    }
                    (Variant_::Resample, Some(Variant_::Resample)) => {
                        let value = map.next_value()?;
                        EnumSeries::Resample(value)
                    }
                    (
                        Variant_::NthPointDownsample,
                        Some(Variant_::NthPointDownsample),
                    ) => {
                        let value = map.next_value()?;
                        EnumSeries::NthPointDownsample(value)
                    }
                    (Variant_::TimeRangeFilter, Some(Variant_::TimeRangeFilter)) => {
                        let value = map.next_value()?;
                        EnumSeries::TimeRangeFilter(value)
                    }
                    (Variant_::TimeShift, Some(Variant_::TimeShift)) => {
                        let value = map.next_value()?;
                        EnumSeries::TimeShift(value)
                    }
                    (Variant_::Union, Some(Variant_::Union)) => {
                        let value = map.next_value()?;
                        EnumSeries::Union(value)
                    }
                    (
                        Variant_::FilterTransformation,
                        Some(Variant_::FilterTransformation),
                    ) => {
                        let value = map.next_value()?;
                        EnumSeries::FilterTransformation(value)
                    }
                    (Variant_::ValueMap, Some(Variant_::ValueMap)) => {
                        let value = map.next_value()?;
                        EnumSeries::ValueMap(value)
                    }
                    (
                        Variant_::Select1dArrayIndex,
                        Some(Variant_::Select1dArrayIndex),
                    ) => {
                        let value = map.next_value()?;
                        EnumSeries::Select1dArrayIndex(value)
                    }
                    (Variant_::ExtractFromStruct, Some(Variant_::ExtractFromStruct)) => {
                        let value = map.next_value()?;
                        EnumSeries::ExtractFromStruct(value)
                    }
                    (Variant_::EventAggregation, Some(Variant_::EventAggregation)) => {
                        let value = map.next_value()?;
                        EnumSeries::EventAggregation(value)
                    }
                    (Variant_::EnumAggregation, Some(Variant_::EnumAggregation)) => {
                        let value = map.next_value()?;
                        EnumSeries::EnumAggregation(value)
                    }
                    (Variant_::SelectEnum, Some(Variant_::SelectEnum)) => {
                        let value = map.next_value()?;
                        EnumSeries::SelectEnum(value)
                    }
                    (Variant_::FilterByTag, Some(Variant_::FilterByTag)) => {
                        let value = map.next_value()?;
                        EnumSeries::FilterByTag(value)
                    }
                    (Variant_::SelectTags, Some(Variant_::SelectTags)) => {
                        let value = map.next_value()?;
                        EnumSeries::SelectTags(value)
                    }
                    (Variant_::ScalarUdf, Some(Variant_::ScalarUdf)) => {
                        let value = map.next_value()?;
                        EnumSeries::ScalarUdf(value)
                    }
                    (Variant_::ToStartOfInterval, Some(Variant_::ToStartOfInterval)) => {
                        let value = map.next_value()?;
                        EnumSeries::ToStartOfInterval(value)
                    }
                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
                        if type_ == b {
                            let value = map.next_value()?;
                            EnumSeries::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_::Aggregate => {
                        let value = map.next_value()?;
                        #[allow(deprecated)] EnumSeries::Aggregate(value)
                    }
                    Variant_::Raw => {
                        let value = map.next_value()?;
                        EnumSeries::Raw(value)
                    }
                    Variant_::Channel => {
                        let value = map.next_value()?;
                        EnumSeries::Channel(value)
                    }
                    Variant_::Derived => {
                        let value = map.next_value()?;
                        EnumSeries::Derived(value)
                    }
                    Variant_::Resample => {
                        let value = map.next_value()?;
                        EnumSeries::Resample(value)
                    }
                    Variant_::NthPointDownsample => {
                        let value = map.next_value()?;
                        EnumSeries::NthPointDownsample(value)
                    }
                    Variant_::TimeRangeFilter => {
                        let value = map.next_value()?;
                        EnumSeries::TimeRangeFilter(value)
                    }
                    Variant_::TimeShift => {
                        let value = map.next_value()?;
                        EnumSeries::TimeShift(value)
                    }
                    Variant_::Union => {
                        let value = map.next_value()?;
                        EnumSeries::Union(value)
                    }
                    Variant_::FilterTransformation => {
                        let value = map.next_value()?;
                        EnumSeries::FilterTransformation(value)
                    }
                    Variant_::ValueMap => {
                        let value = map.next_value()?;
                        EnumSeries::ValueMap(value)
                    }
                    Variant_::Select1dArrayIndex => {
                        let value = map.next_value()?;
                        EnumSeries::Select1dArrayIndex(value)
                    }
                    Variant_::ExtractFromStruct => {
                        let value = map.next_value()?;
                        EnumSeries::ExtractFromStruct(value)
                    }
                    Variant_::EventAggregation => {
                        let value = map.next_value()?;
                        EnumSeries::EventAggregation(value)
                    }
                    Variant_::EnumAggregation => {
                        let value = map.next_value()?;
                        EnumSeries::EnumAggregation(value)
                    }
                    Variant_::SelectEnum => {
                        let value = map.next_value()?;
                        EnumSeries::SelectEnum(value)
                    }
                    Variant_::FilterByTag => {
                        let value = map.next_value()?;
                        EnumSeries::FilterByTag(value)
                    }
                    Variant_::SelectTags => {
                        let value = map.next_value()?;
                        EnumSeries::SelectTags(value)
                    }
                    Variant_::ScalarUdf => {
                        let value = map.next_value()?;
                        EnumSeries::ScalarUdf(value)
                    }
                    Variant_::ToStartOfInterval => {
                        let value = map.next_value()?;
                        EnumSeries::ToStartOfInterval(value)
                    }
                    Variant_::Unknown(type_) => {
                        let value = map.next_value()?;
                        EnumSeries::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_ {
    Aggregate,
    Raw,
    Channel,
    Derived,
    Resample,
    NthPointDownsample,
    TimeRangeFilter,
    TimeShift,
    Union,
    FilterTransformation,
    ValueMap,
    Select1dArrayIndex,
    ExtractFromStruct,
    EventAggregation,
    EnumAggregation,
    SelectEnum,
    FilterByTag,
    SelectTags,
    ScalarUdf,
    ToStartOfInterval,
    Unknown(Box<str>),
}
impl Variant_ {
    fn as_str(&self) -> &'static str {
        match *self {
            Variant_::Aggregate => "aggregate",
            Variant_::Raw => "raw",
            Variant_::Channel => "channel",
            Variant_::Derived => "derived",
            Variant_::Resample => "resample",
            Variant_::NthPointDownsample => "nthPointDownsample",
            Variant_::TimeRangeFilter => "timeRangeFilter",
            Variant_::TimeShift => "timeShift",
            Variant_::Union => "union",
            Variant_::FilterTransformation => "filterTransformation",
            Variant_::ValueMap => "valueMap",
            Variant_::Select1dArrayIndex => "select1dArrayIndex",
            Variant_::ExtractFromStruct => "extractFromStruct",
            Variant_::EventAggregation => "eventAggregation",
            Variant_::EnumAggregation => "enumAggregation",
            Variant_::SelectEnum => "selectEnum",
            Variant_::FilterByTag => "filterByTag",
            Variant_::SelectTags => "selectTags",
            Variant_::ScalarUdf => "scalarUdf",
            Variant_::ToStartOfInterval => "toStartOfInterval",
            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 {
            "aggregate" => Variant_::Aggregate,
            "raw" => Variant_::Raw,
            "channel" => Variant_::Channel,
            "derived" => Variant_::Derived,
            "resample" => Variant_::Resample,
            "nthPointDownsample" => Variant_::NthPointDownsample,
            "timeRangeFilter" => Variant_::TimeRangeFilter,
            "timeShift" => Variant_::TimeShift,
            "union" => Variant_::Union,
            "filterTransformation" => Variant_::FilterTransformation,
            "valueMap" => Variant_::ValueMap,
            "select1dArrayIndex" => Variant_::Select1dArrayIndex,
            "extractFromStruct" => Variant_::ExtractFromStruct,
            "eventAggregation" => Variant_::EventAggregation,
            "enumAggregation" => Variant_::EnumAggregation,
            "selectEnum" => Variant_::SelectEnum,
            "filterByTag" => Variant_::FilterByTag,
            "selectTags" => Variant_::SelectTags,
            "scalarUdf" => Variant_::ScalarUdf,
            "toStartOfInterval" => Variant_::ToStartOfInterval,
            value => Variant_::Unknown(value.to_string().into_boxed_str()),
        };
        Ok(v)
    }
}
///An unknown variant of the EnumSeries 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_
    }
}
