{-# 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 Data.Aeson qualified as Aeson
import Data.Maybe (catMaybes)
import Data.Scientific (toBoundedInteger)
import Data.Text qualified as T
import Optics.TH
import System.Random (Uniform)
import System.Random.Stateful (Uniform (uniformM), UniformRange (uniformRM))
import TextShow.TH

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

$(deriveTextShow ''CustomID)

instance Uniform CustomID where
  uniformM :: forall g (m :: * -> *). StatefulGen g m => 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
forall g (m :: * -> *).
StatefulGen g m =>
(Char, Char) -> g -> m Char
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
$c== :: ComponentType -> ComponentType -> Bool
== :: ComponentType -> ComponentType -> Bool
$c/= :: ComponentType -> ComponentType -> Bool
/= :: 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
$cshowsPrec :: Int -> ComponentType -> ShowS
showsPrec :: Int -> ComponentType -> ShowS
$cshow :: ComponentType -> String
show :: ComponentType -> String
$cshowList :: [ComponentType] -> ShowS
showList :: [ComponentType] -> ShowS
Show)

$(deriveTextShow ''ComponentType)

instance Aeson.ToJSON ComponentType where
  toJSON :: ComponentType -> Value
toJSON ComponentType
x = 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 = 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 forall i. (Integral i, Bounded i) => Scientific -> Maybe i
toBoundedInteger @Int Scientific
n of
    Just Int
1 -> ComponentType -> Parser ComponentType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ComponentType
ActionRowType
    Just Int
2 -> ComponentType -> Parser ComponentType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ComponentType
ButtonType
    Just Int
3 -> ComponentType -> Parser ComponentType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ComponentType
SelectType
    Just Int
4 -> ComponentType -> Parser ComponentType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ComponentType
TextInputType
    Maybe Int
_ -> String -> Parser ComponentType
forall a. String -> Parser a
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
$c== :: ButtonStyle -> ButtonStyle -> Bool
== :: ButtonStyle -> ButtonStyle -> Bool
$c/= :: ButtonStyle -> ButtonStyle -> Bool
/= :: 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
$cshowsPrec :: Int -> ButtonStyle -> ShowS
showsPrec :: Int -> ButtonStyle -> ShowS
$cshow :: ButtonStyle -> String
show :: ButtonStyle -> String
$cshowList :: [ButtonStyle] -> ShowS
showList :: [ButtonStyle] -> ShowS
Show)

$(deriveTextShow ''ButtonStyle)

instance Aeson.ToJSON ButtonStyle where
  toJSON :: ButtonStyle -> Value
toJSON ButtonStyle
t = 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 = 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 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 a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ButtonStyle
ButtonPrimary
      Int
2 -> ButtonStyle -> Parser ButtonStyle
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ButtonStyle
ButtonSecondary
      Int
3 -> ButtonStyle -> Parser ButtonStyle
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ButtonStyle
ButtonSuccess
      Int
4 -> ButtonStyle -> Parser ButtonStyle
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ButtonStyle
ButtonDanger
      Int
5 -> ButtonStyle -> Parser ButtonStyle
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ButtonStyle
ButtonLink
      Int
_ -> String -> Parser ButtonStyle
forall a. String -> Parser a
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 a. String -> Parser a
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
$cshowsPrec :: Int -> Button -> ShowS
showsPrec :: Int -> Button -> ShowS
$cshow :: Button -> String
show :: Button -> String
$cshowList :: [Button] -> ShowS
showList :: [Button] -> ShowS
Show)
  deriving ([Button] -> Value
[Button] -> Encoding
Button -> Value
Button -> Encoding
(Button -> Value)
-> (Button -> Encoding)
-> ([Button] -> Value)
-> ([Button] -> Encoding)
-> ToJSON Button
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
$ctoJSON :: Button -> Value
toJSON :: Button -> Value
$ctoEncoding :: Button -> Encoding
toEncoding :: Button -> Encoding
$ctoJSONList :: [Button] -> Value
toJSONList :: [Button] -> Value
$ctoEncodingList :: [Button] -> Encoding
toEncodingList :: [Button] -> Encoding
Aeson.ToJSON) via CalamityToJSON Button

instance CalamityToJSON' Button where
  toPairs :: forall kv. KeyValue kv => Button -> [Maybe kv]
toPairs Button {Bool
Maybe Text
Maybe RawEmoji
CustomID
ButtonStyle
$sel:style:Button :: Button -> ButtonStyle
$sel:label:Button :: Button -> Maybe Text
$sel:emoji:Button :: Button -> Maybe RawEmoji
$sel:disabled:Button :: Button -> Bool
$sel:customID:Button :: Button -> CustomID
style :: ButtonStyle
label :: Maybe Text
emoji :: Maybe RawEmoji
disabled :: Bool
customID :: CustomID
..} =
    [ 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 a b. Parser (a -> b) -> Parser a -> Parser b
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 a b. Parser (a -> b) -> Parser a -> Parser b
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 a b. Parser (a -> b) -> Parser a -> Parser b
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 a b. Parser (a -> b) -> Parser a -> Parser b
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
$cshowsPrec :: Int -> LinkButton -> ShowS
showsPrec :: Int -> LinkButton -> ShowS
$cshow :: LinkButton -> String
show :: LinkButton -> String
$cshowList :: [LinkButton] -> ShowS
showList :: [LinkButton] -> ShowS
Show)
  deriving ([LinkButton] -> Value
[LinkButton] -> Encoding
LinkButton -> Value
LinkButton -> Encoding
(LinkButton -> Value)
-> (LinkButton -> Encoding)
-> ([LinkButton] -> Value)
-> ([LinkButton] -> Encoding)
-> ToJSON LinkButton
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
$ctoJSON :: LinkButton -> Value
toJSON :: LinkButton -> Value
$ctoEncoding :: LinkButton -> Encoding
toEncoding :: LinkButton -> Encoding
$ctoJSONList :: [LinkButton] -> Value
toJSONList :: [LinkButton] -> Value
$ctoEncodingList :: [LinkButton] -> Encoding
toEncodingList :: [LinkButton] -> Encoding
Aeson.ToJSON) via CalamityToJSON LinkButton

instance CalamityToJSON' LinkButton where
  toPairs :: forall kv. KeyValue kv => LinkButton -> [Maybe kv]
toPairs LinkButton {Bool
Maybe Text
Maybe RawEmoji
Text
ButtonStyle
$sel:style:LinkButton :: LinkButton -> ButtonStyle
$sel:label:LinkButton :: LinkButton -> Maybe Text
$sel:emoji:LinkButton :: LinkButton -> Maybe RawEmoji
$sel:url:LinkButton :: LinkButton -> Text
$sel:disabled:LinkButton :: LinkButton -> Bool
style :: ButtonStyle
label :: Maybe Text
emoji :: Maybe RawEmoji
url :: Text
disabled :: Bool
..} =
    [ 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 a b. Parser (a -> b) -> Parser a -> Parser b
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 a b. Parser (a -> b) -> Parser a -> Parser b
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 a b. Parser (a -> b) -> Parser a -> Parser b
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 a b. Parser (a -> b) -> Parser a -> Parser b
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
$cshowsPrec :: Int -> SelectOption -> ShowS
showsPrec :: Int -> SelectOption -> ShowS
$cshow :: SelectOption -> String
show :: SelectOption -> String
$cshowList :: [SelectOption] -> ShowS
showList :: [SelectOption] -> ShowS
Show)
  deriving ([SelectOption] -> Value
[SelectOption] -> Encoding
SelectOption -> Value
SelectOption -> Encoding
(SelectOption -> Value)
-> (SelectOption -> Encoding)
-> ([SelectOption] -> Value)
-> ([SelectOption] -> Encoding)
-> ToJSON SelectOption
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
$ctoJSON :: SelectOption -> Value
toJSON :: SelectOption -> Value
$ctoEncoding :: SelectOption -> Encoding
toEncoding :: SelectOption -> Encoding
$ctoJSONList :: [SelectOption] -> Value
toJSONList :: [SelectOption] -> Value
$ctoEncodingList :: [SelectOption] -> Encoding
toEncodingList :: [SelectOption] -> Encoding
Aeson.ToJSON) via CalamityToJSON SelectOption

instance CalamityToJSON' SelectOption where
  toPairs :: forall kv. KeyValue kv => SelectOption -> [Maybe kv]
toPairs SelectOption {Bool
Maybe Text
Maybe RawEmoji
Text
$sel:label:SelectOption :: SelectOption -> Text
$sel:value:SelectOption :: SelectOption -> Text
$sel:description:SelectOption :: SelectOption -> Maybe Text
$sel:emoji:SelectOption :: SelectOption -> Maybe RawEmoji
$sel:default_:SelectOption :: SelectOption -> Bool
label :: Text
value :: Text
description :: Maybe Text
emoji :: Maybe RawEmoji
default_ :: Bool
..} =
    [ 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 a b. Parser (a -> b) -> Parser a -> Parser b
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 a b. Parser (a -> b) -> Parser a -> Parser b
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 a b. Parser (a -> b) -> Parser a -> Parser b
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 a b. Parser (a -> b) -> Parser a -> Parser b
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
$cshowsPrec :: Int -> Select -> ShowS
showsPrec :: Int -> Select -> ShowS
$cshow :: Select -> String
show :: Select -> String
$cshowList :: [Select] -> ShowS
showList :: [Select] -> ShowS
Show)
  deriving ([Select] -> Value
[Select] -> Encoding
Select -> Value
Select -> Encoding
(Select -> Value)
-> (Select -> Encoding)
-> ([Select] -> Value)
-> ([Select] -> Encoding)
-> ToJSON Select
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
$ctoJSON :: Select -> Value
toJSON :: Select -> Value
$ctoEncoding :: Select -> Encoding
toEncoding :: Select -> Encoding
$ctoJSONList :: [Select] -> Value
toJSONList :: [Select] -> Value
$ctoEncodingList :: [Select] -> Encoding
toEncodingList :: [Select] -> Encoding
Aeson.ToJSON) via CalamityToJSON Select

instance CalamityToJSON' Select where
  toPairs :: forall kv. KeyValue kv => Select -> [Maybe kv]
toPairs Select {Bool
[SelectOption]
Maybe Int
Maybe Text
CustomID
$sel:options:Select :: Select -> [SelectOption]
$sel:placeholder:Select :: Select -> Maybe Text
$sel:minValues:Select :: Select -> Maybe Int
$sel:maxValues:Select :: Select -> Maybe Int
$sel:disabled:Select :: Select -> Bool
$sel:customID:Select :: Select -> CustomID
options :: [SelectOption]
placeholder :: Maybe Text
minValues :: Maybe Int
maxValues :: Maybe Int
disabled :: Bool
customID :: CustomID
..} =
    [ 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 a b. Parser (a -> b) -> Parser a -> Parser b
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 a b. Parser (a -> b) -> Parser a -> Parser b
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 a b. Parser (a -> b) -> Parser a -> Parser b
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 a b. Parser (a -> b) -> Parser a -> Parser b
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 a b. Parser (a -> b) -> Parser a -> Parser b
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
$cshowsPrec :: Int -> TextInputStyle -> ShowS
showsPrec :: Int -> TextInputStyle -> ShowS
$cshow :: TextInputStyle -> String
show :: TextInputStyle -> String
$cshowList :: [TextInputStyle] -> ShowS
showList :: [TextInputStyle] -> ShowS
Show)

$(deriveTextShow ''TextInputStyle)

instance Aeson.ToJSON TextInputStyle where
  toJSON :: TextInputStyle -> Value
toJSON TextInputStyle
t = 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 = 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 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 a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TextInputStyle
TextInputShort
      Int
2 -> TextInputStyle -> Parser TextInputStyle
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TextInputStyle
TextInputParagraph
      Int
_ -> String -> Parser TextInputStyle
forall a. String -> Parser a
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 a. String -> Parser a
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
$cshowsPrec :: Int -> TextInput -> ShowS
showsPrec :: Int -> TextInput -> ShowS
$cshow :: TextInput -> String
show :: TextInput -> String
$cshowList :: [TextInput] -> ShowS
showList :: [TextInput] -> ShowS
Show)
  deriving ([TextInput] -> Value
[TextInput] -> Encoding
TextInput -> Value
TextInput -> Encoding
(TextInput -> Value)
-> (TextInput -> Encoding)
-> ([TextInput] -> Value)
-> ([TextInput] -> Encoding)
-> ToJSON TextInput
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
$ctoJSON :: TextInput -> Value
toJSON :: TextInput -> Value
$ctoEncoding :: TextInput -> Encoding
toEncoding :: TextInput -> Encoding
$ctoJSONList :: [TextInput] -> Value
toJSONList :: [TextInput] -> Value
$ctoEncodingList :: [TextInput] -> Encoding
toEncodingList :: [TextInput] -> Encoding
Aeson.ToJSON) via CalamityToJSON TextInput

instance CalamityToJSON' TextInput where
  toPairs :: forall kv. KeyValue kv => TextInput -> [Maybe kv]
toPairs TextInput {Bool
Maybe Int
Maybe Text
Text
CustomID
TextInputStyle
$sel:style:TextInput :: TextInput -> TextInputStyle
$sel:label:TextInput :: TextInput -> Text
$sel:minLength:TextInput :: TextInput -> Maybe Int
$sel:maxLength:TextInput :: TextInput -> Maybe Int
$sel:required:TextInput :: TextInput -> Bool
$sel:value:TextInput :: TextInput -> Maybe Text
$sel:placeholder:TextInput :: TextInput -> Maybe Text
$sel:customID:TextInput :: TextInput -> CustomID
style :: TextInputStyle
label :: Text
minLength :: Maybe Int
maxLength :: Maybe Int
required :: Bool
value :: Maybe Text
placeholder :: Maybe Text
customID :: CustomID
..} =
    [ 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 a b. Parser (a -> b) -> Parser a -> Parser b
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 a b. Parser (a -> b) -> Parser a -> Parser b
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 a b. Parser (a -> b) -> Parser a -> Parser b
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 a b. Parser (a -> b) -> Parser a -> Parser b
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 a b. Parser (a -> b) -> Parser a -> Parser b
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 a b. Parser (a -> b) -> Parser a -> Parser b
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 a b. Parser (a -> b) -> Parser a -> Parser b
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
$cshowsPrec :: Int -> Component -> ShowS
showsPrec :: Int -> Component -> ShowS
$cshow :: Component -> String
show :: Component -> String
$cshowList :: [Component] -> ShowS
showList :: [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 a. String -> Parser a
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