// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: at.inlay.pack
//
// 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;

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

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct Export<'a> {
    ///AT-URI of the component record
    #[serde(borrow)]
    pub component: AtUri<'a>,
    ///NSID of the type being exported
    #[serde(borrow)]
    pub r#type: Nsid<'a>,
}

/// A list of type to component exports

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct Pack<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created_at: Option<Datetime>,
    ///Type to component mappings
    #[serde(borrow)]
    pub exports: Vec<pack::Export<'a>>,
    ///Short slug for the pack (e.g. "core", "ui")
    #[serde(borrow)]
    pub name: CowStr<'a>,
}

/// 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 PackGetRecordOutput<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub cid: Option<Cid<'a>>,
    #[serde(borrow)]
    pub uri: AtUri<'a>,
    #[serde(borrow)]
    pub value: Pack<'a>,
}

impl<'a> Pack<'a> {
    pub fn uri(
        uri: impl Into<CowStr<'a>>,
    ) -> Result<RecordUri<'a, PackRecord>, UriError> {
        RecordUri::try_from_uri(AtUri::new_cow(uri.into())?)
    }
}

impl<'a> LexiconSchema for Export<'a> {
    fn nsid() -> &'static str {
        "at.inlay.pack"
    }
    fn def_name() -> &'static str {
        "export"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_at_inlay_pack()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        Ok(())
    }
}

/// Marker type for deserializing records from this collection.

#[derive(Debug, Serialize, Deserialize)]
pub struct PackRecord;
impl XrpcResp for PackRecord {
    const NSID: &'static str = "at.inlay.pack";
    const ENCODING: &'static str = "application/json";
    type Output<'de> = PackGetRecordOutput<'de>;
    type Err<'de> = RecordError<'de>;
}

impl From<PackGetRecordOutput<'_>> for Pack<'_> {
    fn from(output: PackGetRecordOutput<'_>) -> Self {
        use jacquard_common::IntoStatic;
        output.value.into_static()
    }
}

impl Collection for Pack<'_> {
    const NSID: &'static str = "at.inlay.pack";
    type Record = PackRecord;
}

impl Collection for PackRecord {
    const NSID: &'static str = "at.inlay.pack";
    type Record = PackRecord;
}

impl<'a> LexiconSchema for Pack<'a> {
    fn nsid() -> &'static str {
        "at.inlay.pack"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_at_inlay_pack()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        {
            let value = &self.name;
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 64usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("name"),
                    max: 64usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        Ok(())
    }
}

pub mod export_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 Type;
        type Component;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type Type = Unset;
        type Component = Unset;
    }
    ///State transition - sets the `type` field to Set
    pub struct SetType<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetType<S> {}
    impl<S: State> State for SetType<S> {
        type Type = Set<members::r#type>;
        type Component = S::Component;
    }
    ///State transition - sets the `component` field to Set
    pub struct SetComponent<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetComponent<S> {}
    impl<S: State> State for SetComponent<S> {
        type Type = S::Type;
        type Component = Set<members::component>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `type` field
        pub struct r#type(());
        ///Marker type for the `component` field
        pub struct component(());
    }
}

/// Builder for constructing an instance of this type
pub struct ExportBuilder<'a, S: export_state::State> {
    _state: PhantomData<fn() -> S>,
    _fields: (Option<AtUri<'a>>, Option<Nsid<'a>>),
    _lifetime: PhantomData<&'a ()>,
}

impl<'a> Export<'a> {
    /// Create a new builder for this type
    pub fn new() -> ExportBuilder<'a, export_state::Empty> {
        ExportBuilder::new()
    }
}

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

impl<'a, S> ExportBuilder<'a, S>
where
    S: export_state::State,
    S::Component: export_state::IsUnset,
{
    /// Set the `component` field (required)
    pub fn component(
        mut self,
        value: impl Into<AtUri<'a>>,
    ) -> ExportBuilder<'a, export_state::SetComponent<S>> {
        self._fields.0 = Option::Some(value.into());
        ExportBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> ExportBuilder<'a, S>
where
    S: export_state::State,
    S::Type: export_state::IsUnset,
{
    /// Set the `type` field (required)
    pub fn r#type(
        mut self,
        value: impl Into<Nsid<'a>>,
    ) -> ExportBuilder<'a, export_state::SetType<S>> {
        self._fields.1 = Option::Some(value.into());
        ExportBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> ExportBuilder<'a, S>
where
    S: export_state::State,
    S::Type: export_state::IsSet,
    S::Component: export_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> Export<'a> {
        Export {
            component: self._fields.0.unwrap(),
            r#type: 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<
            jacquard_common::deps::smol_str::SmolStr,
            jacquard_common::types::value::Data<'a>,
        >,
    ) -> Export<'a> {
        Export {
            component: self._fields.0.unwrap(),
            r#type: self._fields.1.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_at_inlay_pack() -> 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("at.inlay.pack"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("export"),
                LexUserType::Object(LexObject {
                    required: Some(
                        vec![
                            SmolStr::new_static("type"), SmolStr::new_static("component")
                        ],
                    ),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("component"),
                            LexObjectProperty::String(LexString {
                                description: Some(
                                    CowStr::new_static("AT-URI of the component record"),
                                ),
                                format: Some(LexStringFormat::AtUri),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("type"),
                            LexObjectProperty::String(LexString {
                                description: Some(
                                    CowStr::new_static("NSID of the type being exported"),
                                ),
                                format: Some(LexStringFormat::Nsid),
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Record(LexRecord {
                    description: Some(
                        CowStr::new_static("A list of type to component exports"),
                    ),
                    key: Some(CowStr::new_static("tid")),
                    record: LexRecordRecord::Object(LexObject {
                        required: Some(
                            vec![
                                SmolStr::new_static("name"), SmolStr::new_static("exports")
                            ],
                        ),
                        properties: {
                            #[allow(unused_mut)]
                            let mut map = BTreeMap::new();
                            map.insert(
                                SmolStr::new_static("createdAt"),
                                LexObjectProperty::String(LexString {
                                    format: Some(LexStringFormat::Datetime),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("exports"),
                                LexObjectProperty::Array(LexArray {
                                    description: Some(
                                        CowStr::new_static("Type to component mappings"),
                                    ),
                                    items: LexArrayItem::Ref(LexRef {
                                        r#ref: CowStr::new_static("#export"),
                                        ..Default::default()
                                    }),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("name"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "Short slug for the pack (e.g. \"core\", \"ui\")",
                                        ),
                                    ),
                                    max_length: Some(64usize),
                                    ..Default::default()
                                }),
                            );
                            map
                        },
                        ..Default::default()
                    }),
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}

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

/// Builder for constructing an instance of this type
pub struct PackBuilder<'a, S: pack_state::State> {
    _state: PhantomData<fn() -> S>,
    _fields: (Option<Datetime>, Option<Vec<pack::Export<'a>>>, Option<CowStr<'a>>),
    _lifetime: PhantomData<&'a ()>,
}

impl<'a> Pack<'a> {
    /// Create a new builder for this type
    pub fn new() -> PackBuilder<'a, pack_state::Empty> {
        PackBuilder::new()
    }
}

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

impl<'a, S: pack_state::State> PackBuilder<'a, S> {
    /// Set the `createdAt` field (optional)
    pub fn created_at(mut self, value: impl Into<Option<Datetime>>) -> Self {
        self._fields.0 = value.into();
        self
    }
    /// Set the `createdAt` field to an Option value (optional)
    pub fn maybe_created_at(mut self, value: Option<Datetime>) -> Self {
        self._fields.0 = value;
        self
    }
}

impl<'a, S> PackBuilder<'a, S>
where
    S: pack_state::State,
    S::Exports: pack_state::IsUnset,
{
    /// Set the `exports` field (required)
    pub fn exports(
        mut self,
        value: impl Into<Vec<pack::Export<'a>>>,
    ) -> PackBuilder<'a, pack_state::SetExports<S>> {
        self._fields.1 = Option::Some(value.into());
        PackBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

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

impl<'a, S> PackBuilder<'a, S>
where
    S: pack_state::State,
    S::Exports: pack_state::IsSet,
    S::Name: pack_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> Pack<'a> {
        Pack {
            created_at: self._fields.0,
            exports: 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,
            jacquard_common::types::value::Data<'a>,
        >,
    ) -> Pack<'a> {
        Pack {
            created_at: self._fields.0,
            exports: self._fields.1.unwrap(),
            name: self._fields.2.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}