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 ValueTableCellConfig {
    Numeric(super::NumericCellConfig),
    Enum(super::EnumCellConfig),
    Range(super::RangeCellConfig),
    BitFlagMap(super::BitFlagMapCellConfig),
    Staleness(super::StalenessCellConfig),
    NumericArray(super::NumericArrayCellConfig),
    EnumArray(super::EnumArrayCellConfig),
    Struct(super::StructCellConfig),
    /// An unknown variant.
    Unknown(Unknown),
}
impl ser::Serialize for ValueTableCellConfig {
    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 {
            ValueTableCellConfig::Numeric(value) => {
                map.serialize_entry(&"type", &"numeric")?;
                map.serialize_entry(&"numeric", value)?;
            }
            ValueTableCellConfig::Enum(value) => {
                map.serialize_entry(&"type", &"enum")?;
                map.serialize_entry(&"enum", value)?;
            }
            ValueTableCellConfig::Range(value) => {
                map.serialize_entry(&"type", &"range")?;
                map.serialize_entry(&"range", value)?;
            }
            ValueTableCellConfig::BitFlagMap(value) => {
                map.serialize_entry(&"type", &"bitFlagMap")?;
                map.serialize_entry(&"bitFlagMap", value)?;
            }
            ValueTableCellConfig::Staleness(value) => {
                map.serialize_entry(&"type", &"staleness")?;
                map.serialize_entry(&"staleness", value)?;
            }
            ValueTableCellConfig::NumericArray(value) => {
                map.serialize_entry(&"type", &"numericArray")?;
                map.serialize_entry(&"numericArray", value)?;
            }
            ValueTableCellConfig::EnumArray(value) => {
                map.serialize_entry(&"type", &"enumArray")?;
                map.serialize_entry(&"enumArray", value)?;
            }
            ValueTableCellConfig::Struct(value) => {
                map.serialize_entry(&"type", &"struct")?;
                map.serialize_entry(&"struct", value)?;
            }
            ValueTableCellConfig::Unknown(value) => {
                map.serialize_entry(&"type", &value.type_)?;
                map.serialize_entry(&value.type_, &value.value)?;
            }
        }
        map.end()
    }
}
impl<'de> de::Deserialize<'de> for ValueTableCellConfig {
    fn deserialize<D>(d: D) -> Result<ValueTableCellConfig, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_map(Visitor_)
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = ValueTableCellConfig;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("union ValueTableCellConfig")
    }
    fn visit_map<A>(self, mut map: A) -> Result<ValueTableCellConfig, 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_::Numeric, Some(Variant_::Numeric)) => {
                        let value = map.next_value()?;
                        ValueTableCellConfig::Numeric(value)
                    }
                    (Variant_::Enum, Some(Variant_::Enum)) => {
                        let value = map.next_value()?;
                        ValueTableCellConfig::Enum(value)
                    }
                    (Variant_::Range, Some(Variant_::Range)) => {
                        let value = map.next_value()?;
                        ValueTableCellConfig::Range(value)
                    }
                    (Variant_::BitFlagMap, Some(Variant_::BitFlagMap)) => {
                        let value = map.next_value()?;
                        ValueTableCellConfig::BitFlagMap(value)
                    }
                    (Variant_::Staleness, Some(Variant_::Staleness)) => {
                        let value = map.next_value()?;
                        ValueTableCellConfig::Staleness(value)
                    }
                    (Variant_::NumericArray, Some(Variant_::NumericArray)) => {
                        let value = map.next_value()?;
                        ValueTableCellConfig::NumericArray(value)
                    }
                    (Variant_::EnumArray, Some(Variant_::EnumArray)) => {
                        let value = map.next_value()?;
                        ValueTableCellConfig::EnumArray(value)
                    }
                    (Variant_::Struct, Some(Variant_::Struct)) => {
                        let value = map.next_value()?;
                        ValueTableCellConfig::Struct(value)
                    }
                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
                        if type_ == b {
                            let value = map.next_value()?;
                            ValueTableCellConfig::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_::Numeric => {
                        let value = map.next_value()?;
                        ValueTableCellConfig::Numeric(value)
                    }
                    Variant_::Enum => {
                        let value = map.next_value()?;
                        ValueTableCellConfig::Enum(value)
                    }
                    Variant_::Range => {
                        let value = map.next_value()?;
                        ValueTableCellConfig::Range(value)
                    }
                    Variant_::BitFlagMap => {
                        let value = map.next_value()?;
                        ValueTableCellConfig::BitFlagMap(value)
                    }
                    Variant_::Staleness => {
                        let value = map.next_value()?;
                        ValueTableCellConfig::Staleness(value)
                    }
                    Variant_::NumericArray => {
                        let value = map.next_value()?;
                        ValueTableCellConfig::NumericArray(value)
                    }
                    Variant_::EnumArray => {
                        let value = map.next_value()?;
                        ValueTableCellConfig::EnumArray(value)
                    }
                    Variant_::Struct => {
                        let value = map.next_value()?;
                        ValueTableCellConfig::Struct(value)
                    }
                    Variant_::Unknown(type_) => {
                        let value = map.next_value()?;
                        ValueTableCellConfig::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_ {
    Numeric,
    Enum,
    Range,
    BitFlagMap,
    Staleness,
    NumericArray,
    EnumArray,
    Struct,
    Unknown(Box<str>),
}
impl Variant_ {
    fn as_str(&self) -> &'static str {
        match *self {
            Variant_::Numeric => "numeric",
            Variant_::Enum => "enum",
            Variant_::Range => "range",
            Variant_::BitFlagMap => "bitFlagMap",
            Variant_::Staleness => "staleness",
            Variant_::NumericArray => "numericArray",
            Variant_::EnumArray => "enumArray",
            Variant_::Struct => "struct",
            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 {
            "numeric" => Variant_::Numeric,
            "enum" => Variant_::Enum,
            "range" => Variant_::Range,
            "bitFlagMap" => Variant_::BitFlagMap,
            "staleness" => Variant_::Staleness,
            "numericArray" => Variant_::NumericArray,
            "enumArray" => Variant_::EnumArray,
            "struct" => Variant_::Struct,
            value => Variant_::Unknown(value.to_string().into_boxed_str()),
        };
        Ok(v)
    }
}
///An unknown variant of the ValueTableCellConfig 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_
    }
}
