// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: app.offprint.block.heading
//
// 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::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};

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct Heading<S: BosStr = DefaultStr> {
    ///Facets for text formatting
    #[serde(skip_serializing_if = "Option::is_none")]
    pub facets: Option<Vec<Data<S>>>,
    ///Heading level (1-3)
    pub level: i64,
    ///The heading text content
    pub plaintext: S,
    ///Text alignment
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text_align: Option<S>,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}

impl<S: BosStr> LexiconSchema for Heading<S> {
    fn nsid() -> &'static str {
        "app.offprint.block.heading"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_app_offprint_block_heading()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        {
            let value = &self.level;
            if *value > 3i64 {
                return Err(ConstraintError::Maximum {
                    path: ValidationPath::from_field("level"),
                    max: 3i64,
                    actual: *value,
                });
            }
        }
        {
            let value = &self.level;
            if *value < 1i64 {
                return Err(ConstraintError::Minimum {
                    path: ValidationPath::from_field("level"),
                    min: 1i64,
                    actual: *value,
                });
            }
        }
        Ok(())
    }
}

pub mod heading_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 Level;
        type Plaintext;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type Level = Unset;
        type Plaintext = Unset;
    }
    ///State transition - sets the `level` field to Set
    pub struct SetLevel<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetLevel<St> {}
    impl<St: State> State for SetLevel<St> {
        type Level = Set<members::level>;
        type Plaintext = St::Plaintext;
    }
    ///State transition - sets the `plaintext` field to Set
    pub struct SetPlaintext<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetPlaintext<St> {}
    impl<St: State> State for SetPlaintext<St> {
        type Level = St::Level;
        type Plaintext = Set<members::plaintext>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `level` field
        pub struct level(());
        ///Marker type for the `plaintext` field
        pub struct plaintext(());
    }
}

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

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

impl<S: BosStr> Heading<S> {
    /// Create a new builder for this type
    pub fn builder() -> HeadingBuilder<heading_state::Empty, S> {
        HeadingBuilder::builder()
    }
}

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

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

impl<St: heading_state::State, S: BosStr> HeadingBuilder<St, S> {
    /// Set the `facets` field (optional)
    pub fn facets(mut self, value: impl Into<Option<Vec<Data<S>>>>) -> Self {
        self._fields.0 = value.into();
        self
    }
    /// Set the `facets` field to an Option value (optional)
    pub fn maybe_facets(mut self, value: Option<Vec<Data<S>>>) -> Self {
        self._fields.0 = value;
        self
    }
}

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

impl<St, S: BosStr> HeadingBuilder<St, S>
where
    St: heading_state::State,
    St::Plaintext: heading_state::IsUnset,
{
    /// Set the `plaintext` field (required)
    pub fn plaintext(
        mut self,
        value: impl Into<S>,
    ) -> HeadingBuilder<heading_state::SetPlaintext<St>, S> {
        self._fields.2 = Option::Some(value.into());
        HeadingBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St: heading_state::State, S: BosStr> HeadingBuilder<St, S> {
    /// Set the `textAlign` field (optional)
    pub fn text_align(mut self, value: impl Into<Option<S>>) -> Self {
        self._fields.3 = value.into();
        self
    }
    /// Set the `textAlign` field to an Option value (optional)
    pub fn maybe_text_align(mut self, value: Option<S>) -> Self {
        self._fields.3 = value;
        self
    }
}

impl<St, S: BosStr> HeadingBuilder<St, S>
where
    St: heading_state::State,
    St::Level: heading_state::IsSet,
    St::Plaintext: heading_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> Heading<S> {
        Heading {
            facets: self._fields.0,
            level: self._fields.1.unwrap(),
            plaintext: self._fields.2.unwrap(),
            text_align: self._fields.3,
            extra_data: Default::default(),
        }
    }
    /// Build the final struct with custom extra_data.
    pub fn build_with_data(self, extra_data: BTreeMap<SmolStr, Data<S>>) -> Heading<S> {
        Heading {
            facets: self._fields.0,
            level: self._fields.1.unwrap(),
            plaintext: self._fields.2.unwrap(),
            text_align: self._fields.3,
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_app_offprint_block_heading() -> 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("app.offprint.block.heading"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Object(LexObject {
                    required: Some(
                        vec![
                            SmolStr::new_static("plaintext"),
                            SmolStr::new_static("level")
                        ],
                    ),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("facets"),
                            LexObjectProperty::Array(LexArray {
                                description: Some(
                                    CowStr::new_static("Facets for text formatting"),
                                ),
                                items: LexArrayItem::Ref(LexRef {
                                    r#ref: CowStr::new_static("app.offprint.richtext.facet"),
                                    ..Default::default()
                                }),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("level"),
                            LexObjectProperty::Integer(LexInteger {
                                minimum: Some(1i64),
                                maximum: Some(3i64),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("plaintext"),
                            LexObjectProperty::String(LexString {
                                description: Some(
                                    CowStr::new_static("The heading text content"),
                                ),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("textAlign"),
                            LexObjectProperty::String(LexString {
                                description: Some(CowStr::new_static("Text alignment")),
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}