// @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;
use jacquard_common::{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::string::{AtUri, RecordKey, Rkey};
use jacquard_common::types::value::Data;
use jacquard_derive::IntoStatic;
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.

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct Indexable<S: BosStr = DefaultStr> {
    ///The internal ID for the value, limited to the RecordKey character set.
    pub id: RecordKey<Rkey<S>>,
    ///The numeric index used for sorting.
    pub index: i64,
    ///The human-readable name of the value for displaying in UIs.
    pub name: Data<S>,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}

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

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic, Default)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct TypedRef<S: BosStr = DefaultStr> {
    ///The AT URI of the record this object references.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub r#ref: Option<AtUri<S>>,
    ///The type of the record this object references.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub r#type: Option<RecordKey<Rkey<S>>>,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}

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

/// Builder for constructing an instance of this type.
pub struct IndexableBuilder<St: indexable_state::State, S: BosStr = DefaultStr> {
    _state: PhantomData<fn() -> St>,
    _fields: (Option<RecordKey<Rkey<S>>>, Option<i64>, Option<Data<S>>),
    _type: PhantomData<fn() -> S>,
}

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

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

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

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

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

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

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

impl<St, S: BosStr> IndexableBuilder<St, S>
where
    St: indexable_state::State,
    St::Id: indexable_state::IsSet,
    St::Index: indexable_state::IsSet,
    St::Name: indexable_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> Indexable<S> {
        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<SmolStr, Data<S>>,
    ) -> Indexable<S> {
        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()
    }
}