// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: uk.skyblur.preference
//
// 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;
use jacquard_common::CowStr;

#[allow(unused_imports)]
use jacquard_common::deps::codegen::unicode_segmentation::UnicodeSegmentation;
use jacquard_common::types::collection::{Collection, RecordError};
use jacquard_common::types::string::{AtUri, Cid};
use jacquard_common::types::uri::{RecordUri, UriError};
use jacquard_common::xrpc::XrpcResp;
use jacquard_derive::{IntoStatic, lexicon};
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::uk_skyblur::preference;
/// A declaration of a Skyblur account.

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct Preference<'a> {
    #[serde(borrow)]
    pub my_page: preference::MyPage<'a>,
}

/// Typed wrapper for GetRecord response with this collection's record type.

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct PreferenceGetRecordOutput<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub cid: Option<Cid<'a>>,
    #[serde(borrow)]
    pub uri: AtUri<'a>,
    #[serde(borrow)]
    pub value: Preference<'a>,
}


#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct MyPage<'a> {
    ///Define the description displayed on MyPage.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub description: Option<CowStr<'a>>,
    ///If this item is true, MyPage will be displayed.
    pub is_use_my_page: bool,
}

impl<'a> Preference<'a> {
    pub fn uri(
        uri: impl Into<CowStr<'a>>,
    ) -> Result<RecordUri<'a, PreferenceRecord>, UriError> {
        RecordUri::try_from_uri(AtUri::new_cow(uri.into())?)
    }
}

/// Marker type for deserializing records from this collection.

#[derive(Debug, Serialize, Deserialize)]
pub struct PreferenceRecord;
impl XrpcResp for PreferenceRecord {
    const NSID: &'static str = "uk.skyblur.preference";
    const ENCODING: &'static str = "application/json";
    type Output<'de> = PreferenceGetRecordOutput<'de>;
    type Err<'de> = RecordError<'de>;
}

impl From<PreferenceGetRecordOutput<'_>> for Preference<'_> {
    fn from(output: PreferenceGetRecordOutput<'_>) -> Self {
        use jacquard_common::IntoStatic;
        output.value.into_static()
    }
}

impl Collection for Preference<'_> {
    const NSID: &'static str = "uk.skyblur.preference";
    type Record = PreferenceRecord;
}

impl Collection for PreferenceRecord {
    const NSID: &'static str = "uk.skyblur.preference";
    type Record = PreferenceRecord;
}

impl<'a> LexiconSchema for Preference<'a> {
    fn nsid() -> &'static str {
        "uk.skyblur.preference"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_uk_skyblur_preference()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        Ok(())
    }
}

impl<'a> LexiconSchema for MyPage<'a> {
    fn nsid() -> &'static str {
        "uk.skyblur.preference"
    }
    fn def_name() -> &'static str {
        "myPage"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_uk_skyblur_preference()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        if let Some(ref value) = self.description {
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 10000usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("description"),
                    max: 10000usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        if let Some(ref value) = self.description {
            {
                let count = UnicodeSegmentation::graphemes(value.as_ref(), true).count();
                if count > 100000usize {
                    return Err(ConstraintError::MaxGraphemes {
                        path: ValidationPath::from_field("description"),
                        max: 100000usize,
                        actual: count,
                    });
                }
            }
        }
        Ok(())
    }
}

pub mod preference_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 MyPage;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type MyPage = Unset;
    }
    ///State transition - sets the `my_page` field to Set
    pub struct SetMyPage<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetMyPage<S> {}
    impl<S: State> State for SetMyPage<S> {
        type MyPage = Set<members::my_page>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `my_page` field
        pub struct my_page(());
    }
}

/// Builder for constructing an instance of this type
pub struct PreferenceBuilder<'a, S: preference_state::State> {
    _state: PhantomData<fn() -> S>,
    _fields: (Option<preference::MyPage<'a>>,),
    _lifetime: PhantomData<&'a ()>,
}

impl<'a> Preference<'a> {
    /// Create a new builder for this type
    pub fn new() -> PreferenceBuilder<'a, preference_state::Empty> {
        PreferenceBuilder::new()
    }
}

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

impl<'a, S> PreferenceBuilder<'a, S>
where
    S: preference_state::State,
    S::MyPage: preference_state::IsUnset,
{
    /// Set the `myPage` field (required)
    pub fn my_page(
        mut self,
        value: impl Into<preference::MyPage<'a>>,
    ) -> PreferenceBuilder<'a, preference_state::SetMyPage<S>> {
        self._fields.0 = Option::Some(value.into());
        PreferenceBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> PreferenceBuilder<'a, S>
where
    S: preference_state::State,
    S::MyPage: preference_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> Preference<'a> {
        Preference {
            my_page: self._fields.0.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>,
        >,
    ) -> Preference<'a> {
        Preference {
            my_page: self._fields.0.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_uk_skyblur_preference() -> 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("uk.skyblur.preference"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Record(LexRecord {
                    description: Some(
                        CowStr::new_static("A declaration of a Skyblur account."),
                    ),
                    key: Some(CowStr::new_static("literal:self")),
                    record: LexRecordRecord::Object(LexObject {
                        required: Some(vec![SmolStr::new_static("myPage")]),
                        properties: {
                            #[allow(unused_mut)]
                            let mut map = BTreeMap::new();
                            map.insert(
                                SmolStr::new_static("myPage"),
                                LexObjectProperty::Union(LexRefUnion {
                                    refs: vec![
                                        CowStr::new_static("uk.skyblur.preference#myPage")
                                    ],
                                    ..Default::default()
                                }),
                            );
                            map
                        },
                        ..Default::default()
                    }),
                    ..Default::default()
                }),
            );
            map.insert(
                SmolStr::new_static("myPage"),
                LexUserType::Object(LexObject {
                    required: Some(vec![SmolStr::new_static("isUseMyPage")]),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("description"),
                            LexObjectProperty::String(LexString {
                                description: Some(
                                    CowStr::new_static(
                                        "Define the description displayed on MyPage.",
                                    ),
                                ),
                                max_length: Some(10000usize),
                                max_graphemes: Some(100000usize),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("isUseMyPage"),
                            LexObjectProperty::Boolean(LexBoolean {
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}

pub mod my_page_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 IsUseMyPage;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type IsUseMyPage = Unset;
    }
    ///State transition - sets the `is_use_my_page` field to Set
    pub struct SetIsUseMyPage<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetIsUseMyPage<S> {}
    impl<S: State> State for SetIsUseMyPage<S> {
        type IsUseMyPage = Set<members::is_use_my_page>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `is_use_my_page` field
        pub struct is_use_my_page(());
    }
}

/// Builder for constructing an instance of this type
pub struct MyPageBuilder<'a, S: my_page_state::State> {
    _state: PhantomData<fn() -> S>,
    _fields: (Option<CowStr<'a>>, Option<bool>),
    _lifetime: PhantomData<&'a ()>,
}

impl<'a> MyPage<'a> {
    /// Create a new builder for this type
    pub fn new() -> MyPageBuilder<'a, my_page_state::Empty> {
        MyPageBuilder::new()
    }
}

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

impl<'a, S: my_page_state::State> MyPageBuilder<'a, S> {
    /// Set the `description` field (optional)
    pub fn description(mut self, value: impl Into<Option<CowStr<'a>>>) -> Self {
        self._fields.0 = value.into();
        self
    }
    /// Set the `description` field to an Option value (optional)
    pub fn maybe_description(mut self, value: Option<CowStr<'a>>) -> Self {
        self._fields.0 = value;
        self
    }
}

impl<'a, S> MyPageBuilder<'a, S>
where
    S: my_page_state::State,
    S::IsUseMyPage: my_page_state::IsUnset,
{
    /// Set the `isUseMyPage` field (required)
    pub fn is_use_my_page(
        mut self,
        value: impl Into<bool>,
    ) -> MyPageBuilder<'a, my_page_state::SetIsUseMyPage<S>> {
        self._fields.1 = Option::Some(value.into());
        MyPageBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> MyPageBuilder<'a, S>
where
    S: my_page_state::State,
    S::IsUseMyPage: my_page_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> MyPage<'a> {
        MyPage {
            description: self._fields.0,
            is_use_my_page: 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>,
        >,
    ) -> MyPage<'a> {
        MyPage {
            description: self._fields.0,
            is_use_my_page: self._fields.1.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}