module Web.Scotty.Form
  ( Trans.ScottyFormError(..)
  , encQP
  , ScottyForm
  , ditto
  , dittoSingle
  , simpleDittoGET
  , simpleDittoPOST
  , liftParser'
  , liftParser
  ) where

import Data.Text (Text)
import Ditto.Core hiding (view)
import Ditto.Types
import Web.Scotty
import qualified Web.Scotty.Trans.Form as Trans
import qualified Data.Text.Lazy as TL
import Lucid

encQP :: [(a, TL.Text)] -> Text
encQP :: [(a, Text)] -> Text
encQP = [(a, Text)] -> Text
forall a. [(a, Text)] -> Text
Trans.encQP

-- | a @ditto@ formlet for @scotty@
type ScottyForm a = Form ActionM [Param] Trans.ScottyFormError (Html ()) a

ditto :: (Monoid view)
  => ([(Text, Text)] -> view -> view) -- ^ wrap raw form html inside a <form> tag
  -> Text -- ^ form name prefix
  -> Form ActionM [Param] err view a -- ^ the formlet
  -> ActionM (Result err a, view)
ditto :: ([(Text, Text)] -> view -> view)
-> Text
-> Form ActionM [Param] err view a
-> ActionM (Result err a, view)
ditto = ([(Text, Text)] -> view -> view)
-> Text
-> Form ActionM [Param] err view a
-> ActionM (Result err a, view)
forall view (m :: * -> *) e err a.
(Monoid view, Monad m, ScottyError e) =>
([(Text, Text)] -> view -> view)
-> Text
-> Form (ActionT e m) [Param] err view a
-> ActionT e m (Result err a, view)
Trans.ditto

-- | a helpful wrapper around 'runForm'
dittoSingle
  :: ([(Text, Text)] -> view -> view) -- ^ wrap raw form html inside a <form> tag
  -> Text -- ^ form name prefix
  -> Form ActionM [Param] err view a -- ^ the formlet
  -> ActionM (Result err a, view)
dittoSingle :: ([(Text, Text)] -> view -> view)
-> Text
-> Form ActionM [Param] err view a
-> ActionM (Result err a, view)
dittoSingle = ([(Text, Text)] -> view -> view)
-> Text
-> Form ActionM [Param] err view a
-> ActionM (Result err a, view)
forall (m :: * -> *) e view err a.
(Monad m, ScottyError e) =>
([(Text, Text)] -> view -> view)
-> Text
-> Form (ActionT e m) [Param] err view a
-> ActionT e m (Result err a, view)
Trans.dittoSingle

-- | create @\<form action=action method=\"GET\" enctype=\"application/xxx-form-urlencoded\"\>@
simpleDittoGET :: (Applicative f)
  => Text -- ^ action
  -> Form ActionM [Param] err (HtmlT f ()) b -- ^ formlet
  -> ActionM (Result err b, HtmlT f ())
simpleDittoGET :: Text
-> Form ActionM [Param] err (HtmlT f ()) b
-> ActionM (Result err b, HtmlT f ())
simpleDittoGET = Text
-> Form ActionM [Param] err (HtmlT f ()) b
-> ActionM (Result err b, HtmlT f ())
forall (f :: * -> *) (m :: * -> *) e err b.
(Applicative f, Monad m, ScottyError e) =>
Text
-> Form (ActionT e m) [Param] err (HtmlT f ()) b
-> ActionT e m (Result err b, HtmlT f ())
Trans.simpleDittoGET

-- | create @\<form action=action method=\"POST\" enctype=\"application/xxx-form-urlencoded\"\>@
simpleDittoPOST :: (Applicative f)
  => Text -- ^ action
  -> Form ActionM [Param] err (HtmlT f ()) b -- ^ formlet
  -> ActionM (Result err b, HtmlT f ())
simpleDittoPOST :: Text
-> Form ActionM [Param] err (HtmlT f ()) b
-> ActionM (Result err b, HtmlT f ())
simpleDittoPOST = Text
-> Form ActionM [Param] err (HtmlT f ()) b
-> ActionM (Result err b, HtmlT f ())
forall (f :: * -> *) (m :: * -> *) e err b.
(Applicative f, Monad m, ScottyError e) =>
Text
-> Form (ActionT e m) [Param] err (HtmlT f ()) b
-> ActionT e m (Result err b, HtmlT f ())
Trans.simpleDittoPOST

-- | lift a function which parses strict @Text@ into a function which parses a @[Param]@
liftParser' :: (Text -> Either Text a) -> ([Param] -> Either Trans.ScottyFormError a)
liftParser' :: (Text -> Either Text a) -> [Param] -> Either ScottyFormError a
liftParser' = (Text -> Either Text a) -> [Param] -> Either ScottyFormError a
forall a.
(Text -> Either Text a) -> [Param] -> Either ScottyFormError a
Trans.liftParser'

-- | lift a function which parses lazy @Text@ into a function which parses a @[Param]@
-- e.g.
--
-- @
-- parserRead :: Read a => [Param] -> Either ScottyFormError a
-- parserRead = liftParser readEither
-- @
liftParser :: (TL.Text -> Either TL.Text a) -> ([Param] -> Either Trans.ScottyFormError a)
liftParser :: (Text -> Either Text a) -> [Param] -> Either ScottyFormError a
liftParser = (Text -> Either Text a) -> [Param] -> Either ScottyFormError a
forall a.
(Text -> Either Text a) -> [Param] -> Either ScottyFormError a
Trans.liftParser