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 SearchQuery {
    DateTimeField(super::DateTimeField),
    StringField(super::StringField),
    TimestampField(super::TimestampField),
    LongField(super::LongField),
    BooleanField(super::BooleanField),
    /// Performs case insensitive exact match search on the title.
    ExactMatch(String),
    /// Requires the string values in the query to exactly match the set of string values in the resource.
    /// To do a partial match, use an "and" on StringField queries.
    StringArrayExactMatch(super::StringArrayField),
    StringArrayLength(super::super::super::super::metadata::StringArrayLengthQuery),
    SearchText(String),
    Label(String),
    Property(super::super::super::super::super::api::Property),
    /// Matches documents that have a property with this key, regardless of value.
    PropertyKey(String),
    /// Matches documents that have a value for the given datetime field name.
    DateTimeFieldPresent(String),
    And(Vec<super::SearchQuery>),
    Or(Vec<super::SearchQuery>),
    Not(Box<super::SearchQuery>),
    Workspace(conjure_object::ResourceIdentifier),
    CreatedAt(super::super::super::super::metadata::CreatedAtQuery),
    /// Prefer to use archivedStatus field in IndexableDb#search
    ArchivedStatus(super::super::super::super::super::api::ArchivedStatus),
    IsPublished(bool),
    /// An unknown variant.
    Unknown(Unknown),
}
impl ser::Serialize for SearchQuery {
    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 {
            SearchQuery::DateTimeField(value) => {
                map.serialize_entry(&"type", &"dateTimeField")?;
                map.serialize_entry(&"dateTimeField", value)?;
            }
            SearchQuery::StringField(value) => {
                map.serialize_entry(&"type", &"stringField")?;
                map.serialize_entry(&"stringField", value)?;
            }
            SearchQuery::TimestampField(value) => {
                map.serialize_entry(&"type", &"timestampField")?;
                map.serialize_entry(&"timestampField", value)?;
            }
            SearchQuery::LongField(value) => {
                map.serialize_entry(&"type", &"longField")?;
                map.serialize_entry(&"longField", value)?;
            }
            SearchQuery::BooleanField(value) => {
                map.serialize_entry(&"type", &"booleanField")?;
                map.serialize_entry(&"booleanField", value)?;
            }
            SearchQuery::ExactMatch(value) => {
                map.serialize_entry(&"type", &"exactMatch")?;
                map.serialize_entry(&"exactMatch", value)?;
            }
            SearchQuery::StringArrayExactMatch(value) => {
                map.serialize_entry(&"type", &"stringArrayExactMatch")?;
                map.serialize_entry(&"stringArrayExactMatch", value)?;
            }
            SearchQuery::StringArrayLength(value) => {
                map.serialize_entry(&"type", &"stringArrayLength")?;
                map.serialize_entry(&"stringArrayLength", value)?;
            }
            SearchQuery::SearchText(value) => {
                map.serialize_entry(&"type", &"searchText")?;
                map.serialize_entry(&"searchText", value)?;
            }
            SearchQuery::Label(value) => {
                map.serialize_entry(&"type", &"label")?;
                map.serialize_entry(&"label", value)?;
            }
            SearchQuery::Property(value) => {
                map.serialize_entry(&"type", &"property")?;
                map.serialize_entry(&"property", value)?;
            }
            SearchQuery::PropertyKey(value) => {
                map.serialize_entry(&"type", &"propertyKey")?;
                map.serialize_entry(&"propertyKey", value)?;
            }
            SearchQuery::DateTimeFieldPresent(value) => {
                map.serialize_entry(&"type", &"dateTimeFieldPresent")?;
                map.serialize_entry(&"dateTimeFieldPresent", value)?;
            }
            SearchQuery::And(value) => {
                map.serialize_entry(&"type", &"and")?;
                map.serialize_entry(&"and", value)?;
            }
            SearchQuery::Or(value) => {
                map.serialize_entry(&"type", &"or")?;
                map.serialize_entry(&"or", value)?;
            }
            SearchQuery::Not(value) => {
                map.serialize_entry(&"type", &"not")?;
                map.serialize_entry(&"not", value)?;
            }
            SearchQuery::Workspace(value) => {
                map.serialize_entry(&"type", &"workspace")?;
                map.serialize_entry(&"workspace", value)?;
            }
            SearchQuery::CreatedAt(value) => {
                map.serialize_entry(&"type", &"createdAt")?;
                map.serialize_entry(&"createdAt", value)?;
            }
            SearchQuery::ArchivedStatus(value) => {
                map.serialize_entry(&"type", &"archivedStatus")?;
                map.serialize_entry(&"archivedStatus", value)?;
            }
            SearchQuery::IsPublished(value) => {
                map.serialize_entry(&"type", &"isPublished")?;
                map.serialize_entry(&"isPublished", value)?;
            }
            SearchQuery::Unknown(value) => {
                map.serialize_entry(&"type", &value.type_)?;
                map.serialize_entry(&value.type_, &value.value)?;
            }
        }
        map.end()
    }
}
impl<'de> de::Deserialize<'de> for SearchQuery {
    fn deserialize<D>(d: D) -> Result<SearchQuery, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_map(Visitor_)
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = SearchQuery;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("union SearchQuery")
    }
    fn visit_map<A>(self, mut map: A) -> Result<SearchQuery, 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_::DateTimeField, Some(Variant_::DateTimeField)) => {
                        let value = map.next_value()?;
                        SearchQuery::DateTimeField(value)
                    }
                    (Variant_::StringField, Some(Variant_::StringField)) => {
                        let value = map.next_value()?;
                        SearchQuery::StringField(value)
                    }
                    (Variant_::TimestampField, Some(Variant_::TimestampField)) => {
                        let value = map.next_value()?;
                        SearchQuery::TimestampField(value)
                    }
                    (Variant_::LongField, Some(Variant_::LongField)) => {
                        let value = map.next_value()?;
                        SearchQuery::LongField(value)
                    }
                    (Variant_::BooleanField, Some(Variant_::BooleanField)) => {
                        let value = map.next_value()?;
                        SearchQuery::BooleanField(value)
                    }
                    (Variant_::ExactMatch, Some(Variant_::ExactMatch)) => {
                        let value = map.next_value()?;
                        SearchQuery::ExactMatch(value)
                    }
                    (
                        Variant_::StringArrayExactMatch,
                        Some(Variant_::StringArrayExactMatch),
                    ) => {
                        let value = map.next_value()?;
                        SearchQuery::StringArrayExactMatch(value)
                    }
                    (Variant_::StringArrayLength, Some(Variant_::StringArrayLength)) => {
                        let value = map.next_value()?;
                        SearchQuery::StringArrayLength(value)
                    }
                    (Variant_::SearchText, Some(Variant_::SearchText)) => {
                        let value = map.next_value()?;
                        SearchQuery::SearchText(value)
                    }
                    (Variant_::Label, Some(Variant_::Label)) => {
                        let value = map.next_value()?;
                        SearchQuery::Label(value)
                    }
                    (Variant_::Property, Some(Variant_::Property)) => {
                        let value = map.next_value()?;
                        SearchQuery::Property(value)
                    }
                    (Variant_::PropertyKey, Some(Variant_::PropertyKey)) => {
                        let value = map.next_value()?;
                        SearchQuery::PropertyKey(value)
                    }
                    (
                        Variant_::DateTimeFieldPresent,
                        Some(Variant_::DateTimeFieldPresent),
                    ) => {
                        let value = map.next_value()?;
                        SearchQuery::DateTimeFieldPresent(value)
                    }
                    (Variant_::And, Some(Variant_::And)) => {
                        let value = map.next_value()?;
                        SearchQuery::And(value)
                    }
                    (Variant_::Or, Some(Variant_::Or)) => {
                        let value = map.next_value()?;
                        SearchQuery::Or(value)
                    }
                    (Variant_::Not, Some(Variant_::Not)) => {
                        let value = map.next_value()?;
                        SearchQuery::Not(value)
                    }
                    (Variant_::Workspace, Some(Variant_::Workspace)) => {
                        let value = map.next_value()?;
                        SearchQuery::Workspace(value)
                    }
                    (Variant_::CreatedAt, Some(Variant_::CreatedAt)) => {
                        let value = map.next_value()?;
                        SearchQuery::CreatedAt(value)
                    }
                    (Variant_::ArchivedStatus, Some(Variant_::ArchivedStatus)) => {
                        let value = map.next_value()?;
                        SearchQuery::ArchivedStatus(value)
                    }
                    (Variant_::IsPublished, Some(Variant_::IsPublished)) => {
                        let value = map.next_value()?;
                        SearchQuery::IsPublished(value)
                    }
                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
                        if type_ == b {
                            let value = map.next_value()?;
                            SearchQuery::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_::DateTimeField => {
                        let value = map.next_value()?;
                        SearchQuery::DateTimeField(value)
                    }
                    Variant_::StringField => {
                        let value = map.next_value()?;
                        SearchQuery::StringField(value)
                    }
                    Variant_::TimestampField => {
                        let value = map.next_value()?;
                        SearchQuery::TimestampField(value)
                    }
                    Variant_::LongField => {
                        let value = map.next_value()?;
                        SearchQuery::LongField(value)
                    }
                    Variant_::BooleanField => {
                        let value = map.next_value()?;
                        SearchQuery::BooleanField(value)
                    }
                    Variant_::ExactMatch => {
                        let value = map.next_value()?;
                        SearchQuery::ExactMatch(value)
                    }
                    Variant_::StringArrayExactMatch => {
                        let value = map.next_value()?;
                        SearchQuery::StringArrayExactMatch(value)
                    }
                    Variant_::StringArrayLength => {
                        let value = map.next_value()?;
                        SearchQuery::StringArrayLength(value)
                    }
                    Variant_::SearchText => {
                        let value = map.next_value()?;
                        SearchQuery::SearchText(value)
                    }
                    Variant_::Label => {
                        let value = map.next_value()?;
                        SearchQuery::Label(value)
                    }
                    Variant_::Property => {
                        let value = map.next_value()?;
                        SearchQuery::Property(value)
                    }
                    Variant_::PropertyKey => {
                        let value = map.next_value()?;
                        SearchQuery::PropertyKey(value)
                    }
                    Variant_::DateTimeFieldPresent => {
                        let value = map.next_value()?;
                        SearchQuery::DateTimeFieldPresent(value)
                    }
                    Variant_::And => {
                        let value = map.next_value()?;
                        SearchQuery::And(value)
                    }
                    Variant_::Or => {
                        let value = map.next_value()?;
                        SearchQuery::Or(value)
                    }
                    Variant_::Not => {
                        let value = map.next_value()?;
                        SearchQuery::Not(value)
                    }
                    Variant_::Workspace => {
                        let value = map.next_value()?;
                        SearchQuery::Workspace(value)
                    }
                    Variant_::CreatedAt => {
                        let value = map.next_value()?;
                        SearchQuery::CreatedAt(value)
                    }
                    Variant_::ArchivedStatus => {
                        let value = map.next_value()?;
                        SearchQuery::ArchivedStatus(value)
                    }
                    Variant_::IsPublished => {
                        let value = map.next_value()?;
                        SearchQuery::IsPublished(value)
                    }
                    Variant_::Unknown(type_) => {
                        let value = map.next_value()?;
                        SearchQuery::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_ {
    DateTimeField,
    StringField,
    TimestampField,
    LongField,
    BooleanField,
    ExactMatch,
    StringArrayExactMatch,
    StringArrayLength,
    SearchText,
    Label,
    Property,
    PropertyKey,
    DateTimeFieldPresent,
    And,
    Or,
    Not,
    Workspace,
    CreatedAt,
    ArchivedStatus,
    IsPublished,
    Unknown(Box<str>),
}
impl Variant_ {
    fn as_str(&self) -> &'static str {
        match *self {
            Variant_::DateTimeField => "dateTimeField",
            Variant_::StringField => "stringField",
            Variant_::TimestampField => "timestampField",
            Variant_::LongField => "longField",
            Variant_::BooleanField => "booleanField",
            Variant_::ExactMatch => "exactMatch",
            Variant_::StringArrayExactMatch => "stringArrayExactMatch",
            Variant_::StringArrayLength => "stringArrayLength",
            Variant_::SearchText => "searchText",
            Variant_::Label => "label",
            Variant_::Property => "property",
            Variant_::PropertyKey => "propertyKey",
            Variant_::DateTimeFieldPresent => "dateTimeFieldPresent",
            Variant_::And => "and",
            Variant_::Or => "or",
            Variant_::Not => "not",
            Variant_::Workspace => "workspace",
            Variant_::CreatedAt => "createdAt",
            Variant_::ArchivedStatus => "archivedStatus",
            Variant_::IsPublished => "isPublished",
            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 {
            "dateTimeField" => Variant_::DateTimeField,
            "stringField" => Variant_::StringField,
            "timestampField" => Variant_::TimestampField,
            "longField" => Variant_::LongField,
            "booleanField" => Variant_::BooleanField,
            "exactMatch" => Variant_::ExactMatch,
            "stringArrayExactMatch" => Variant_::StringArrayExactMatch,
            "stringArrayLength" => Variant_::StringArrayLength,
            "searchText" => Variant_::SearchText,
            "label" => Variant_::Label,
            "property" => Variant_::Property,
            "propertyKey" => Variant_::PropertyKey,
            "dateTimeFieldPresent" => Variant_::DateTimeFieldPresent,
            "and" => Variant_::And,
            "or" => Variant_::Or,
            "not" => Variant_::Not,
            "workspace" => Variant_::Workspace,
            "createdAt" => Variant_::CreatedAt,
            "archivedStatus" => Variant_::ArchivedStatus,
            "isPublished" => Variant_::IsPublished,
            value => Variant_::Unknown(value.to_string().into_boxed_str()),
        };
        Ok(v)
    }
}
///An unknown variant of the SearchQuery 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_
    }
}
