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, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ChecklistSearchQuery {
    And(Vec<super::ChecklistSearchQuery>),
    Or(Vec<super::ChecklistSearchQuery>),
    SearchText(String),
    #[deprecated(note = "use labels instead")]
    Label(String),
    Labels(super::super::super::rids::api::LabelsFilter),
    #[deprecated(note = "use properties")]
    Property(super::super::super::super::api::Property),
    Properties(super::super::super::rids::api::PropertiesFilter),
    AuthorRid(super::super::super::rids::api::UserRid),
    AssigneeRid(super::super::super::rids::api::UserRid),
    IsPublished(bool),
    Not(Box<super::ChecklistSearchQuery>),
    Workspace(conjure_object::ResourceIdentifier),
    AuthorIsCurrentUser(bool),
    AuthorRids(std::collections::BTreeSet<super::super::super::rids::api::UserRid>),
    IsArchived(bool),
    /// An unknown variant.
    Unknown(Unknown),
}
impl ser::Serialize for ChecklistSearchQuery {
    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 {
            ChecklistSearchQuery::And(value) => {
                map.serialize_entry(&"type", &"and")?;
                map.serialize_entry(&"and", value)?;
            }
            ChecklistSearchQuery::Or(value) => {
                map.serialize_entry(&"type", &"or")?;
                map.serialize_entry(&"or", value)?;
            }
            ChecklistSearchQuery::SearchText(value) => {
                map.serialize_entry(&"type", &"searchText")?;
                map.serialize_entry(&"searchText", value)?;
            }
            #[allow(deprecated)]
            ChecklistSearchQuery::Label(value) => {
                map.serialize_entry(&"type", &"label")?;
                map.serialize_entry(&"label", value)?;
            }
            ChecklistSearchQuery::Labels(value) => {
                map.serialize_entry(&"type", &"labels")?;
                map.serialize_entry(&"labels", value)?;
            }
            #[allow(deprecated)]
            ChecklistSearchQuery::Property(value) => {
                map.serialize_entry(&"type", &"property")?;
                map.serialize_entry(&"property", value)?;
            }
            ChecklistSearchQuery::Properties(value) => {
                map.serialize_entry(&"type", &"properties")?;
                map.serialize_entry(&"properties", value)?;
            }
            ChecklistSearchQuery::AuthorRid(value) => {
                map.serialize_entry(&"type", &"authorRid")?;
                map.serialize_entry(&"authorRid", value)?;
            }
            ChecklistSearchQuery::AssigneeRid(value) => {
                map.serialize_entry(&"type", &"assigneeRid")?;
                map.serialize_entry(&"assigneeRid", value)?;
            }
            ChecklistSearchQuery::IsPublished(value) => {
                map.serialize_entry(&"type", &"isPublished")?;
                map.serialize_entry(&"isPublished", value)?;
            }
            ChecklistSearchQuery::Not(value) => {
                map.serialize_entry(&"type", &"not")?;
                map.serialize_entry(&"not", value)?;
            }
            ChecklistSearchQuery::Workspace(value) => {
                map.serialize_entry(&"type", &"workspace")?;
                map.serialize_entry(&"workspace", value)?;
            }
            ChecklistSearchQuery::AuthorIsCurrentUser(value) => {
                map.serialize_entry(&"type", &"authorIsCurrentUser")?;
                map.serialize_entry(&"authorIsCurrentUser", value)?;
            }
            ChecklistSearchQuery::AuthorRids(value) => {
                map.serialize_entry(&"type", &"authorRids")?;
                map.serialize_entry(&"authorRids", value)?;
            }
            ChecklistSearchQuery::IsArchived(value) => {
                map.serialize_entry(&"type", &"isArchived")?;
                map.serialize_entry(&"isArchived", value)?;
            }
            ChecklistSearchQuery::Unknown(value) => {
                map.serialize_entry(&"type", &value.type_)?;
                map.serialize_entry(&value.type_, &value.value)?;
            }
        }
        map.end()
    }
}
impl<'de> de::Deserialize<'de> for ChecklistSearchQuery {
    fn deserialize<D>(d: D) -> Result<ChecklistSearchQuery, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_map(Visitor_)
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = ChecklistSearchQuery;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("union ChecklistSearchQuery")
    }
    fn visit_map<A>(self, mut map: A) -> Result<ChecklistSearchQuery, 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_::And, Some(Variant_::And)) => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::And(value)
                    }
                    (Variant_::Or, Some(Variant_::Or)) => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::Or(value)
                    }
                    (Variant_::SearchText, Some(Variant_::SearchText)) => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::SearchText(value)
                    }
                    #[allow(deprecated)]
                    (Variant_::Label, Some(Variant_::Label)) => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::Label(value)
                    }
                    (Variant_::Labels, Some(Variant_::Labels)) => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::Labels(value)
                    }
                    #[allow(deprecated)]
                    (Variant_::Property, Some(Variant_::Property)) => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::Property(value)
                    }
                    (Variant_::Properties, Some(Variant_::Properties)) => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::Properties(value)
                    }
                    (Variant_::AuthorRid, Some(Variant_::AuthorRid)) => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::AuthorRid(value)
                    }
                    (Variant_::AssigneeRid, Some(Variant_::AssigneeRid)) => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::AssigneeRid(value)
                    }
                    (Variant_::IsPublished, Some(Variant_::IsPublished)) => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::IsPublished(value)
                    }
                    (Variant_::Not, Some(Variant_::Not)) => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::Not(value)
                    }
                    (Variant_::Workspace, Some(Variant_::Workspace)) => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::Workspace(value)
                    }
                    (
                        Variant_::AuthorIsCurrentUser,
                        Some(Variant_::AuthorIsCurrentUser),
                    ) => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::AuthorIsCurrentUser(value)
                    }
                    (Variant_::AuthorRids, Some(Variant_::AuthorRids)) => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::AuthorRids(value)
                    }
                    (Variant_::IsArchived, Some(Variant_::IsArchived)) => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::IsArchived(value)
                    }
                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
                        if type_ == b {
                            let value = map.next_value()?;
                            ChecklistSearchQuery::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_::And => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::And(value)
                    }
                    Variant_::Or => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::Or(value)
                    }
                    Variant_::SearchText => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::SearchText(value)
                    }
                    Variant_::Label => {
                        let value = map.next_value()?;
                        #[allow(deprecated)] ChecklistSearchQuery::Label(value)
                    }
                    Variant_::Labels => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::Labels(value)
                    }
                    Variant_::Property => {
                        let value = map.next_value()?;
                        #[allow(deprecated)] ChecklistSearchQuery::Property(value)
                    }
                    Variant_::Properties => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::Properties(value)
                    }
                    Variant_::AuthorRid => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::AuthorRid(value)
                    }
                    Variant_::AssigneeRid => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::AssigneeRid(value)
                    }
                    Variant_::IsPublished => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::IsPublished(value)
                    }
                    Variant_::Not => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::Not(value)
                    }
                    Variant_::Workspace => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::Workspace(value)
                    }
                    Variant_::AuthorIsCurrentUser => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::AuthorIsCurrentUser(value)
                    }
                    Variant_::AuthorRids => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::AuthorRids(value)
                    }
                    Variant_::IsArchived => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::IsArchived(value)
                    }
                    Variant_::Unknown(type_) => {
                        let value = map.next_value()?;
                        ChecklistSearchQuery::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_ {
    And,
    Or,
    SearchText,
    Label,
    Labels,
    Property,
    Properties,
    AuthorRid,
    AssigneeRid,
    IsPublished,
    Not,
    Workspace,
    AuthorIsCurrentUser,
    AuthorRids,
    IsArchived,
    Unknown(Box<str>),
}
impl Variant_ {
    fn as_str(&self) -> &'static str {
        match *self {
            Variant_::And => "and",
            Variant_::Or => "or",
            Variant_::SearchText => "searchText",
            Variant_::Label => "label",
            Variant_::Labels => "labels",
            Variant_::Property => "property",
            Variant_::Properties => "properties",
            Variant_::AuthorRid => "authorRid",
            Variant_::AssigneeRid => "assigneeRid",
            Variant_::IsPublished => "isPublished",
            Variant_::Not => "not",
            Variant_::Workspace => "workspace",
            Variant_::AuthorIsCurrentUser => "authorIsCurrentUser",
            Variant_::AuthorRids => "authorRids",
            Variant_::IsArchived => "isArchived",
            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 {
            "and" => Variant_::And,
            "or" => Variant_::Or,
            "searchText" => Variant_::SearchText,
            "label" => Variant_::Label,
            "labels" => Variant_::Labels,
            "property" => Variant_::Property,
            "properties" => Variant_::Properties,
            "authorRid" => Variant_::AuthorRid,
            "assigneeRid" => Variant_::AssigneeRid,
            "isPublished" => Variant_::IsPublished,
            "not" => Variant_::Not,
            "workspace" => Variant_::Workspace,
            "authorIsCurrentUser" => Variant_::AuthorIsCurrentUser,
            "authorRids" => Variant_::AuthorRids,
            "isArchived" => Variant_::IsArchived,
            value => Variant_::Unknown(value.to_string().into_boxed_str()),
        };
        Ok(v)
    }
}
///An unknown variant of the ChecklistSearchQuery 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_
    }
}
