{-# LANGUAGE UndecidableInstances #-}

module Web.Hyperbole.HyperView.Hyper where

import Web.Atomic.Types
import Web.Hyperbole.Data.Encoded as Encoded
import Web.Hyperbole.HyperView.Handled (HyperViewHandled)
import Web.Hyperbole.HyperView.Types
import Web.Hyperbole.View (View, runViewContext, tag)
import Web.Hyperbole.View.ViewId


{- | Embed a 'HyperView' into a page or another 'View'

@
page :: 'Page' es '[Message]
page = do
  pure $ do
    'el' \"Unchanging Header\"
    'hyper' Message1 $ messageView \"Hello\"
    'hyper' Message2 $ messageView \"World\"
@
-}
hyper
  :: forall id ctx
   . (HyperViewHandled id ctx, ViewId id, ViewState id ~ (), ConcurrencyValue (Concurrency id))
  => id
  -> View id ()
  -> View ctx ()
hyper :: forall id ctx.
(HyperViewHandled id ctx, ViewId id, ViewState id ~ (),
 ConcurrencyValue (Concurrency id)) =>
id -> View id () -> View ctx ()
hyper id
vid = id -> ViewState id -> View id () -> View ctx ()
forall id ctx.
(HyperViewHandled id ctx, ViewId id, ToEncoded (ViewState id),
 ConcurrencyValue (Concurrency id)) =>
id -> ViewState id -> View id () -> View ctx ()
hyperState id
vid ()


hyperState
  :: forall id ctx
   . (HyperViewHandled id ctx, ViewId id, ToEncoded (ViewState id), ConcurrencyValue (Concurrency id))
  => id
  -> ViewState id
  -> View id ()
  -> View ctx ()
hyperState :: forall id ctx.
(HyperViewHandled id ctx, ViewId id, ToEncoded (ViewState id),
 ConcurrencyValue (Concurrency id)) =>
id -> ViewState id -> View id () -> View ctx ()
hyperState = id -> ViewState id -> View id () -> View ctx ()
forall id ctx.
(ViewId id, ViewState id ~ ViewState id, ToEncoded (ViewState id),
 ConcurrencyValue (Concurrency id)) =>
id -> ViewState id -> View id () -> View ctx ()
hyperUnsafe


hyperUnsafe :: forall id ctx. (ViewId id, ViewState id ~ ViewState id, ToEncoded (ViewState id), ConcurrencyValue (Concurrency id)) => id -> ViewState id -> View id () -> View ctx ()
hyperUnsafe :: forall id ctx.
(ViewId id, ViewState id ~ ViewState id, ToEncoded (ViewState id),
 ConcurrencyValue (Concurrency id)) =>
id -> ViewState id -> View id () -> View ctx ()
hyperUnsafe id
vid ViewState id
st View id ()
vw = do
  Text -> View ctx () -> View ctx ()
forall c. Text -> View c () -> View c ()
tag Text
"div" (View ctx () -> View ctx ())
-> (Attributes (View ctx () -> View ctx ())
    -> Attributes (View ctx () -> View ctx ()))
-> View ctx ()
-> View ctx ()
forall h.
Attributable h =>
h -> (Attributes h -> Attributes h) -> h
@ Text
-> Text
-> Attributes (View ctx () -> View ctx ())
-> Attributes (View ctx () -> View ctx ())
forall h.
Attributable h =>
Text -> Text -> Attributes h -> Attributes h
att Text
"id" (Encoded -> Text
encodedToText (Encoded -> Text) -> Encoded -> Text
forall a b. (a -> b) -> a -> b
$ id -> Encoded
forall a. ViewId a => a -> Encoded
toViewId id
vid) (Attributes (View ctx () -> View ctx ())
 -> Attributes (View ctx () -> View ctx ()))
-> (Attributes (View ctx () -> View ctx ())
    -> Attributes (View ctx () -> View ctx ()))
-> Attributes (View ctx () -> View ctx ())
-> Attributes (View ctx () -> View ctx ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Attributes (View ctx () -> View ctx ())
-> Attributes (View ctx () -> View ctx ())
state (Attributes (View ctx () -> View ctx ())
 -> Attributes (View ctx () -> View ctx ()))
-> (Attributes (View ctx () -> View ctx ())
    -> Attributes (View ctx () -> View ctx ()))
-> Attributes (View ctx () -> View ctx ())
-> Attributes (View ctx () -> View ctx ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Attributes (View ctx () -> View ctx ())
-> Attributes (View ctx () -> View ctx ())
concurrency (View ctx () -> View ctx ()) -> View ctx () -> View ctx ()
forall a b. (a -> b) -> a -> b
$
    id -> ViewState id -> View id () -> View ctx ()
forall ctx c. ctx -> ViewState ctx -> View ctx () -> View c ()
runViewContext id
vid ViewState id
st View id ()
vw
 where
  concurrency :: Attributes (View ctx () -> View ctx ())
-> Attributes (View ctx () -> View ctx ())
concurrency =
    case forall {k} (a :: k). ConcurrencyValue a => ConcurrencyMode
forall (a :: ConcurrencyMode).
ConcurrencyValue a =>
ConcurrencyMode
concurrencyMode @(Concurrency id) of
      ConcurrencyMode
Drop -> Attributes (View ctx () -> View ctx ())
-> Attributes (View ctx () -> View ctx ())
forall a. a -> a
id
      ConcurrencyMode
Replace -> Text
-> Text
-> Attributes (View ctx () -> View ctx ())
-> Attributes (View ctx () -> View ctx ())
forall h.
Attributable h =>
Text -> Text -> Attributes h -> Attributes h
att Text
"data-concurrency" (ConcurrencyMode -> Text
forall a. ToEncoded a => a -> Text
encode ConcurrencyMode
Replace)

  state :: Attributes (View ctx () -> View ctx ())
-> Attributes (View ctx () -> View ctx ())
state =
    if ViewState id -> Text
forall a. ToEncoded a => a -> Text
encode ViewState id
st Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
forall a. Monoid a => a
mempty
      then Attributes (View ctx () -> View ctx ())
-> Attributes (View ctx () -> View ctx ())
forall a. a -> a
id
      else Text
-> Text
-> Attributes (View ctx () -> View ctx ())
-> Attributes (View ctx () -> View ctx ())
forall h.
Attributable h =>
Text -> Text -> Attributes h -> Attributes h
att Text
"data-state" (ViewState id -> Text
forall a. ToEncoded a => a -> Text
encode ViewState id
st)