// @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, BosStr, DefaultStr, FromStaticStr};

#[allow(unused_imports)]
use jacquard_common::deps::codegen::unicode_segmentation::UnicodeSegmentation;
use jacquard_common::deps::smol_str::SmolStr;
use jacquard_common::types::collection::{Collection, RecordError};
use jacquard_common::types::string::{AtUri, Cid};
use jacquard_common::types::uri::{RecordUri, UriError};
use jacquard_common::types::value::Data;
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.

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(
    rename_all = "camelCase",
    rename = "uk.skyblur.preference",
    tag = "$type",
    bound(deserialize = "S: Deserialize<'de> + BosStr")
)]
pub struct Preference<S: BosStr = DefaultStr> {
    pub my_page: preference::MyPage<S>,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}

/// 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<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cid: Option<Cid<S>>,
    pub uri: AtUri<S>,
    pub value: Preference<S>,
}


#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct MyPage<S: BosStr = DefaultStr> {
    ///Define the description displayed on MyPage.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<S>,
    ///If this item is true, MyPage will be displayed.
    pub is_use_my_page: bool,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}

impl<S: BosStr> Preference<S> {
    pub fn uri(uri: S) -> Result<RecordUri<S, PreferenceRecord>, UriError> {
        RecordUri::try_from_uri(AtUri::new(uri)?)
    }
}

/// 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<S: BosStr> = PreferenceGetRecordOutput<S>;
    type Err = RecordError;
}

impl<S: BosStr> From<PreferenceGetRecordOutput<S>> for Preference<S> {
    fn from(output: PreferenceGetRecordOutput<S>) -> Self {
        output.value
    }
}

impl<S: BosStr> Collection for Preference<S> {
    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<S: BosStr> LexiconSchema for Preference<S> {
    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<S: BosStr> LexiconSchema for MyPage<S> {
    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<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetMyPage<St> {}
    impl<St: State> State for SetMyPage<St> {
        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<St: preference_state::State, S: BosStr = DefaultStr> {
    _state: PhantomData<fn() -> St>,
    _fields: (Option<preference::MyPage<S>>,),
    _type: PhantomData<fn() -> S>,
}

impl Preference<DefaultStr> {
    /// Create a new builder for this type, using the default string type (DefaultStr = SmolStr) if needed
    pub fn new() -> PreferenceBuilder<preference_state::Empty, DefaultStr> {
        PreferenceBuilder::new()
    }
}

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

impl PreferenceBuilder<preference_state::Empty, DefaultStr> {
    /// Create a new builder with all fields unset, using the default string type, if needed
    pub fn new() -> Self {
        PreferenceBuilder {
            _state: PhantomData,
            _fields: (None,),
            _type: PhantomData,
        }
    }
}

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

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

impl<St, S: BosStr> PreferenceBuilder<St, S>
where
    St: preference_state::State,
    St::MyPage: preference_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> Preference<S> {
        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<SmolStr, Data<S>>,
    ) -> Preference<S> {
        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<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetIsUseMyPage<St> {}
    impl<St: State> State for SetIsUseMyPage<St> {
        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<St: my_page_state::State, S: BosStr = DefaultStr> {
    _state: PhantomData<fn() -> St>,
    _fields: (Option<S>, Option<bool>),
    _type: PhantomData<fn() -> S>,
}

impl MyPage<DefaultStr> {
    /// Create a new builder for this type, using the default string type (DefaultStr = SmolStr) if needed
    pub fn new() -> MyPageBuilder<my_page_state::Empty, DefaultStr> {
        MyPageBuilder::new()
    }
}

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

impl MyPageBuilder<my_page_state::Empty, DefaultStr> {
    /// Create a new builder with all fields unset, using the default string type, if needed
    pub fn new() -> Self {
        MyPageBuilder {
            _state: PhantomData,
            _fields: (None, None),
            _type: PhantomData,
        }
    }
}

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

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

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

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