// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: place.stream.richtext.facet
//
// This file was automatically generated from Lexicon schemas.
// Any manual changes will be overwritten on the next regeneration.

#[allow(unused_imports)]
use alloc::collections::BTreeMap;

#[allow(unused_imports)]
use core::marker::PhantomData;

#[allow(unused_imports)]
use jacquard_common::deps::codegen::unicode_segmentation::UnicodeSegmentation;
use jacquard_derive::{IntoStatic, lexicon, open_union};
use jacquard_lexicon::lexicon::LexiconDoc;
use jacquard_lexicon::schema::LexiconSchema;

#[allow(unused_imports)]
use jacquard_lexicon::validation::{ConstraintError, ValidationPath};
use serde::{Serialize, Deserialize};
use crate::app_bsky::richtext::facet::ByteSlice;
use crate::app_bsky::richtext::facet::Link;
use crate::app_bsky::richtext::facet::Mention;
/// Annotation of a sub-string within rich text.

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct Facet<'a> {
    #[serde(borrow)]
    pub features: Vec<FacetFeaturesItem<'a>>,
    #[serde(borrow)]
    pub index: ByteSlice<'a>,
}


#[open_union]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(tag = "$type")]
#[serde(bound(deserialize = "'de: 'a"))]
pub enum FacetFeaturesItem<'a> {
    #[serde(rename = "app.bsky.richtext.facet#mention")]
    FacetMention(Box<Mention<'a>>),
    #[serde(rename = "app.bsky.richtext.facet#link")]
    FacetLink(Box<Link<'a>>),
}

impl<'a> LexiconSchema for Facet<'a> {
    fn nsid() -> &'static str {
        "place.stream.richtext.facet"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_place_stream_richtext_facet()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        Ok(())
    }
}

pub mod facet_state {

    pub use crate::builder_types::{Set, Unset, IsSet, IsUnset};
    #[allow(unused)]
    use ::core::marker::PhantomData;
    mod sealed {
        pub trait Sealed {}
    }
    /// State trait tracking which required fields have been set
    pub trait State: sealed::Sealed {
        type Features;
        type Index;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type Features = Unset;
        type Index = Unset;
    }
    ///State transition - sets the `features` field to Set
    pub struct SetFeatures<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetFeatures<S> {}
    impl<S: State> State for SetFeatures<S> {
        type Features = Set<members::features>;
        type Index = S::Index;
    }
    ///State transition - sets the `index` field to Set
    pub struct SetIndex<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetIndex<S> {}
    impl<S: State> State for SetIndex<S> {
        type Features = S::Features;
        type Index = Set<members::index>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `features` field
        pub struct features(());
        ///Marker type for the `index` field
        pub struct index(());
    }
}

/// Builder for constructing an instance of this type
pub struct FacetBuilder<'a, S: facet_state::State> {
    _state: PhantomData<fn() -> S>,
    _fields: (Option<Vec<FacetFeaturesItem<'a>>>, Option<ByteSlice<'a>>),
    _lifetime: PhantomData<&'a ()>,
}

impl<'a> Facet<'a> {
    /// Create a new builder for this type
    pub fn new() -> FacetBuilder<'a, facet_state::Empty> {
        FacetBuilder::new()
    }
}

impl<'a> FacetBuilder<'a, facet_state::Empty> {
    /// Create a new builder with all fields unset
    pub fn new() -> Self {
        FacetBuilder {
            _state: PhantomData,
            _fields: (None, None),
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> FacetBuilder<'a, S>
where
    S: facet_state::State,
    S::Features: facet_state::IsUnset,
{
    /// Set the `features` field (required)
    pub fn features(
        mut self,
        value: impl Into<Vec<FacetFeaturesItem<'a>>>,
    ) -> FacetBuilder<'a, facet_state::SetFeatures<S>> {
        self._fields.0 = Option::Some(value.into());
        FacetBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> FacetBuilder<'a, S>
where
    S: facet_state::State,
    S::Index: facet_state::IsUnset,
{
    /// Set the `index` field (required)
    pub fn index(
        mut self,
        value: impl Into<ByteSlice<'a>>,
    ) -> FacetBuilder<'a, facet_state::SetIndex<S>> {
        self._fields.1 = Option::Some(value.into());
        FacetBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> FacetBuilder<'a, S>
where
    S: facet_state::State,
    S::Features: facet_state::IsSet,
    S::Index: facet_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> Facet<'a> {
        Facet {
            features: self._fields.0.unwrap(),
            index: self._fields.1.unwrap(),
            extra_data: Default::default(),
        }
    }
    /// Build the final struct with custom extra_data
    pub fn build_with_data(
        self,
        extra_data: BTreeMap<
            jacquard_common::deps::smol_str::SmolStr,
            jacquard_common::types::value::Data<'a>,
        >,
    ) -> Facet<'a> {
        Facet {
            features: self._fields.0.unwrap(),
            index: self._fields.1.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_place_stream_richtext_facet() -> LexiconDoc<'static> {
    #[allow(unused_imports)]
    use jacquard_common::{CowStr, deps::smol_str::SmolStr, types::blob::MimeType};
    use jacquard_lexicon::lexicon::*;
    use alloc::collections::BTreeMap;
    LexiconDoc {
        lexicon: Lexicon::Lexicon1,
        id: CowStr::new_static("place.stream.richtext.facet"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Object(LexObject {
                    description: Some(
                        CowStr::new_static(
                            "Annotation of a sub-string within rich text.",
                        ),
                    ),
                    required: Some(
                        vec![
                            SmolStr::new_static("index"), SmolStr::new_static("features")
                        ],
                    ),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("features"),
                            LexObjectProperty::Array(LexArray {
                                items: LexArrayItem::Union(LexRefUnion {
                                    refs: vec![
                                        CowStr::new_static("app.bsky.richtext.facet#mention"),
                                        CowStr::new_static("app.bsky.richtext.facet#link")
                                    ],
                                    ..Default::default()
                                }),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("index"),
                            LexObjectProperty::Ref(LexRef {
                                r#ref: CowStr::new_static(
                                    "app.bsky.richtext.facet#byteSlice",
                                ),
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}