{- |
Module      : Htmx.Lucid.Core
Description : Provides core htmx tags

This module defines the "core" 11 HTMX attributes
<https://htmx.org/reference/#attributes>
-}
module Htmx.Lucid.Core where

import Data.Text (Text, pack)
import Htmx.Event
import Htmx.Render
import Htmx.Swap (Swap)
import Lucid (Html, HtmlT, script_, src_)
import Lucid.Base (Attributes, makeAttributes)

-- | <https://htmx.org/attributes/hx-get/>
-- issues a GET to the specified URL
hxGet_ :: Text -> Attributes
hxGet_ :: Text -> Attributes
hxGet_ = Text -> Text -> Attributes
makeAttributes Text
"hx-get"

-- | <https://htmx.org/attributes/hx-get/>
-- issues a POST to the specified URL
hxPost_ :: Text -> Attributes
hxPost_ :: Text -> Attributes
hxPost_ = Text -> Text -> Attributes
makeAttributes Text
"hx-post"

-- | <https://htmx.org/attributes/hx-push-url/>
-- push a URL into the browser location bar to create history
hxPushUrl_ :: Text -> Attributes
hxPushUrl_ :: Text -> Attributes
hxPushUrl_ = Text -> Text -> Attributes
makeAttributes Text
"hx-push-url"

-- | <https://htmx.org/attributes/hx-select/>
-- select content to swap in from a response
hxSelect_ :: Text -> Attributes
hxSelect_ :: Text -> Attributes
hxSelect_ = Text -> Text -> Attributes
makeAttributes Text
"hx-select"

-- | <https://htmx.org/attributes/hx-select-oob/>
-- select content to swap in from a response, somewhere other than the target
-- (out of band)
hxSelectOob_ :: Text -> Attributes
hxSelectOob_ :: Text -> Attributes
hxSelectOob_ = Text -> Text -> Attributes
makeAttributes Text
"hx-select-oob"

-- | <https://htmx.org/attributes/hx-swap/>
-- controls how content will swap in (outerHTML, beforeend, afterend, …)
hxSwap_ :: Text -> Attributes
hxSwap_ :: Text -> Attributes
hxSwap_ = Text -> Text -> Attributes
makeAttributes Text
"hx-swap"

-- | Like 'hxSwap_' but takes a strongly typed swap style.
-- This doesn't allow [modifiers](https://htmx.org/attributes/hx-swap/#modifiers) to be applied.
hxSwapS_ :: Swap -> Attributes
hxSwapS_ :: Swap -> Attributes
hxSwapS_ = Text -> Text -> Attributes
makeAttributes Text
"hx-swap" (Text -> Attributes) -> (Swap -> Text) -> Swap -> Attributes
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Swap -> Text
forall a. Render a => a -> Text
render

-- | <https://htmx.org/attributes/hx-swap-oob/>
-- mark element to swap in from a response (out of band)
hxSwapOob_ :: Text -> Attributes
hxSwapOob_ :: Text -> Attributes
hxSwapOob_ = Text -> Text -> Attributes
makeAttributes Text
"hx-swap-oob"

-- | <https://htmx.org/attributes/hx-target/>
-- specifies the target element to be swapped
hxTarget_ :: Text -> Attributes
hxTarget_ :: Text -> Attributes
hxTarget_ = Text -> Text -> Attributes
makeAttributes Text
"hx-target"

-- | <https://htmx.org/attributes/hx-trigger/>
-- specifies the event that triggers the request
hxTrigger_ :: Text -> Attributes
hxTrigger_ :: Text -> Attributes
hxTrigger_ = Text -> Text -> Attributes
makeAttributes Text
"hx-trigger"

-- | <https://htmx.org/attributes/hx-vals/>
-- add values to submit with the request (JSON format)
hxVals_ :: Text -> Attributes
hxVals_ :: Text -> Attributes
hxVals_ = Text -> Text -> Attributes
makeAttributes Text
"hx-vals"

-- | Indicates whether you are handling an arbitrary DOM event
-- or on of the bespoke 'HtmxEvent' (defined by the htmx js bundle)
data OnEvent = DomOnEvent Text | HtmxOnEvent HtmxEvent

-- | <https://htmx.org/attributes/hx-on/>
-- handle events with inline scripts on elements
hxOn_ :: OnEvent -> Text -> Attributes
hxOn_ :: OnEvent -> Text -> Attributes
hxOn_ = \case
    DomOnEvent Text
event -> Text -> Text -> Attributes
makeAttributes (Text -> Text -> Attributes) -> Text -> Text -> Attributes
forall a b. (a -> b) -> a -> b
$ Text
"hx-on:" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
event
    HtmxOnEvent HtmxEvent
htmxEvent -> Text -> Text -> Attributes
makeAttributes (Text -> Text -> Attributes) -> Text -> Text -> Attributes
forall a b. (a -> b) -> a -> b
$ Text
"hx-on::" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> HtmxEvent -> Text
forall a. Render a => a -> Text
render HtmxEvent
htmxEvent