{-# LANGUAGE TemplateHaskell #-}

-- | Message components
module Calamity.Types.Model.Channel.Component (
  CustomID (..),
  Component (..),
  Button (..),
  LinkButton (..),
  button,
  button',
  lbutton,
  lbutton',
  ButtonStyle (..),
  Select (..),
  select,
  SelectOption (..),
  sopt,
  TextInput (..),
  TextInputStyle (..),
  textInput,
  ComponentType (..),
  componentType,
) where

import Calamity.Internal.Utils (CalamityToJSON (..), CalamityToJSON' (..), (.=), (.?=))
import Calamity.Types.Model.Guild.Emoji
import Control.Monad (replicateM)
import Data.Aeson ((.!=), (.:), (.:?))
import qualified Data.Aeson as Aeson
import Data.Scientific (toBoundedInteger)
import qualified Data.Text as T
import Optics.TH
import System.Random (Uniform)
import System.Random.Stateful (Uniform (uniformM), UniformRange (uniformRM))
import TextShow.TH
import Data.Maybe (catMaybes)

newtype CustomID = CustomID T.Text
  deriving stock (CustomID -> CustomID -> Bool
(CustomID -> CustomID -> Bool)
-> (CustomID -> CustomID -> Bool) -> Eq CustomID
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CustomID -> CustomID -> Bool
$c/= :: CustomID -> CustomID -> Bool
== :: CustomID -> CustomID -> Bool
$c== :: CustomID -> CustomID -> Bool
Eq, Eq CustomID
Eq CustomID
-> (CustomID -> CustomID -> Ordering)
-> (CustomID -> CustomID -> Bool)
-> (CustomID -> CustomID -> Bool)
-> (CustomID -> CustomID -> Bool)
-> (CustomID -> CustomID -> Bool)
-> (CustomID -> CustomID -> CustomID)
-> (CustomID -> CustomID -> CustomID)
-> Ord CustomID
CustomID -> CustomID -> Bool
CustomID -> CustomID -> Ordering
CustomID -> CustomID -> CustomID
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: CustomID -> CustomID -> CustomID
$cmin :: CustomID -> CustomID -> CustomID
max :: CustomID -> CustomID -> CustomID
$cmax :: CustomID -> CustomID -> CustomID
>= :: CustomID -> CustomID -> Bool
$c>= :: CustomID -> CustomID -> Bool
> :: CustomID -> CustomID -> Bool
$c> :: CustomID -> CustomID -> Bool
<= :: CustomID -> CustomID -> Bool
$c<= :: CustomID -> CustomID -> Bool
< :: CustomID -> CustomID -> Bool
$c< :: CustomID -> CustomID -> Bool
compare :: CustomID -> CustomID -> Ordering
$ccompare :: CustomID -> CustomID -> Ordering
$cp1Ord :: Eq CustomID
Ord, Int -> CustomID -> ShowS
[CustomID] -> ShowS
CustomID -> String
(Int -> CustomID -> ShowS)
-> (CustomID -> String) -> ([CustomID] -> ShowS) -> Show CustomID
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CustomID] -> ShowS
$cshowList :: [CustomID] -> ShowS
show :: CustomID -> String
$cshow :: CustomID -> String
showsPrec :: Int -> CustomID -> ShowS
$cshowsPrec :: Int -> CustomID -> ShowS
Show)
  deriving ([CustomID] -> Encoding
[CustomID] -> Value
CustomID -> Encoding
CustomID -> Value
(CustomID -> Value)
-> (CustomID -> Encoding)
-> ([CustomID] -> Value)
-> ([CustomID] -> Encoding)
-> ToJSON CustomID
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [CustomID] -> Encoding
$ctoEncodingList :: [CustomID] -> Encoding
toJSONList :: [CustomID] -> Value
$ctoJSONList :: [CustomID] -> Value
toEncoding :: CustomID -> Encoding
$ctoEncoding :: CustomID -> Encoding
toJSON :: CustomID -> Value
$ctoJSON :: CustomID -> Value
Aeson.ToJSON, Value -> Parser [CustomID]
Value -> Parser CustomID
(Value -> Parser CustomID)
-> (Value -> Parser [CustomID]) -> FromJSON CustomID
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [CustomID]
$cparseJSONList :: Value -> Parser [CustomID]
parseJSON :: Value -> Parser CustomID
$cparseJSON :: Value -> Parser CustomID
Aeson.FromJSON) via T.Text

$(deriveTextShow ''CustomID)

instance Uniform CustomID where
  uniformM :: g -> m CustomID
uniformM = ((Text -> CustomID
CustomID (Text -> CustomID) -> (String -> Text) -> String -> CustomID
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack) (String -> CustomID) -> m String -> m CustomID
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) (m String -> m CustomID) -> (g -> m String) -> g -> m CustomID
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> m Char -> m String
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
16 (m Char -> m String) -> (g -> m Char) -> g -> m String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char, Char) -> g -> m Char
forall a g (m :: * -> *).
(UniformRange a, StatefulGen g m) =>
(a, a) -> g -> m a
uniformRM (Char
'a', Char
'z')

data ComponentType
  = ActionRowType
  | ButtonType
  | SelectType
  | TextInputType
  deriving (ComponentType -> ComponentType -> Bool
(ComponentType -> ComponentType -> Bool)
-> (ComponentType -> ComponentType -> Bool) -> Eq ComponentType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ComponentType -> ComponentType -> Bool
$c/= :: ComponentType -> ComponentType -> Bool
== :: ComponentType -> ComponentType -> Bool
$c== :: ComponentType -> ComponentType -> Bool
Eq, Int -> ComponentType -> ShowS
[ComponentType] -> ShowS
ComponentType -> String
(Int -> ComponentType -> ShowS)
-> (ComponentType -> String)
-> ([ComponentType] -> ShowS)
-> Show ComponentType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ComponentType] -> ShowS
$cshowList :: [ComponentType] -> ShowS
show :: ComponentType -> String
$cshow :: ComponentType -> String
showsPrec :: Int -> ComponentType -> ShowS
$cshowsPrec :: Int -> ComponentType -> ShowS
Show)

$(deriveTextShow ''ComponentType)

instance Aeson.ToJSON ComponentType where
  toJSON :: ComponentType -> Value
toJSON ComponentType
x = ToJSON Int => Int -> Value
forall a. ToJSON a => a -> Value
Aeson.toJSON @Int (Int -> Value) -> Int -> Value
forall a b. (a -> b) -> a -> b
$ case ComponentType
x of
    ComponentType
ActionRowType -> Int
1
    ComponentType
ButtonType -> Int
2
    ComponentType
SelectType -> Int
3
    ComponentType
TextInputType -> Int
4
  toEncoding :: ComponentType -> Encoding
toEncoding ComponentType
x = ToJSON Int => Int -> Encoding
forall a. ToJSON a => a -> Encoding
Aeson.toEncoding @Int (Int -> Encoding) -> Int -> Encoding
forall a b. (a -> b) -> a -> b
$ case ComponentType
x of
    ComponentType
ActionRowType -> Int
1
    ComponentType
ButtonType -> Int
2
    ComponentType
SelectType -> Int
3
    ComponentType
TextInputType -> Int
4

instance Aeson.FromJSON ComponentType where
  parseJSON :: Value -> Parser ComponentType
parseJSON = String
-> (Scientific -> Parser ComponentType)
-> Value
-> Parser ComponentType
forall a. String -> (Scientific -> Parser a) -> Value -> Parser a
Aeson.withScientific String
"Components.ComponentType" ((Scientific -> Parser ComponentType)
 -> Value -> Parser ComponentType)
-> (Scientific -> Parser ComponentType)
-> Value
-> Parser ComponentType
forall a b. (a -> b) -> a -> b
$ \Scientific
n -> case Scientific -> Maybe Int
forall i. (Integral i, Bounded i) => Scientific -> Maybe i
toBoundedInteger @Int Scientific
n of
    Just Int
1 -> ComponentType -> Parser ComponentType
forall (f :: * -> *) a. Applicative f => a -> f a
pure ComponentType
ActionRowType
    Just Int
2 -> ComponentType -> Parser ComponentType
forall (f :: * -> *) a. Applicative f => a -> f a
pure ComponentType
ButtonType
    Just Int
3 -> ComponentType -> Parser ComponentType
forall (f :: * -> *) a. Applicative f => a -> f a
pure ComponentType
SelectType
    Just Int
4 -> ComponentType -> Parser ComponentType
forall (f :: * -> *) a. Applicative f => a -> f a
pure ComponentType
TextInputType
    Maybe Int
_ -> String -> Parser ComponentType
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser ComponentType) -> String -> Parser ComponentType
forall a b. (a -> b) -> a -> b
$ String
"Invalid ComponentType: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Scientific -> String
forall a. Show a => a -> String
show Scientific
n

data ButtonStyle
  = ButtonPrimary
  | ButtonSecondary
  | ButtonSuccess
  | ButtonDanger
  | ButtonLink
  deriving (ButtonStyle -> ButtonStyle -> Bool
(ButtonStyle -> ButtonStyle -> Bool)
-> (ButtonStyle -> ButtonStyle -> Bool) -> Eq ButtonStyle
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ButtonStyle -> ButtonStyle -> Bool
$c/= :: ButtonStyle -> ButtonStyle -> Bool
== :: ButtonStyle -> ButtonStyle -> Bool
$c== :: ButtonStyle -> ButtonStyle -> Bool
Eq, Int -> ButtonStyle -> ShowS
[ButtonStyle] -> ShowS
ButtonStyle -> String
(Int -> ButtonStyle -> ShowS)
-> (ButtonStyle -> String)
-> ([ButtonStyle] -> ShowS)
-> Show ButtonStyle
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ButtonStyle] -> ShowS
$cshowList :: [ButtonStyle] -> ShowS
show :: ButtonStyle -> String
$cshow :: ButtonStyle -> String
showsPrec :: Int -> ButtonStyle -> ShowS
$cshowsPrec :: Int -> ButtonStyle -> ShowS
Show)

$(deriveTextShow ''ButtonStyle)

instance Aeson.ToJSON ButtonStyle where
  toJSON :: ButtonStyle -> Value
toJSON ButtonStyle
t = ToJSON Int => Int -> Value
forall a. ToJSON a => a -> Value
Aeson.toJSON @Int (Int -> Value) -> Int -> Value
forall a b. (a -> b) -> a -> b
$ case ButtonStyle
t of
    ButtonStyle
ButtonPrimary -> Int
1
    ButtonStyle
ButtonSecondary -> Int
2
    ButtonStyle
ButtonSuccess -> Int
3
    ButtonStyle
ButtonDanger -> Int
4
    ButtonStyle
ButtonLink -> Int
5
  toEncoding :: ButtonStyle -> Encoding
toEncoding ButtonStyle
t = ToJSON Int => Int -> Encoding
forall a. ToJSON a => a -> Encoding
Aeson.toEncoding @Int (Int -> Encoding) -> Int -> Encoding
forall a b. (a -> b) -> a -> b
$ case ButtonStyle
t of
    ButtonStyle
ButtonPrimary -> Int
1
    ButtonStyle
ButtonSecondary -> Int
2
    ButtonStyle
ButtonSuccess -> Int
3
    ButtonStyle
ButtonDanger -> Int
4
    ButtonStyle
ButtonLink -> Int
5

instance Aeson.FromJSON ButtonStyle where
  parseJSON :: Value -> Parser ButtonStyle
parseJSON = String
-> (Scientific -> Parser ButtonStyle)
-> Value
-> Parser ButtonStyle
forall a. String -> (Scientific -> Parser a) -> Value -> Parser a
Aeson.withScientific String
"Components.ButtonStyle" ((Scientific -> Parser ButtonStyle) -> Value -> Parser ButtonStyle)
-> (Scientific -> Parser ButtonStyle)
-> Value
-> Parser ButtonStyle
forall a b. (a -> b) -> a -> b
$ \Scientific
n -> case Scientific -> Maybe Int
forall i. (Integral i, Bounded i) => Scientific -> Maybe i
toBoundedInteger @Int Scientific
n of
    Just Int
v -> case Int
v of
      Int
1 -> ButtonStyle -> Parser ButtonStyle
forall (f :: * -> *) a. Applicative f => a -> f a
pure ButtonStyle
ButtonPrimary
      Int
2 -> ButtonStyle -> Parser ButtonStyle
forall (f :: * -> *) a. Applicative f => a -> f a
pure ButtonStyle
ButtonSecondary
      Int
3 -> ButtonStyle -> Parser ButtonStyle
forall (f :: * -> *) a. Applicative f => a -> f a
pure ButtonStyle
ButtonSuccess
      Int
4 -> ButtonStyle -> Parser ButtonStyle
forall (f :: * -> *) a. Applicative f => a -> f a
pure ButtonStyle
ButtonDanger
      Int
5 -> ButtonStyle -> Parser ButtonStyle
forall (f :: * -> *) a. Applicative f => a -> f a
pure ButtonStyle
ButtonLink
      Int
_ -> String -> Parser ButtonStyle
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser ButtonStyle) -> String -> Parser ButtonStyle
forall a b. (a -> b) -> a -> b
$ String
"Invalid ButtonStyle: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Scientific -> String
forall a. Show a => a -> String
show Scientific
n
    Maybe Int
Nothing -> String -> Parser ButtonStyle
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser ButtonStyle) -> String -> Parser ButtonStyle
forall a b. (a -> b) -> a -> b
$ String
"Invalid ButtonStyle: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Scientific -> String
forall a. Show a => a -> String
show Scientific
n

data Button = Button
  { Button -> ButtonStyle
style :: ButtonStyle
  , Button -> Maybe Text
label :: Maybe T.Text
  , Button -> Maybe RawEmoji
emoji :: Maybe RawEmoji
  , Button -> Bool
disabled :: Bool
  , Button -> CustomID
customID :: CustomID
  }
  deriving (Int -> Button -> ShowS
[Button] -> ShowS
Button -> String
(Int -> Button -> ShowS)
-> (Button -> String) -> ([Button] -> ShowS) -> Show Button
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Button] -> ShowS
$cshowList :: [Button] -> ShowS
show :: Button -> String
$cshow :: Button -> String
showsPrec :: Int -> Button -> ShowS
$cshowsPrec :: Int -> Button -> ShowS
Show)
  deriving ([Button] -> Encoding
[Button] -> Value
Button -> Encoding
Button -> Value
(Button -> Value)
-> (Button -> Encoding)
-> ([Button] -> Value)
-> ([Button] -> Encoding)
-> ToJSON Button
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [Button] -> Encoding
$ctoEncodingList :: [Button] -> Encoding
toJSONList :: [Button] -> Value
$ctoJSONList :: [Button] -> Value
toEncoding :: Button -> Encoding
$ctoEncoding :: Button -> Encoding
toJSON :: Button -> Value
$ctoJSON :: Button -> Value
Aeson.ToJSON) via CalamityToJSON Button

instance CalamityToJSON' Button where
  toPairs :: Button -> [Maybe kv]
toPairs Button {Bool
Maybe Text
Maybe RawEmoji
CustomID
ButtonStyle
customID :: CustomID
disabled :: Bool
emoji :: Maybe RawEmoji
label :: Maybe Text
style :: ButtonStyle
$sel:customID:Button :: Button -> CustomID
$sel:disabled:Button :: Button -> Bool
$sel:emoji:Button :: Button -> Maybe RawEmoji
$sel:label:Button :: Button -> Maybe Text
$sel:style:Button :: Button -> ButtonStyle
..} =
    [ Key
"style" Key -> ButtonStyle -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= ButtonStyle
style
    , Key
"label" Key -> Maybe Text -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> Maybe v -> Maybe kv
.?= Maybe Text
label
    , Key
"emoji" Key -> Maybe RawEmoji -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> Maybe v -> Maybe kv
.?= Maybe RawEmoji
emoji
    , Key
"disabled" Key -> Bool -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= Bool
disabled
    , Key
"custom_id" Key -> CustomID -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= CustomID
customID
    , Key
"type" Key -> ComponentType -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= ComponentType
ButtonType
    ]

$(deriveTextShow ''Button)
$(makeFieldLabelsNoPrefix ''Button)


instance Aeson.FromJSON Button where
  parseJSON :: Value -> Parser Button
parseJSON = String -> (Object -> Parser Button) -> Value -> Parser Button
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Aeson.withObject String
"Components.Button" ((Object -> Parser Button) -> Value -> Parser Button)
-> (Object -> Parser Button) -> Value -> Parser Button
forall a b. (a -> b) -> a -> b
$ \Object
v ->
    ButtonStyle
-> Maybe Text -> Maybe RawEmoji -> Bool -> CustomID -> Button
Button
      (ButtonStyle
 -> Maybe Text -> Maybe RawEmoji -> Bool -> CustomID -> Button)
-> Parser ButtonStyle
-> Parser
     (Maybe Text -> Maybe RawEmoji -> Bool -> CustomID -> Button)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Key -> Parser ButtonStyle
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"style"
      Parser (Maybe Text -> Maybe RawEmoji -> Bool -> CustomID -> Button)
-> Parser (Maybe Text)
-> Parser (Maybe RawEmoji -> Bool -> CustomID -> Button)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"label"
      Parser (Maybe RawEmoji -> Bool -> CustomID -> Button)
-> Parser (Maybe RawEmoji) -> Parser (Bool -> CustomID -> Button)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe RawEmoji)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"emoji"
      Parser (Bool -> CustomID -> Button)
-> Parser Bool -> Parser (CustomID -> Button)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"disabled" Parser (Maybe Bool) -> Bool -> Parser Bool
forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
False
      Parser (CustomID -> Button) -> Parser CustomID -> Parser Button
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser CustomID
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"custom_id"

data LinkButton = LinkButton
  { LinkButton -> ButtonStyle
style :: ButtonStyle
  , LinkButton -> Maybe Text
label :: Maybe T.Text
  , LinkButton -> Maybe RawEmoji
emoji :: Maybe RawEmoji
  , LinkButton -> Text
url :: T.Text
  , LinkButton -> Bool
disabled :: Bool
  }
  deriving (Int -> LinkButton -> ShowS
[LinkButton] -> ShowS
LinkButton -> String
(Int -> LinkButton -> ShowS)
-> (LinkButton -> String)
-> ([LinkButton] -> ShowS)
-> Show LinkButton
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [LinkButton] -> ShowS
$cshowList :: [LinkButton] -> ShowS
show :: LinkButton -> String
$cshow :: LinkButton -> String
showsPrec :: Int -> LinkButton -> ShowS
$cshowsPrec :: Int -> LinkButton -> ShowS
Show)
  deriving ([LinkButton] -> Encoding
[LinkButton] -> Value
LinkButton -> Encoding
LinkButton -> Value
(LinkButton -> Value)
-> (LinkButton -> Encoding)
-> ([LinkButton] -> Value)
-> ([LinkButton] -> Encoding)
-> ToJSON LinkButton
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [LinkButton] -> Encoding
$ctoEncodingList :: [LinkButton] -> Encoding
toJSONList :: [LinkButton] -> Value
$ctoJSONList :: [LinkButton] -> Value
toEncoding :: LinkButton -> Encoding
$ctoEncoding :: LinkButton -> Encoding
toJSON :: LinkButton -> Value
$ctoJSON :: LinkButton -> Value
Aeson.ToJSON) via CalamityToJSON LinkButton

instance CalamityToJSON' LinkButton where
  toPairs :: LinkButton -> [Maybe kv]
toPairs LinkButton {Bool
Maybe Text
Maybe RawEmoji
Text
ButtonStyle
disabled :: Bool
url :: Text
emoji :: Maybe RawEmoji
label :: Maybe Text
style :: ButtonStyle
$sel:disabled:LinkButton :: LinkButton -> Bool
$sel:url:LinkButton :: LinkButton -> Text
$sel:emoji:LinkButton :: LinkButton -> Maybe RawEmoji
$sel:label:LinkButton :: LinkButton -> Maybe Text
$sel:style:LinkButton :: LinkButton -> ButtonStyle
..} =
      [ Key
"style" Key -> ButtonStyle -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= ButtonStyle
style
      , Key
"label" Key -> Maybe Text -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> Maybe v -> Maybe kv
.?= Maybe Text
label
      , Key
"emoji" Key -> Maybe RawEmoji -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> Maybe v -> Maybe kv
.?= Maybe RawEmoji
emoji
      , Key
"url" Key -> Text -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= Text
url
      , Key
"disabled" Key -> Bool -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= Bool
disabled
      , Key
"type" Key -> ComponentType -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= ComponentType
ButtonType
      ]

instance Aeson.FromJSON LinkButton where
  parseJSON :: Value -> Parser LinkButton
parseJSON = String
-> (Object -> Parser LinkButton) -> Value -> Parser LinkButton
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Aeson.withObject String
"Components.Linkbutton" ((Object -> Parser LinkButton) -> Value -> Parser LinkButton)
-> (Object -> Parser LinkButton) -> Value -> Parser LinkButton
forall a b. (a -> b) -> a -> b
$ \Object
v ->
    ButtonStyle
-> Maybe Text -> Maybe RawEmoji -> Text -> Bool -> LinkButton
LinkButton
      (ButtonStyle
 -> Maybe Text -> Maybe RawEmoji -> Text -> Bool -> LinkButton)
-> Parser ButtonStyle
-> Parser
     (Maybe Text -> Maybe RawEmoji -> Text -> Bool -> LinkButton)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Key -> Parser ButtonStyle
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"style"
      Parser (Maybe Text -> Maybe RawEmoji -> Text -> Bool -> LinkButton)
-> Parser (Maybe Text)
-> Parser (Maybe RawEmoji -> Text -> Bool -> LinkButton)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"label"
      Parser (Maybe RawEmoji -> Text -> Bool -> LinkButton)
-> Parser (Maybe RawEmoji) -> Parser (Text -> Bool -> LinkButton)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe RawEmoji)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"emoji"
      Parser (Text -> Bool -> LinkButton)
-> Parser Text -> Parser (Bool -> LinkButton)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"url"
      Parser (Bool -> LinkButton) -> Parser Bool -> Parser LinkButton
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"disabled" Parser (Maybe Bool) -> Bool -> Parser Bool
forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
False

$(deriveTextShow ''LinkButton)
$(makeFieldLabelsNoPrefix ''LinkButton)

{- | Constuct a non-disabled 'Button' with the given 'ButtonStyle' and 'CustomID',
 all other fields are set to 'Nothing'
-}
button :: ButtonStyle -> CustomID -> Button
button :: ButtonStyle -> CustomID -> Button
button ButtonStyle
s = ButtonStyle
-> Maybe Text -> Maybe RawEmoji -> Bool -> CustomID -> Button
Button ButtonStyle
s Maybe Text
forall a. Maybe a
Nothing Maybe RawEmoji
forall a. Maybe a
Nothing Bool
False

{- | Constuct a non-disabled 'Button' with the given 'ButtonStyle', 'CustomID',
 and label, all other fields are set to 'Nothing'
-}
button' :: ButtonStyle -> T.Text -> CustomID -> Button
button' :: ButtonStyle -> Text -> CustomID -> Button
button' ButtonStyle
s Text
l = ButtonStyle
-> Maybe Text -> Maybe RawEmoji -> Bool -> CustomID -> Button
Button ButtonStyle
s (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
l) Maybe RawEmoji
forall a. Maybe a
Nothing Bool
False

{- | Constuct a non-disabled 'LinkButton' with the given 'ButtonStyle', link, all
   other fields are set to 'Nothing'
-}
lbutton ::
  ButtonStyle ->
  -- | The link to use
  T.Text ->
  LinkButton
lbutton :: ButtonStyle -> Text -> LinkButton
lbutton ButtonStyle
s Text
lnk = ButtonStyle
-> Maybe Text -> Maybe RawEmoji -> Text -> Bool -> LinkButton
LinkButton ButtonStyle
s Maybe Text
forall a. Maybe a
Nothing Maybe RawEmoji
forall a. Maybe a
Nothing Text
lnk Bool
False

{- | Constuct a non-disabled 'LinkButton' with the given 'ButtonStyle', link,
 and label, all other fields are set to 'Nothing'
-}
lbutton' ::
  ButtonStyle ->
  -- | The link to use
  T.Text ->
  -- | The label to use
  T.Text ->
  LinkButton
lbutton' :: ButtonStyle -> Text -> Text -> LinkButton
lbutton' ButtonStyle
s Text
lnk Text
lbl = ButtonStyle
-> Maybe Text -> Maybe RawEmoji -> Text -> Bool -> LinkButton
LinkButton ButtonStyle
s (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
lbl) Maybe RawEmoji
forall a. Maybe a
Nothing Text
lnk Bool
False

data SelectOption = SelectOption
  { SelectOption -> Text
label :: T.Text
  , SelectOption -> Text
value :: T.Text
  , SelectOption -> Maybe Text
description :: Maybe T.Text
  , SelectOption -> Maybe RawEmoji
emoji :: Maybe RawEmoji
  , SelectOption -> Bool
default_ :: Bool
  }
  deriving (Int -> SelectOption -> ShowS
[SelectOption] -> ShowS
SelectOption -> String
(Int -> SelectOption -> ShowS)
-> (SelectOption -> String)
-> ([SelectOption] -> ShowS)
-> Show SelectOption
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SelectOption] -> ShowS
$cshowList :: [SelectOption] -> ShowS
show :: SelectOption -> String
$cshow :: SelectOption -> String
showsPrec :: Int -> SelectOption -> ShowS
$cshowsPrec :: Int -> SelectOption -> ShowS
Show)
  deriving ([SelectOption] -> Encoding
[SelectOption] -> Value
SelectOption -> Encoding
SelectOption -> Value
(SelectOption -> Value)
-> (SelectOption -> Encoding)
-> ([SelectOption] -> Value)
-> ([SelectOption] -> Encoding)
-> ToJSON SelectOption
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [SelectOption] -> Encoding
$ctoEncodingList :: [SelectOption] -> Encoding
toJSONList :: [SelectOption] -> Value
$ctoJSONList :: [SelectOption] -> Value
toEncoding :: SelectOption -> Encoding
$ctoEncoding :: SelectOption -> Encoding
toJSON :: SelectOption -> Value
$ctoJSON :: SelectOption -> Value
Aeson.ToJSON) via CalamityToJSON SelectOption

instance CalamityToJSON' SelectOption where
  toPairs :: SelectOption -> [Maybe kv]
toPairs SelectOption {Bool
Maybe Text
Maybe RawEmoji
Text
default_ :: Bool
emoji :: Maybe RawEmoji
description :: Maybe Text
value :: Text
label :: Text
$sel:default_:SelectOption :: SelectOption -> Bool
$sel:emoji:SelectOption :: SelectOption -> Maybe RawEmoji
$sel:description:SelectOption :: SelectOption -> Maybe Text
$sel:value:SelectOption :: SelectOption -> Text
$sel:label:SelectOption :: SelectOption -> Text
..} =
      [ Key
"label" Key -> Text -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= Text
label
      , Key
"value" Key -> Text -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= Text
value
      , Key
"description" Key -> Maybe Text -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> Maybe v -> Maybe kv
.?= Maybe Text
description
      , Key
"emoji" Key -> Maybe RawEmoji -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> Maybe v -> Maybe kv
.?= Maybe RawEmoji
emoji
      , Key
"default" Key -> Bool -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= Bool
default_
      ]

instance Aeson.FromJSON SelectOption where
  parseJSON :: Value -> Parser SelectOption
parseJSON = String
-> (Object -> Parser SelectOption) -> Value -> Parser SelectOption
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Aeson.withObject String
"Components.SelectOption" ((Object -> Parser SelectOption) -> Value -> Parser SelectOption)
-> (Object -> Parser SelectOption) -> Value -> Parser SelectOption
forall a b. (a -> b) -> a -> b
$ \Object
v ->
    Text
-> Text -> Maybe Text -> Maybe RawEmoji -> Bool -> SelectOption
SelectOption
      (Text
 -> Text -> Maybe Text -> Maybe RawEmoji -> Bool -> SelectOption)
-> Parser Text
-> Parser
     (Text -> Maybe Text -> Maybe RawEmoji -> Bool -> SelectOption)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"label"
      Parser
  (Text -> Maybe Text -> Maybe RawEmoji -> Bool -> SelectOption)
-> Parser Text
-> Parser (Maybe Text -> Maybe RawEmoji -> Bool -> SelectOption)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"value"
      Parser (Maybe Text -> Maybe RawEmoji -> Bool -> SelectOption)
-> Parser (Maybe Text)
-> Parser (Maybe RawEmoji -> Bool -> SelectOption)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"description"
      Parser (Maybe RawEmoji -> Bool -> SelectOption)
-> Parser (Maybe RawEmoji) -> Parser (Bool -> SelectOption)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe RawEmoji)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"emoji"
      Parser (Bool -> SelectOption) -> Parser Bool -> Parser SelectOption
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"default" Parser (Maybe Bool) -> Bool -> Parser Bool
forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
False

$(deriveTextShow ''SelectOption)
$(makeFieldLabelsNoPrefix ''SelectOption)

data Select = Select
  { Select -> [SelectOption]
options :: [SelectOption]
  , Select -> Maybe Text
placeholder :: Maybe T.Text
  , Select -> Maybe Int
minValues :: Maybe Int
  , Select -> Maybe Int
maxValues :: Maybe Int
  , Select -> Bool
disabled :: Bool
  , Select -> CustomID
customID :: CustomID
  }
  deriving (Int -> Select -> ShowS
[Select] -> ShowS
Select -> String
(Int -> Select -> ShowS)
-> (Select -> String) -> ([Select] -> ShowS) -> Show Select
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Select] -> ShowS
$cshowList :: [Select] -> ShowS
show :: Select -> String
$cshow :: Select -> String
showsPrec :: Int -> Select -> ShowS
$cshowsPrec :: Int -> Select -> ShowS
Show)
  deriving ([Select] -> Encoding
[Select] -> Value
Select -> Encoding
Select -> Value
(Select -> Value)
-> (Select -> Encoding)
-> ([Select] -> Value)
-> ([Select] -> Encoding)
-> ToJSON Select
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [Select] -> Encoding
$ctoEncodingList :: [Select] -> Encoding
toJSONList :: [Select] -> Value
$ctoJSONList :: [Select] -> Value
toEncoding :: Select -> Encoding
$ctoEncoding :: Select -> Encoding
toJSON :: Select -> Value
$ctoJSON :: Select -> Value
Aeson.ToJSON) via CalamityToJSON Select

instance CalamityToJSON' Select where
  toPairs :: Select -> [Maybe kv]
toPairs Select {Bool
[SelectOption]
Maybe Int
Maybe Text
CustomID
customID :: CustomID
disabled :: Bool
maxValues :: Maybe Int
minValues :: Maybe Int
placeholder :: Maybe Text
options :: [SelectOption]
$sel:customID:Select :: Select -> CustomID
$sel:disabled:Select :: Select -> Bool
$sel:maxValues:Select :: Select -> Maybe Int
$sel:minValues:Select :: Select -> Maybe Int
$sel:placeholder:Select :: Select -> Maybe Text
$sel:options:Select :: Select -> [SelectOption]
..} =
      [ Key
"options" Key -> [SelectOption] -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= [SelectOption]
options
      , Key
"placeholder" Key -> Maybe Text -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> Maybe v -> Maybe kv
.?= Maybe Text
placeholder
      , Key
"min_values" Key -> Maybe Int -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> Maybe v -> Maybe kv
.?= Maybe Int
minValues
      , Key
"max_values" Key -> Maybe Int -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> Maybe v -> Maybe kv
.?= Maybe Int
maxValues
      , Key
"disabled" Key -> Bool -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= Bool
disabled
      , Key
"custom_id" Key -> CustomID -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= CustomID
customID
      , Key
"type" Key -> ComponentType -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= ComponentType
SelectType
      ]

instance Aeson.FromJSON Select where
  parseJSON :: Value -> Parser Select
parseJSON = String -> (Object -> Parser Select) -> Value -> Parser Select
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Aeson.withObject String
"Components.Select" ((Object -> Parser Select) -> Value -> Parser Select)
-> (Object -> Parser Select) -> Value -> Parser Select
forall a b. (a -> b) -> a -> b
$ \Object
v ->
    [SelectOption]
-> Maybe Text
-> Maybe Int
-> Maybe Int
-> Bool
-> CustomID
-> Select
Select
      ([SelectOption]
 -> Maybe Text
 -> Maybe Int
 -> Maybe Int
 -> Bool
 -> CustomID
 -> Select)
-> Parser [SelectOption]
-> Parser
     (Maybe Text
      -> Maybe Int -> Maybe Int -> Bool -> CustomID -> Select)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Key -> Parser [SelectOption]
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"options"
      Parser
  (Maybe Text
   -> Maybe Int -> Maybe Int -> Bool -> CustomID -> Select)
-> Parser (Maybe Text)
-> Parser (Maybe Int -> Maybe Int -> Bool -> CustomID -> Select)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"placeholder"
      Parser (Maybe Int -> Maybe Int -> Bool -> CustomID -> Select)
-> Parser (Maybe Int)
-> Parser (Maybe Int -> Bool -> CustomID -> Select)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"min_values"
      Parser (Maybe Int -> Bool -> CustomID -> Select)
-> Parser (Maybe Int) -> Parser (Bool -> CustomID -> Select)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"max_values"
      Parser (Bool -> CustomID -> Select)
-> Parser Bool -> Parser (CustomID -> Select)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"disabled" Parser (Maybe Bool) -> Bool -> Parser Bool
forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
False
      Parser (CustomID -> Select) -> Parser CustomID -> Parser Select
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser CustomID
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"custom_id"

$(deriveTextShow ''Select)
$(makeFieldLabelsNoPrefix ''Select)

select :: [SelectOption] -> CustomID -> Select
select :: [SelectOption] -> CustomID -> Select
select [SelectOption]
o = [SelectOption]
-> Maybe Text
-> Maybe Int
-> Maybe Int
-> Bool
-> CustomID
-> Select
Select [SelectOption]
o Maybe Text
forall a. Maybe a
Nothing Maybe Int
forall a. Maybe a
Nothing Maybe Int
forall a. Maybe a
Nothing Bool
False

sopt ::
  -- | Label
  T.Text ->
  -- | Value
  T.Text ->
  SelectOption
sopt :: Text -> Text -> SelectOption
sopt Text
l Text
v = Text
-> Text -> Maybe Text -> Maybe RawEmoji -> Bool -> SelectOption
SelectOption Text
l Text
v Maybe Text
forall a. Maybe a
Nothing Maybe RawEmoji
forall a. Maybe a
Nothing Bool
False

data TextInputStyle
  = TextInputShort
  | TextInputParagraph
  deriving (Int -> TextInputStyle -> ShowS
[TextInputStyle] -> ShowS
TextInputStyle -> String
(Int -> TextInputStyle -> ShowS)
-> (TextInputStyle -> String)
-> ([TextInputStyle] -> ShowS)
-> Show TextInputStyle
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TextInputStyle] -> ShowS
$cshowList :: [TextInputStyle] -> ShowS
show :: TextInputStyle -> String
$cshow :: TextInputStyle -> String
showsPrec :: Int -> TextInputStyle -> ShowS
$cshowsPrec :: Int -> TextInputStyle -> ShowS
Show)

$(deriveTextShow ''TextInputStyle)

instance Aeson.ToJSON TextInputStyle where
  toJSON :: TextInputStyle -> Value
toJSON TextInputStyle
t = ToJSON Int => Int -> Value
forall a. ToJSON a => a -> Value
Aeson.toJSON @Int (Int -> Value) -> Int -> Value
forall a b. (a -> b) -> a -> b
$ case TextInputStyle
t of
    TextInputStyle
TextInputShort -> Int
1
    TextInputStyle
TextInputParagraph -> Int
2
  toEncoding :: TextInputStyle -> Encoding
toEncoding TextInputStyle
t = ToJSON Int => Int -> Encoding
forall a. ToJSON a => a -> Encoding
Aeson.toEncoding @Int (Int -> Encoding) -> Int -> Encoding
forall a b. (a -> b) -> a -> b
$ case TextInputStyle
t of
    TextInputStyle
TextInputShort -> Int
1
    TextInputStyle
TextInputParagraph -> Int
2

instance Aeson.FromJSON TextInputStyle where
  parseJSON :: Value -> Parser TextInputStyle
parseJSON = String
-> (Scientific -> Parser TextInputStyle)
-> Value
-> Parser TextInputStyle
forall a. String -> (Scientific -> Parser a) -> Value -> Parser a
Aeson.withScientific String
"Components.TextInputStyle" ((Scientific -> Parser TextInputStyle)
 -> Value -> Parser TextInputStyle)
-> (Scientific -> Parser TextInputStyle)
-> Value
-> Parser TextInputStyle
forall a b. (a -> b) -> a -> b
$ \Scientific
n -> case Scientific -> Maybe Int
forall i. (Integral i, Bounded i) => Scientific -> Maybe i
toBoundedInteger @Int Scientific
n of
    Just Int
v -> case Int
v of
      Int
1 -> TextInputStyle -> Parser TextInputStyle
forall (f :: * -> *) a. Applicative f => a -> f a
pure TextInputStyle
TextInputShort
      Int
2 -> TextInputStyle -> Parser TextInputStyle
forall (f :: * -> *) a. Applicative f => a -> f a
pure TextInputStyle
TextInputParagraph
      Int
_ -> String -> Parser TextInputStyle
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser TextInputStyle)
-> String -> Parser TextInputStyle
forall a b. (a -> b) -> a -> b
$ String
"Invalid TextInputStyle: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Scientific -> String
forall a. Show a => a -> String
show Scientific
n
    Maybe Int
Nothing -> String -> Parser TextInputStyle
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser TextInputStyle)
-> String -> Parser TextInputStyle
forall a b. (a -> b) -> a -> b
$ String
"Invalid TextInputStyle: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Scientific -> String
forall a. Show a => a -> String
show Scientific
n

data TextInput = TextInput
  { TextInput -> TextInputStyle
style :: TextInputStyle
  , TextInput -> Text
label :: T.Text
  , TextInput -> Maybe Int
minLength :: Maybe Int
  , TextInput -> Maybe Int
maxLength :: Maybe Int
  , TextInput -> Bool
required :: Bool
  , TextInput -> Maybe Text
value :: Maybe T.Text
  , TextInput -> Maybe Text
placeholder :: Maybe T.Text
  , TextInput -> CustomID
customID :: CustomID
  }
  deriving (Int -> TextInput -> ShowS
[TextInput] -> ShowS
TextInput -> String
(Int -> TextInput -> ShowS)
-> (TextInput -> String)
-> ([TextInput] -> ShowS)
-> Show TextInput
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TextInput] -> ShowS
$cshowList :: [TextInput] -> ShowS
show :: TextInput -> String
$cshow :: TextInput -> String
showsPrec :: Int -> TextInput -> ShowS
$cshowsPrec :: Int -> TextInput -> ShowS
Show)
  deriving ([TextInput] -> Encoding
[TextInput] -> Value
TextInput -> Encoding
TextInput -> Value
(TextInput -> Value)
-> (TextInput -> Encoding)
-> ([TextInput] -> Value)
-> ([TextInput] -> Encoding)
-> ToJSON TextInput
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [TextInput] -> Encoding
$ctoEncodingList :: [TextInput] -> Encoding
toJSONList :: [TextInput] -> Value
$ctoJSONList :: [TextInput] -> Value
toEncoding :: TextInput -> Encoding
$ctoEncoding :: TextInput -> Encoding
toJSON :: TextInput -> Value
$ctoJSON :: TextInput -> Value
Aeson.ToJSON) via CalamityToJSON TextInput

instance CalamityToJSON' TextInput where
  toPairs :: TextInput -> [Maybe kv]
toPairs TextInput {Bool
Maybe Int
Maybe Text
Text
CustomID
TextInputStyle
customID :: CustomID
placeholder :: Maybe Text
value :: Maybe Text
required :: Bool
maxLength :: Maybe Int
minLength :: Maybe Int
label :: Text
style :: TextInputStyle
$sel:customID:TextInput :: TextInput -> CustomID
$sel:placeholder:TextInput :: TextInput -> Maybe Text
$sel:value:TextInput :: TextInput -> Maybe Text
$sel:required:TextInput :: TextInput -> Bool
$sel:maxLength:TextInput :: TextInput -> Maybe Int
$sel:minLength:TextInput :: TextInput -> Maybe Int
$sel:label:TextInput :: TextInput -> Text
$sel:style:TextInput :: TextInput -> TextInputStyle
..} =
      [ Key
"style" Key -> TextInputStyle -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= TextInputStyle
style
      , Key
"label" Key -> Text -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= Text
label
      , Key
"min_length" Key -> Maybe Int -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= Maybe Int
minLength
      , Key
"max_length" Key -> Maybe Int -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= Maybe Int
maxLength
      , Key
"required" Key -> Bool -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= Bool
required
      , Key
"value" Key -> Maybe Text -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= Maybe Text
value
      , Key
"placeholder" Key -> Maybe Text -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= Maybe Text
placeholder
      , Key
"custom_id" Key -> CustomID -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= CustomID
customID
      , Key
"type" Key -> ComponentType -> Maybe kv
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= ComponentType
TextInputType
      ]

instance Aeson.FromJSON TextInput where
  parseJSON :: Value -> Parser TextInput
parseJSON = String -> (Object -> Parser TextInput) -> Value -> Parser TextInput
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Aeson.withObject String
"Components.TextInput" ((Object -> Parser TextInput) -> Value -> Parser TextInput)
-> (Object -> Parser TextInput) -> Value -> Parser TextInput
forall a b. (a -> b) -> a -> b
$ \Object
v ->
    TextInputStyle
-> Text
-> Maybe Int
-> Maybe Int
-> Bool
-> Maybe Text
-> Maybe Text
-> CustomID
-> TextInput
TextInput
      (TextInputStyle
 -> Text
 -> Maybe Int
 -> Maybe Int
 -> Bool
 -> Maybe Text
 -> Maybe Text
 -> CustomID
 -> TextInput)
-> Parser TextInputStyle
-> Parser
     (Text
      -> Maybe Int
      -> Maybe Int
      -> Bool
      -> Maybe Text
      -> Maybe Text
      -> CustomID
      -> TextInput)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Key -> Parser TextInputStyle
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"style"
      Parser
  (Text
   -> Maybe Int
   -> Maybe Int
   -> Bool
   -> Maybe Text
   -> Maybe Text
   -> CustomID
   -> TextInput)
-> Parser Text
-> Parser
     (Maybe Int
      -> Maybe Int
      -> Bool
      -> Maybe Text
      -> Maybe Text
      -> CustomID
      -> TextInput)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"label"
      Parser
  (Maybe Int
   -> Maybe Int
   -> Bool
   -> Maybe Text
   -> Maybe Text
   -> CustomID
   -> TextInput)
-> Parser (Maybe Int)
-> Parser
     (Maybe Int
      -> Bool -> Maybe Text -> Maybe Text -> CustomID -> TextInput)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"min_length"
      Parser
  (Maybe Int
   -> Bool -> Maybe Text -> Maybe Text -> CustomID -> TextInput)
-> Parser (Maybe Int)
-> Parser
     (Bool -> Maybe Text -> Maybe Text -> CustomID -> TextInput)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"max_length"
      Parser (Bool -> Maybe Text -> Maybe Text -> CustomID -> TextInput)
-> Parser Bool
-> Parser (Maybe Text -> Maybe Text -> CustomID -> TextInput)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"required" Parser (Maybe Bool) -> Bool -> Parser Bool
forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
False
      Parser (Maybe Text -> Maybe Text -> CustomID -> TextInput)
-> Parser (Maybe Text)
-> Parser (Maybe Text -> CustomID -> TextInput)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"value"
      Parser (Maybe Text -> CustomID -> TextInput)
-> Parser (Maybe Text) -> Parser (CustomID -> TextInput)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"placeholder"
      Parser (CustomID -> TextInput)
-> Parser CustomID -> Parser TextInput
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser CustomID
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"custom_id"

$(deriveTextShow ''TextInput)
$(makeFieldLabelsNoPrefix ''TextInput)

textInput ::
  TextInputStyle ->
  -- | Label
  T.Text ->
  CustomID ->
  TextInput
textInput :: TextInputStyle -> Text -> CustomID -> TextInput
textInput TextInputStyle
s Text
l = TextInputStyle
-> Text
-> Maybe Int
-> Maybe Int
-> Bool
-> Maybe Text
-> Maybe Text
-> CustomID
-> TextInput
TextInput TextInputStyle
s Text
l Maybe Int
forall a. Maybe a
Nothing Maybe Int
forall a. Maybe a
Nothing Bool
True Maybe Text
forall a. Maybe a
Nothing Maybe Text
forall a. Maybe a
Nothing

data Component
  = ActionRow' [Component]
  | Button' Button
  | LinkButton' LinkButton
  | Select' Select
  | TextInput' TextInput
  deriving (Int -> Component -> ShowS
[Component] -> ShowS
Component -> String
(Int -> Component -> ShowS)
-> (Component -> String)
-> ([Component] -> ShowS)
-> Show Component
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Component] -> ShowS
$cshowList :: [Component] -> ShowS
show :: Component -> String
$cshow :: Component -> String
showsPrec :: Int -> Component -> ShowS
$cshowsPrec :: Int -> Component -> ShowS
Show)

$(deriveTextShow ''Component)

instance Aeson.ToJSON Component where
  toJSON :: Component -> Value
toJSON Component
t =
    case Component
t of
      ActionRow' [Component]
xs -> [Pair] -> Value
Aeson.object ([Pair] -> Value)
-> ([Maybe Pair] -> [Pair]) -> [Maybe Pair] -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe Pair] -> Value) -> [Maybe Pair] -> Value
forall a b. (a -> b) -> a -> b
$ [Key
"components" Key -> [Component] -> Maybe Pair
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= [Component]
xs, Key
"type" Key -> ComponentType -> Maybe Pair
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= ComponentType
ActionRowType]
      Button' Button
b -> Button -> Value
forall a. ToJSON a => a -> Value
Aeson.toJSON Button
b
      LinkButton' LinkButton
lb -> LinkButton -> Value
forall a. ToJSON a => a -> Value
Aeson.toJSON LinkButton
lb
      Select' Select
s -> Select -> Value
forall a. ToJSON a => a -> Value
Aeson.toJSON Select
s
      TextInput' TextInput
ti -> TextInput -> Value
forall a. ToJSON a => a -> Value
Aeson.toJSON TextInput
ti

  toEncoding :: Component -> Encoding
toEncoding Component
t =
    case Component
t of
      ActionRow' [Component]
xs -> Series -> Encoding
Aeson.pairs (Series -> Encoding)
-> ([Maybe Series] -> Series) -> [Maybe Series] -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Series] -> Series
forall a. Monoid a => [a] -> a
mconcat ([Series] -> Series)
-> ([Maybe Series] -> [Series]) -> [Maybe Series] -> Series
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Maybe Series] -> [Series]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe Series] -> Encoding) -> [Maybe Series] -> Encoding
forall a b. (a -> b) -> a -> b
$ [Key
"components" Key -> [Component] -> Maybe Series
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= [Component]
xs, Key
"type" Key -> ComponentType -> Maybe Series
forall v kv. (ToJSON v, KeyValue kv) => Key -> v -> Maybe kv
.= ComponentType
ActionRowType]
      Button' Button
b -> Button -> Encoding
forall a. ToJSON a => a -> Encoding
Aeson.toEncoding Button
b
      LinkButton' LinkButton
lb -> LinkButton -> Encoding
forall a. ToJSON a => a -> Encoding
Aeson.toEncoding LinkButton
lb
      Select' Select
s -> Select -> Encoding
forall a. ToJSON a => a -> Encoding
Aeson.toEncoding Select
s
      TextInput' TextInput
ti -> TextInput -> Encoding
forall a. ToJSON a => a -> Encoding
Aeson.toEncoding TextInput
ti

instance Aeson.FromJSON Component where
  parseJSON :: Value -> Parser Component
parseJSON = String -> (Object -> Parser Component) -> Value -> Parser Component
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Aeson.withObject String
"Component" ((Object -> Parser Component) -> Value -> Parser Component)
-> (Object -> Parser Component) -> Value -> Parser Component
forall a b. (a -> b) -> a -> b
$ \Object
v -> do
    ComponentType
type_ :: ComponentType <- Object
v Object -> Key -> Parser ComponentType
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"type"

    case ComponentType
type_ of
      ComponentType
ActionRowType -> [Component] -> Component
ActionRow' ([Component] -> Component)
-> Parser [Component] -> Parser Component
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Key -> Parser [Component]
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"components"
      ComponentType
ButtonType -> do
        Maybe CustomID
cid :: Maybe CustomID <- Object
v Object -> Key -> Parser (Maybe CustomID)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"custom_id"
        Maybe Text
url :: Maybe T.Text <- Object
v Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"url"
        case (Maybe CustomID
cid, Maybe Text
url) of
          (Just CustomID
_, Maybe Text
_) -> Button -> Component
Button' (Button -> Component) -> Parser Button -> Parser Component
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser Button
forall a. FromJSON a => Value -> Parser a
Aeson.parseJSON (Object -> Value
Aeson.Object Object
v)
          (Maybe CustomID
_, Just Text
_) -> LinkButton -> Component
LinkButton' (LinkButton -> Component) -> Parser LinkButton -> Parser Component
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser LinkButton
forall a. FromJSON a => Value -> Parser a
Aeson.parseJSON (Object -> Value
Aeson.Object Object
v)
          (Maybe CustomID, Maybe Text)
_ -> String -> Parser Component
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser Component) -> String -> Parser Component
forall a b. (a -> b) -> a -> b
$ String
"Impossible button: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Object -> String
forall a. Show a => a -> String
show Object
v
      ComponentType
SelectType -> Select -> Component
Select' (Select -> Component) -> Parser Select -> Parser Component
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser Select
forall a. FromJSON a => Value -> Parser a
Aeson.parseJSON (Object -> Value
Aeson.Object Object
v)
      ComponentType
TextInputType -> TextInput -> Component
TextInput' (TextInput -> Component) -> Parser TextInput -> Parser Component
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser TextInput
forall a. FromJSON a => Value -> Parser a
Aeson.parseJSON (Object -> Value
Aeson.Object Object
v)

componentType :: Component -> ComponentType
componentType :: Component -> ComponentType
componentType (ActionRow' [Component]
_) = ComponentType
ActionRowType
componentType (Button' Button
_) = ComponentType
ButtonType
componentType (LinkButton' LinkButton
_) = ComponentType
ButtonType
componentType (Select' Select
_) = ComponentType
SelectType
componentType (TextInput' TextInput
_) = ComponentType
TextInputType