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 ResolvedNode {
    Ranges(super::SummarizeRangesNode),
    Series(super::SummarizeSeriesNode),
    Value(Box<super::SelectValueNode>),
    Cartesian(super::SummarizeCartesianNode),
    Cartesian3d(super::SummarizeCartesian3dNode),
    Frequency(Box<super::FrequencyDomainNode>),
    FrequencyV2(Box<super::FrequencyDomainNodeV2>),
    Histogram(Box<super::HistogramNode>),
    Curve(super::CurveFitNode),
    Multivariate(super::SummarizeMultivariateNode),
    /// An unknown variant.
    Unknown(Unknown),
}
impl ser::Serialize for ResolvedNode {
    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 {
            ResolvedNode::Ranges(value) => {
                map.serialize_entry(&"type", &"ranges")?;
                map.serialize_entry(&"ranges", value)?;
            }
            ResolvedNode::Series(value) => {
                map.serialize_entry(&"type", &"series")?;
                map.serialize_entry(&"series", value)?;
            }
            ResolvedNode::Value(value) => {
                map.serialize_entry(&"type", &"value")?;
                map.serialize_entry(&"value", value)?;
            }
            ResolvedNode::Cartesian(value) => {
                map.serialize_entry(&"type", &"cartesian")?;
                map.serialize_entry(&"cartesian", value)?;
            }
            ResolvedNode::Cartesian3d(value) => {
                map.serialize_entry(&"type", &"cartesian3d")?;
                map.serialize_entry(&"cartesian3d", value)?;
            }
            ResolvedNode::Frequency(value) => {
                map.serialize_entry(&"type", &"frequency")?;
                map.serialize_entry(&"frequency", value)?;
            }
            ResolvedNode::FrequencyV2(value) => {
                map.serialize_entry(&"type", &"frequencyV2")?;
                map.serialize_entry(&"frequencyV2", value)?;
            }
            ResolvedNode::Histogram(value) => {
                map.serialize_entry(&"type", &"histogram")?;
                map.serialize_entry(&"histogram", value)?;
            }
            ResolvedNode::Curve(value) => {
                map.serialize_entry(&"type", &"curve")?;
                map.serialize_entry(&"curve", value)?;
            }
            ResolvedNode::Multivariate(value) => {
                map.serialize_entry(&"type", &"multivariate")?;
                map.serialize_entry(&"multivariate", value)?;
            }
            ResolvedNode::Unknown(value) => {
                map.serialize_entry(&"type", &value.type_)?;
                map.serialize_entry(&value.type_, &value.value)?;
            }
        }
        map.end()
    }
}
impl<'de> de::Deserialize<'de> for ResolvedNode {
    fn deserialize<D>(d: D) -> Result<ResolvedNode, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_map(Visitor_)
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = ResolvedNode;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("union ResolvedNode")
    }
    fn visit_map<A>(self, mut map: A) -> Result<ResolvedNode, 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_::Ranges, Some(Variant_::Ranges)) => {
                        let value = map.next_value()?;
                        ResolvedNode::Ranges(value)
                    }
                    (Variant_::Series, Some(Variant_::Series)) => {
                        let value = map.next_value()?;
                        ResolvedNode::Series(value)
                    }
                    (Variant_::Value, Some(Variant_::Value)) => {
                        let value = map.next_value()?;
                        ResolvedNode::Value(value)
                    }
                    (Variant_::Cartesian, Some(Variant_::Cartesian)) => {
                        let value = map.next_value()?;
                        ResolvedNode::Cartesian(value)
                    }
                    (Variant_::Cartesian3d, Some(Variant_::Cartesian3d)) => {
                        let value = map.next_value()?;
                        ResolvedNode::Cartesian3d(value)
                    }
                    (Variant_::Frequency, Some(Variant_::Frequency)) => {
                        let value = map.next_value()?;
                        ResolvedNode::Frequency(value)
                    }
                    (Variant_::FrequencyV2, Some(Variant_::FrequencyV2)) => {
                        let value = map.next_value()?;
                        ResolvedNode::FrequencyV2(value)
                    }
                    (Variant_::Histogram, Some(Variant_::Histogram)) => {
                        let value = map.next_value()?;
                        ResolvedNode::Histogram(value)
                    }
                    (Variant_::Curve, Some(Variant_::Curve)) => {
                        let value = map.next_value()?;
                        ResolvedNode::Curve(value)
                    }
                    (Variant_::Multivariate, Some(Variant_::Multivariate)) => {
                        let value = map.next_value()?;
                        ResolvedNode::Multivariate(value)
                    }
                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
                        if type_ == b {
                            let value = map.next_value()?;
                            ResolvedNode::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_::Ranges => {
                        let value = map.next_value()?;
                        ResolvedNode::Ranges(value)
                    }
                    Variant_::Series => {
                        let value = map.next_value()?;
                        ResolvedNode::Series(value)
                    }
                    Variant_::Value => {
                        let value = map.next_value()?;
                        ResolvedNode::Value(value)
                    }
                    Variant_::Cartesian => {
                        let value = map.next_value()?;
                        ResolvedNode::Cartesian(value)
                    }
                    Variant_::Cartesian3d => {
                        let value = map.next_value()?;
                        ResolvedNode::Cartesian3d(value)
                    }
                    Variant_::Frequency => {
                        let value = map.next_value()?;
                        ResolvedNode::Frequency(value)
                    }
                    Variant_::FrequencyV2 => {
                        let value = map.next_value()?;
                        ResolvedNode::FrequencyV2(value)
                    }
                    Variant_::Histogram => {
                        let value = map.next_value()?;
                        ResolvedNode::Histogram(value)
                    }
                    Variant_::Curve => {
                        let value = map.next_value()?;
                        ResolvedNode::Curve(value)
                    }
                    Variant_::Multivariate => {
                        let value = map.next_value()?;
                        ResolvedNode::Multivariate(value)
                    }
                    Variant_::Unknown(type_) => {
                        let value = map.next_value()?;
                        ResolvedNode::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_ {
    Ranges,
    Series,
    Value,
    Cartesian,
    Cartesian3d,
    Frequency,
    FrequencyV2,
    Histogram,
    Curve,
    Multivariate,
    Unknown(Box<str>),
}
impl Variant_ {
    fn as_str(&self) -> &'static str {
        match *self {
            Variant_::Ranges => "ranges",
            Variant_::Series => "series",
            Variant_::Value => "value",
            Variant_::Cartesian => "cartesian",
            Variant_::Cartesian3d => "cartesian3d",
            Variant_::Frequency => "frequency",
            Variant_::FrequencyV2 => "frequencyV2",
            Variant_::Histogram => "histogram",
            Variant_::Curve => "curve",
            Variant_::Multivariate => "multivariate",
            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 {
            "ranges" => Variant_::Ranges,
            "series" => Variant_::Series,
            "value" => Variant_::Value,
            "cartesian" => Variant_::Cartesian,
            "cartesian3d" => Variant_::Cartesian3d,
            "frequency" => Variant_::Frequency,
            "frequencyV2" => Variant_::FrequencyV2,
            "histogram" => Variant_::Histogram,
            "curve" => Variant_::Curve,
            "multivariate" => Variant_::Multivariate,
            value => Variant_::Unknown(value.to_string().into_boxed_str()),
        };
        Ok(v)
    }
}
///An unknown variant of the ResolvedNode 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_
    }
}
