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 ComputeNode {
    Enum(Box<super::EnumSeries>),
    Numeric(Box<super::NumericSeries>),
    Log(Box<super::LogSeries>),
    Ranges(Box<super::RangeSeries>),
    Array(Box<super::ArraySeries>),
    Struct(Box<super::StructSeries>),
    /// Deprecated: use `curveV2` (CurveFitV2) instead.
    CurveFit(super::CurveFit),
    CurveV2(super::CurveFitV2),
    Video(super::Reference),
    #[deprecated(note = "wrap raw references in a typed compute node.")]
    Raw(super::Reference),
    /// An unknown variant.
    Unknown(Unknown),
}
impl ser::Serialize for ComputeNode {
    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 {
            ComputeNode::Enum(value) => {
                map.serialize_entry(&"type", &"enum")?;
                map.serialize_entry(&"enum", value)?;
            }
            ComputeNode::Numeric(value) => {
                map.serialize_entry(&"type", &"numeric")?;
                map.serialize_entry(&"numeric", value)?;
            }
            ComputeNode::Log(value) => {
                map.serialize_entry(&"type", &"log")?;
                map.serialize_entry(&"log", value)?;
            }
            ComputeNode::Ranges(value) => {
                map.serialize_entry(&"type", &"ranges")?;
                map.serialize_entry(&"ranges", value)?;
            }
            ComputeNode::Array(value) => {
                map.serialize_entry(&"type", &"array")?;
                map.serialize_entry(&"array", value)?;
            }
            ComputeNode::Struct(value) => {
                map.serialize_entry(&"type", &"struct")?;
                map.serialize_entry(&"struct", value)?;
            }
            ComputeNode::CurveFit(value) => {
                map.serialize_entry(&"type", &"curveFit")?;
                map.serialize_entry(&"curveFit", value)?;
            }
            ComputeNode::CurveV2(value) => {
                map.serialize_entry(&"type", &"curveV2")?;
                map.serialize_entry(&"curveV2", value)?;
            }
            ComputeNode::Video(value) => {
                map.serialize_entry(&"type", &"video")?;
                map.serialize_entry(&"video", value)?;
            }
            #[allow(deprecated)]
            ComputeNode::Raw(value) => {
                map.serialize_entry(&"type", &"raw")?;
                map.serialize_entry(&"raw", value)?;
            }
            ComputeNode::Unknown(value) => {
                map.serialize_entry(&"type", &value.type_)?;
                map.serialize_entry(&value.type_, &value.value)?;
            }
        }
        map.end()
    }
}
impl<'de> de::Deserialize<'de> for ComputeNode {
    fn deserialize<D>(d: D) -> Result<ComputeNode, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_map(Visitor_)
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = ComputeNode;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("union ComputeNode")
    }
    fn visit_map<A>(self, mut map: A) -> Result<ComputeNode, 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_::Enum, Some(Variant_::Enum)) => {
                        let value = map.next_value()?;
                        ComputeNode::Enum(value)
                    }
                    (Variant_::Numeric, Some(Variant_::Numeric)) => {
                        let value = map.next_value()?;
                        ComputeNode::Numeric(value)
                    }
                    (Variant_::Log, Some(Variant_::Log)) => {
                        let value = map.next_value()?;
                        ComputeNode::Log(value)
                    }
                    (Variant_::Ranges, Some(Variant_::Ranges)) => {
                        let value = map.next_value()?;
                        ComputeNode::Ranges(value)
                    }
                    (Variant_::Array, Some(Variant_::Array)) => {
                        let value = map.next_value()?;
                        ComputeNode::Array(value)
                    }
                    (Variant_::Struct, Some(Variant_::Struct)) => {
                        let value = map.next_value()?;
                        ComputeNode::Struct(value)
                    }
                    (Variant_::CurveFit, Some(Variant_::CurveFit)) => {
                        let value = map.next_value()?;
                        ComputeNode::CurveFit(value)
                    }
                    (Variant_::CurveV2, Some(Variant_::CurveV2)) => {
                        let value = map.next_value()?;
                        ComputeNode::CurveV2(value)
                    }
                    (Variant_::Video, Some(Variant_::Video)) => {
                        let value = map.next_value()?;
                        ComputeNode::Video(value)
                    }
                    #[allow(deprecated)]
                    (Variant_::Raw, Some(Variant_::Raw)) => {
                        let value = map.next_value()?;
                        ComputeNode::Raw(value)
                    }
                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
                        if type_ == b {
                            let value = map.next_value()?;
                            ComputeNode::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_::Enum => {
                        let value = map.next_value()?;
                        ComputeNode::Enum(value)
                    }
                    Variant_::Numeric => {
                        let value = map.next_value()?;
                        ComputeNode::Numeric(value)
                    }
                    Variant_::Log => {
                        let value = map.next_value()?;
                        ComputeNode::Log(value)
                    }
                    Variant_::Ranges => {
                        let value = map.next_value()?;
                        ComputeNode::Ranges(value)
                    }
                    Variant_::Array => {
                        let value = map.next_value()?;
                        ComputeNode::Array(value)
                    }
                    Variant_::Struct => {
                        let value = map.next_value()?;
                        ComputeNode::Struct(value)
                    }
                    Variant_::CurveFit => {
                        let value = map.next_value()?;
                        ComputeNode::CurveFit(value)
                    }
                    Variant_::CurveV2 => {
                        let value = map.next_value()?;
                        ComputeNode::CurveV2(value)
                    }
                    Variant_::Video => {
                        let value = map.next_value()?;
                        ComputeNode::Video(value)
                    }
                    Variant_::Raw => {
                        let value = map.next_value()?;
                        #[allow(deprecated)] ComputeNode::Raw(value)
                    }
                    Variant_::Unknown(type_) => {
                        let value = map.next_value()?;
                        ComputeNode::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_ {
    Enum,
    Numeric,
    Log,
    Ranges,
    Array,
    Struct,
    CurveFit,
    CurveV2,
    Video,
    Raw,
    Unknown(Box<str>),
}
impl Variant_ {
    fn as_str(&self) -> &'static str {
        match *self {
            Variant_::Enum => "enum",
            Variant_::Numeric => "numeric",
            Variant_::Log => "log",
            Variant_::Ranges => "ranges",
            Variant_::Array => "array",
            Variant_::Struct => "struct",
            Variant_::CurveFit => "curveFit",
            Variant_::CurveV2 => "curveV2",
            Variant_::Video => "video",
            Variant_::Raw => "raw",
            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 {
            "enum" => Variant_::Enum,
            "numeric" => Variant_::Numeric,
            "log" => Variant_::Log,
            "ranges" => Variant_::Ranges,
            "array" => Variant_::Array,
            "struct" => Variant_::Struct,
            "curveFit" => Variant_::CurveFit,
            "curveV2" => Variant_::CurveV2,
            "video" => Variant_::Video,
            "raw" => Variant_::Raw,
            value => Variant_::Unknown(value.to_string().into_boxed_str()),
        };
        Ok(v)
    }
}
///An unknown variant of the ComputeNode 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_
    }
}
