----------------------------------------------------------------------

----------------------------------------------------------------------

-- |
-- Module: Web.Slack.Util
-- Description:
module Web.Slack.Util
  ( formOpts,
    jsonOpts,
    toQueryParamIfJust,
  )
where

-- FIXME: Web.Slack.Prelude

-- aeson
import Data.Aeson.TH
import Data.Aeson.Types
-- base
import Data.Char
import Data.Maybe (maybeToList)
-- http-api-data

-- text
import Data.Text (Text)
import Data.Text qualified as Text
import GHC.Exts (fromList)
import Web.FormUrlEncoded (Form, FormOptions (FormOptions))
import Web.HttpApiData (ToHttpApiData, toQueryParam)
import Prelude

formOpts ::
  Text ->
  FormOptions
formOpts :: Text -> FormOptions
formOpts Text
prefix =
  (String -> String) -> FormOptions
FormOptions (Text -> String -> String
modifyLabel Text
prefix)

jsonOpts ::
  Text ->
  Options
jsonOpts :: Text -> Options
jsonOpts Text
prefix =
  Options
defaultOptions
    { fieldLabelModifier :: String -> String
fieldLabelModifier = Text -> String -> String
modifyLabel Text
prefix
    }

modifyLabel ::
  Text ->
  String ->
  String
modifyLabel :: Text -> String -> String
modifyLabel Text
prefix =
  forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Char -> Char
toLower
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
addUnderscores
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> [a] -> [a]
drop (Text -> Int
Text.length Text
prefix)

addUnderscores ::
  String ->
  String
addUnderscores :: String -> String
addUnderscores =
  Char -> String -> String
camelTo2 Char
'_'

toQueryParamIfJust :: ToHttpApiData a => Text -> Maybe a -> Form
toQueryParamIfJust :: forall a. ToHttpApiData a => Text -> Maybe a -> Form
toQueryParamIfJust Text
key =
  forall l. IsList l => [Item l] -> l
fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Maybe a -> [a]
maybeToList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\a
justVal -> (Text
key, forall a. ToHttpApiData a => a -> Text
toQueryParam a
justVal))