// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: pub.leaflet.blocks.website
//
// 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::blob::BlobRef;
use jacquard_common::types::string::UriValue;
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 Website<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<S>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub preview_image: Option<BlobRef<S>>,
    pub src: UriValue<S>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<S>,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}

impl<S: BosStr> LexiconSchema for Website<S> {
    fn nsid() -> &'static str {
        "pub.leaflet.blocks.website"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_pub_leaflet_blocks_website()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        if let Some(ref value) = self.preview_image {
            {
                let size = value.blob().size;
                if size > 1000000usize {
                    return Err(ConstraintError::BlobTooLarge {
                        path: ValidationPath::from_field("preview_image"),
                        max: 1000000usize,
                        actual: size,
                    });
                }
            }
        }
        if let Some(ref value) = self.preview_image {
            {
                let mime = value.blob().mime_type.as_str();
                let accepted: &[&str] = &["image/*"];
                let matched = accepted
                    .iter()
                    .any(|pattern| {
                        if *pattern == "*/*" {
                            true
                        } else if pattern.ends_with("/*") {
                            let prefix = &pattern[..pattern.len() - 2];
                            mime.starts_with(prefix)
                                && mime.as_bytes().get(prefix.len()) == Some(&b'/')
                        } else {
                            mime == *pattern
                        }
                    });
                if !matched {
                    return Err(ConstraintError::BlobMimeTypeNotAccepted {
                        path: ValidationPath::from_field("preview_image"),
                        accepted: vec!["image/*".to_string()],
                        actual: mime.to_string(),
                    });
                }
            }
        }
        Ok(())
    }
}

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

/// Builder for constructing an instance of this type.
pub struct WebsiteBuilder<St: website_state::State, S: BosStr = DefaultStr> {
    _state: PhantomData<fn() -> St>,
    _fields: (Option<S>, Option<BlobRef<S>>, Option<UriValue<S>>, Option<S>),
    _type: PhantomData<fn() -> S>,
}

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

impl<S: BosStr> Website<S> {
    /// Create a new builder for this type
    pub fn builder() -> WebsiteBuilder<website_state::Empty, S> {
        WebsiteBuilder::builder()
    }
}

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

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

impl<St: website_state::State, S: BosStr> WebsiteBuilder<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: website_state::State, S: BosStr> WebsiteBuilder<St, S> {
    /// Set the `previewImage` field (optional)
    pub fn preview_image(mut self, value: impl Into<Option<BlobRef<S>>>) -> Self {
        self._fields.1 = value.into();
        self
    }
    /// Set the `previewImage` field to an Option value (optional)
    pub fn maybe_preview_image(mut self, value: Option<BlobRef<S>>) -> Self {
        self._fields.1 = value;
        self
    }
}

impl<St, S: BosStr> WebsiteBuilder<St, S>
where
    St: website_state::State,
    St::Src: website_state::IsUnset,
{
    /// Set the `src` field (required)
    pub fn src(
        mut self,
        value: impl Into<UriValue<S>>,
    ) -> WebsiteBuilder<website_state::SetSrc<St>, S> {
        self._fields.2 = Option::Some(value.into());
        WebsiteBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

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

impl<St, S: BosStr> WebsiteBuilder<St, S>
where
    St: website_state::State,
    St::Src: website_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> Website<S> {
        Website {
            description: self._fields.0,
            preview_image: self._fields.1,
            src: self._fields.2.unwrap(),
            title: 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>>) -> Website<S> {
        Website {
            description: self._fields.0,
            preview_image: self._fields.1,
            src: self._fields.2.unwrap(),
            title: self._fields.3,
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_pub_leaflet_blocks_website() -> 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("pub.leaflet.blocks.website"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Object(LexObject {
                    required: Some(vec![SmolStr::new_static("src")]),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("description"),
                            LexObjectProperty::String(LexString { ..Default::default() }),
                        );
                        map.insert(
                            SmolStr::new_static("previewImage"),
                            LexObjectProperty::Blob(LexBlob { ..Default::default() }),
                        );
                        map.insert(
                            SmolStr::new_static("src"),
                            LexObjectProperty::String(LexString {
                                format: Some(LexStringFormat::Uri),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("title"),
                            LexObjectProperty::String(LexString { ..Default::default() }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}