// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: org.atsui.Grid
//
// 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};
use jacquard_common::deps::smol_str::SmolStr;
use jacquard_common::types::value::Data;
use jacquard_derive::IntoStatic;
use serde::{Serialize, Deserialize};
use crate::at_inlay::Response;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct Grid<S: BosStr = DefaultStr> {
    pub children: Data<S>,
    ///Number of equal columns.  Defaults to `3`.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default = "_default_grid_columns")]
    pub columns: Option<i64>,
    ///Space between children.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub gap: Option<GridGap<S>>,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}

/// Space between children.

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum GridGap<S: BosStr = DefaultStr> {
    None,
    Small,
    Medium,
    Large,
    Other(S),
}

impl<S: BosStr> GridGap<S> {
    pub fn as_str(&self) -> &str {
        match self {
            Self::None => "none",
            Self::Small => "small",
            Self::Medium => "medium",
            Self::Large => "large",
            Self::Other(s) => s.as_ref(),
        }
    }
    /// Construct from a string-like value, matching known values.
    pub fn from_value(s: S) -> Self {
        match s.as_ref() {
            "none" => Self::None,
            "small" => Self::Small,
            "medium" => Self::Medium,
            "large" => Self::Large,
            _ => Self::Other(s),
        }
    }
}

impl<S: BosStr> core::fmt::Display for GridGap<S> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl<S: BosStr> AsRef<str> for GridGap<S> {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl<S: BosStr> Serialize for GridGap<S> {
    fn serialize<Ser>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error>
    where
        Ser: serde::Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

impl<'de, S: Deserialize<'de> + BosStr> Deserialize<'de> for GridGap<S> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = S::deserialize(deserializer)?;
        Ok(Self::from_value(s))
    }
}

impl<S: BosStr + Default> Default for GridGap<S> {
    fn default() -> Self {
        Self::Other(Default::default())
    }
}

impl<S: BosStr> jacquard_common::IntoStatic for GridGap<S>
where
    S: BosStr + jacquard_common::IntoStatic,
    S::Output: BosStr,
{
    type Output = GridGap<S::Output>;
    fn into_static(self) -> Self::Output {
        match self {
            GridGap::None => GridGap::None,
            GridGap::Small => GridGap::Small,
            GridGap::Medium => GridGap::Medium,
            GridGap::Large => GridGap::Large,
            GridGap::Other(v) => GridGap::Other(v.into_static()),
        }
    }
}


#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct GridOutput<S: BosStr = DefaultStr> {
    #[serde(flatten)]
    pub value: Response<S>,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}

/** Response marker for the `org.atsui.Grid` procedure.

Implements `jacquard_common::xrpc::XrpcResp`; successful bodies decode as `Self::Output<S>`, which is `GridOutput<S>` for this endpoint.*/
pub struct GridResponse;
impl jacquard_common::xrpc::XrpcResp for GridResponse {
    const NSID: &'static str = "org.atsui.Grid";
    const ENCODING: &'static str = "application/json";
    type Output<S: BosStr> = GridOutput<S>;
    type Err = jacquard_common::xrpc::GenericError;
}

impl<S: BosStr> jacquard_common::xrpc::XrpcRequest for Grid<S> {
    const NSID: &'static str = "org.atsui.Grid";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Response = GridResponse;
}

/** Endpoint marker for the `org.atsui.Grid` procedure.

Path: `/xrpc/org.atsui.Grid`. The request payload type is `Grid<S>`; send that request with `jacquard::Client` or use this marker through lower-level `XrpcEndpoint` APIs.*/
pub struct GridRequest;
impl jacquard_common::xrpc::XrpcEndpoint for GridRequest {
    const PATH: &'static str = "/xrpc/org.atsui.Grid";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Request<S: BosStr> = Grid<S>;
    type Response = GridResponse;
}

fn _default_grid_columns() -> Option<i64> {
    Some(3i64)
}

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

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

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

impl<S: BosStr> Grid<S> {
    /// Create a new builder for this type
    pub fn builder() -> GridBuilder<grid_state::Empty, S> {
        GridBuilder::builder()
    }
}

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

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

impl<St, S: BosStr> GridBuilder<St, S>
where
    St: grid_state::State,
    St::Children: grid_state::IsUnset,
{
    /// Set the `children` field (required)
    pub fn children(
        mut self,
        value: impl Into<Data<S>>,
    ) -> GridBuilder<grid_state::SetChildren<St>, S> {
        self._fields.0 = Option::Some(value.into());
        GridBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St: grid_state::State, S: BosStr> GridBuilder<St, S> {
    /// Set the `columns` field (optional)
    pub fn columns(mut self, value: impl Into<Option<i64>>) -> Self {
        self._fields.1 = value.into();
        self
    }
    /// Set the `columns` field to an Option value (optional)
    pub fn maybe_columns(mut self, value: Option<i64>) -> Self {
        self._fields.1 = value;
        self
    }
}

impl<St: grid_state::State, S: BosStr> GridBuilder<St, S> {
    /// Set the `gap` field (optional)
    pub fn gap(mut self, value: impl Into<Option<GridGap<S>>>) -> Self {
        self._fields.2 = value.into();
        self
    }
    /// Set the `gap` field to an Option value (optional)
    pub fn maybe_gap(mut self, value: Option<GridGap<S>>) -> Self {
        self._fields.2 = value;
        self
    }
}

impl<St, S: BosStr> GridBuilder<St, S>
where
    St: grid_state::State,
    St::Children: grid_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> Grid<S> {
        Grid {
            children: self._fields.0.unwrap(),
            columns: self._fields.1.or_else(|| Some(3i64)),
            gap: self._fields.2,
            extra_data: Default::default(),
        }
    }
    /// Build the final struct with custom extra_data.
    pub fn build_with_data(self, extra_data: BTreeMap<SmolStr, Data<S>>) -> Grid<S> {
        Grid {
            children: self._fields.0.unwrap(),
            columns: self._fields.1.or_else(|| Some(3i64)),
            gap: self._fields.2,
            extra_data: Some(extra_data),
        }
    }
}