// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: dev.tsunagite.types
//
// 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_common::types::string::{AtUri, RecordKey, Rkey};
use jacquard_common::types::value::Data;
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};
/// A named value with a numeric index for sorting.

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct Indexable<'a> {
    ///The internal ID for the value, limited to the RecordKey character set.
    #[serde(borrow)]
    pub id: RecordKey<Rkey<'a>>,
    ///The numeric index used for sorting.
    pub index: i64,
    ///The human-readable name of the value for displaying in UIs.
    #[serde(borrow)]
    pub name: Data<'a>,
}

/// A typed record reference that does not require a CID hash.

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic, Default)]
#[serde(rename_all = "camelCase")]
pub struct TypedRef<'a> {
    ///The AT URI of the record this object references.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub r#ref: Option<AtUri<'a>>,
    ///The type of the record this object references.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub r#type: Option<RecordKey<Rkey<'a>>>,
}

impl<'a> LexiconSchema for Indexable<'a> {
    fn nsid() -> &'static str {
        "dev.tsunagite.types"
    }
    fn def_name() -> &'static str {
        "indexable"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_dev_tsunagite_types()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        {
            let value = &self.id;
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 32usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("id"),
                    max: 32usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        {
            let value = &self.id;
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) < 1usize {
                return Err(ConstraintError::MinLength {
                    path: ValidationPath::from_field("id"),
                    min: 1usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        Ok(())
    }
}

impl<'a> LexiconSchema for TypedRef<'a> {
    fn nsid() -> &'static str {
        "dev.tsunagite.types"
    }
    fn def_name() -> &'static str {
        "typedRef"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_dev_tsunagite_types()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        Ok(())
    }
}

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

/// Builder for constructing an instance of this type
pub struct IndexableBuilder<'a, S: indexable_state::State> {
    _state: PhantomData<fn() -> S>,
    _fields: (Option<RecordKey<Rkey<'a>>>, Option<i64>, Option<Data<'a>>),
    _lifetime: PhantomData<&'a ()>,
}

impl<'a> Indexable<'a> {
    /// Create a new builder for this type
    pub fn new() -> IndexableBuilder<'a, indexable_state::Empty> {
        IndexableBuilder::new()
    }
}

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

impl<'a, S> IndexableBuilder<'a, S>
where
    S: indexable_state::State,
    S::Id: indexable_state::IsUnset,
{
    /// Set the `id` field (required)
    pub fn id(
        mut self,
        value: impl Into<RecordKey<Rkey<'a>>>,
    ) -> IndexableBuilder<'a, indexable_state::SetId<S>> {
        self._fields.0 = Option::Some(value.into());
        IndexableBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

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

impl<'a, S> IndexableBuilder<'a, S>
where
    S: indexable_state::State,
    S::Name: indexable_state::IsUnset,
{
    /// Set the `name` field (required)
    pub fn name(
        mut self,
        value: impl Into<Data<'a>>,
    ) -> IndexableBuilder<'a, indexable_state::SetName<S>> {
        self._fields.2 = Option::Some(value.into());
        IndexableBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> IndexableBuilder<'a, S>
where
    S: indexable_state::State,
    S::Index: indexable_state::IsSet,
    S::Id: indexable_state::IsSet,
    S::Name: indexable_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> Indexable<'a> {
        Indexable {
            id: self._fields.0.unwrap(),
            index: self._fields.1.unwrap(),
            name: self._fields.2.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, Data<'a>>,
    ) -> Indexable<'a> {
        Indexable {
            id: self._fields.0.unwrap(),
            index: self._fields.1.unwrap(),
            name: self._fields.2.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_dev_tsunagite_types() -> 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("dev.tsunagite.types"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("indexable"),
                LexUserType::Object(LexObject {
                    description: Some(
                        CowStr::new_static(
                            "A named value with a numeric index for sorting.",
                        ),
                    ),
                    required: Some(
                        vec![
                            SmolStr::new_static("id"), SmolStr::new_static("name"),
                            SmolStr::new_static("index")
                        ],
                    ),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("id"),
                            LexObjectProperty::String(LexString {
                                description: Some(
                                    CowStr::new_static(
                                        "The internal ID for the value, limited to the RecordKey character set.",
                                    ),
                                ),
                                format: Some(LexStringFormat::RecordKey),
                                min_length: Some(1usize),
                                max_length: Some(32usize),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("index"),
                            LexObjectProperty::Integer(LexInteger {
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("name"),
                            LexObjectProperty::Ref(LexRef {
                                r#ref: CowStr::new_static("dev.tsunagite.translatable"),
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map.insert(
                SmolStr::new_static("typedRef"),
                LexUserType::Object(LexObject {
                    description: Some(
                        CowStr::new_static(
                            "A typed record reference that does not require a CID hash.",
                        ),
                    ),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("ref"),
                            LexObjectProperty::String(LexString {
                                description: Some(
                                    CowStr::new_static(
                                        "The AT URI of the record this object references.",
                                    ),
                                ),
                                format: Some(LexStringFormat::AtUri),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("type"),
                            LexObjectProperty::String(LexString {
                                description: Some(
                                    CowStr::new_static(
                                        "The type of the record this object references.",
                                    ),
                                ),
                                format: Some(LexStringFormat::RecordKey),
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}