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 Dataset {
    /// Single asset. Expands to one grouping per data scope on the asset. Groupings are tagged by
    /// `assetRid` and `dataScope` so callers can safely compose multiple assets with `combine`.
    Asset(super::Asset),
    /// Single run. Expands to one grouping per data source attached to the run: asset-typed sources
    /// contribute one grouping per data scope on the asset; dataset-typed sources contribute one
    /// grouping per dataset. Groupings are tagged by `runRid` and `dataScope`; asset-typed groupings
    /// additionally carry `assetRid` so callers can safely compose multiple runs with `combine`.
    Run(super::Run),
    Saved(super::SavedDataset),
    #[deprecated(note = "use saved instead.")]
    Dataset(super::SavedDataset),
    Search(super::SearchDataset),
    Combine(super::CombinedDataset),
    Tag(super::TaggedDataset),
    Filter(super::FilteredDataset),
    TimeShift(super::TimeShiftedDataset),
    WithNumericSeries(super::WithNumericSeriesDataset),
    WithEnumSeries(super::WithEnumSeriesDataset),
    Reference(super::DatasetReference),
    #[deprecated(note = "use reference instead.")]
    NamedReference(super::DatasetReference),
    /// An unknown variant.
    Unknown(Unknown),
}
impl ser::Serialize for Dataset {
    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 {
            Dataset::Asset(value) => {
                map.serialize_entry(&"type", &"asset")?;
                map.serialize_entry(&"asset", value)?;
            }
            Dataset::Run(value) => {
                map.serialize_entry(&"type", &"run")?;
                map.serialize_entry(&"run", value)?;
            }
            Dataset::Saved(value) => {
                map.serialize_entry(&"type", &"saved")?;
                map.serialize_entry(&"saved", value)?;
            }
            #[allow(deprecated)]
            Dataset::Dataset(value) => {
                map.serialize_entry(&"type", &"dataset")?;
                map.serialize_entry(&"dataset", value)?;
            }
            Dataset::Search(value) => {
                map.serialize_entry(&"type", &"search")?;
                map.serialize_entry(&"search", value)?;
            }
            Dataset::Combine(value) => {
                map.serialize_entry(&"type", &"combine")?;
                map.serialize_entry(&"combine", value)?;
            }
            Dataset::Tag(value) => {
                map.serialize_entry(&"type", &"tag")?;
                map.serialize_entry(&"tag", value)?;
            }
            Dataset::Filter(value) => {
                map.serialize_entry(&"type", &"filter")?;
                map.serialize_entry(&"filter", value)?;
            }
            Dataset::TimeShift(value) => {
                map.serialize_entry(&"type", &"timeShift")?;
                map.serialize_entry(&"timeShift", value)?;
            }
            Dataset::WithNumericSeries(value) => {
                map.serialize_entry(&"type", &"withNumericSeries")?;
                map.serialize_entry(&"withNumericSeries", value)?;
            }
            Dataset::WithEnumSeries(value) => {
                map.serialize_entry(&"type", &"withEnumSeries")?;
                map.serialize_entry(&"withEnumSeries", value)?;
            }
            Dataset::Reference(value) => {
                map.serialize_entry(&"type", &"reference")?;
                map.serialize_entry(&"reference", value)?;
            }
            #[allow(deprecated)]
            Dataset::NamedReference(value) => {
                map.serialize_entry(&"type", &"namedReference")?;
                map.serialize_entry(&"namedReference", value)?;
            }
            Dataset::Unknown(value) => {
                map.serialize_entry(&"type", &value.type_)?;
                map.serialize_entry(&value.type_, &value.value)?;
            }
        }
        map.end()
    }
}
impl<'de> de::Deserialize<'de> for Dataset {
    fn deserialize<D>(d: D) -> Result<Dataset, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_map(Visitor_)
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = Dataset;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("union Dataset")
    }
    fn visit_map<A>(self, mut map: A) -> Result<Dataset, 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_::Asset, Some(Variant_::Asset)) => {
                        let value = map.next_value()?;
                        Dataset::Asset(value)
                    }
                    (Variant_::Run, Some(Variant_::Run)) => {
                        let value = map.next_value()?;
                        Dataset::Run(value)
                    }
                    (Variant_::Saved, Some(Variant_::Saved)) => {
                        let value = map.next_value()?;
                        Dataset::Saved(value)
                    }
                    #[allow(deprecated)]
                    (Variant_::Dataset, Some(Variant_::Dataset)) => {
                        let value = map.next_value()?;
                        Dataset::Dataset(value)
                    }
                    (Variant_::Search, Some(Variant_::Search)) => {
                        let value = map.next_value()?;
                        Dataset::Search(value)
                    }
                    (Variant_::Combine, Some(Variant_::Combine)) => {
                        let value = map.next_value()?;
                        Dataset::Combine(value)
                    }
                    (Variant_::Tag, Some(Variant_::Tag)) => {
                        let value = map.next_value()?;
                        Dataset::Tag(value)
                    }
                    (Variant_::Filter, Some(Variant_::Filter)) => {
                        let value = map.next_value()?;
                        Dataset::Filter(value)
                    }
                    (Variant_::TimeShift, Some(Variant_::TimeShift)) => {
                        let value = map.next_value()?;
                        Dataset::TimeShift(value)
                    }
                    (Variant_::WithNumericSeries, Some(Variant_::WithNumericSeries)) => {
                        let value = map.next_value()?;
                        Dataset::WithNumericSeries(value)
                    }
                    (Variant_::WithEnumSeries, Some(Variant_::WithEnumSeries)) => {
                        let value = map.next_value()?;
                        Dataset::WithEnumSeries(value)
                    }
                    (Variant_::Reference, Some(Variant_::Reference)) => {
                        let value = map.next_value()?;
                        Dataset::Reference(value)
                    }
                    #[allow(deprecated)]
                    (Variant_::NamedReference, Some(Variant_::NamedReference)) => {
                        let value = map.next_value()?;
                        Dataset::NamedReference(value)
                    }
                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
                        if type_ == b {
                            let value = map.next_value()?;
                            Dataset::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_::Asset => {
                        let value = map.next_value()?;
                        Dataset::Asset(value)
                    }
                    Variant_::Run => {
                        let value = map.next_value()?;
                        Dataset::Run(value)
                    }
                    Variant_::Saved => {
                        let value = map.next_value()?;
                        Dataset::Saved(value)
                    }
                    Variant_::Dataset => {
                        let value = map.next_value()?;
                        #[allow(deprecated)] Dataset::Dataset(value)
                    }
                    Variant_::Search => {
                        let value = map.next_value()?;
                        Dataset::Search(value)
                    }
                    Variant_::Combine => {
                        let value = map.next_value()?;
                        Dataset::Combine(value)
                    }
                    Variant_::Tag => {
                        let value = map.next_value()?;
                        Dataset::Tag(value)
                    }
                    Variant_::Filter => {
                        let value = map.next_value()?;
                        Dataset::Filter(value)
                    }
                    Variant_::TimeShift => {
                        let value = map.next_value()?;
                        Dataset::TimeShift(value)
                    }
                    Variant_::WithNumericSeries => {
                        let value = map.next_value()?;
                        Dataset::WithNumericSeries(value)
                    }
                    Variant_::WithEnumSeries => {
                        let value = map.next_value()?;
                        Dataset::WithEnumSeries(value)
                    }
                    Variant_::Reference => {
                        let value = map.next_value()?;
                        Dataset::Reference(value)
                    }
                    Variant_::NamedReference => {
                        let value = map.next_value()?;
                        #[allow(deprecated)] Dataset::NamedReference(value)
                    }
                    Variant_::Unknown(type_) => {
                        let value = map.next_value()?;
                        Dataset::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_ {
    Asset,
    Run,
    Saved,
    Dataset,
    Search,
    Combine,
    Tag,
    Filter,
    TimeShift,
    WithNumericSeries,
    WithEnumSeries,
    Reference,
    NamedReference,
    Unknown(Box<str>),
}
impl Variant_ {
    fn as_str(&self) -> &'static str {
        match *self {
            Variant_::Asset => "asset",
            Variant_::Run => "run",
            Variant_::Saved => "saved",
            Variant_::Dataset => "dataset",
            Variant_::Search => "search",
            Variant_::Combine => "combine",
            Variant_::Tag => "tag",
            Variant_::Filter => "filter",
            Variant_::TimeShift => "timeShift",
            Variant_::WithNumericSeries => "withNumericSeries",
            Variant_::WithEnumSeries => "withEnumSeries",
            Variant_::Reference => "reference",
            Variant_::NamedReference => "namedReference",
            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 {
            "asset" => Variant_::Asset,
            "run" => Variant_::Run,
            "saved" => Variant_::Saved,
            "dataset" => Variant_::Dataset,
            "search" => Variant_::Search,
            "combine" => Variant_::Combine,
            "tag" => Variant_::Tag,
            "filter" => Variant_::Filter,
            "timeShift" => Variant_::TimeShift,
            "withNumericSeries" => Variant_::WithNumericSeries,
            "withEnumSeries" => Variant_::WithEnumSeries,
            "reference" => Variant_::Reference,
            "namedReference" => Variant_::NamedReference,
            value => Variant_::Unknown(value.to_string().into_boxed_str()),
        };
        Ok(v)
    }
}
///An unknown variant of the Dataset 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_
    }
}
