-- | Updateable objects
module Calamity.Internal.Updateable (Updateable (..)) where

import Calamity.Internal.Utils
import Calamity.Types.Model.Channel
import Calamity.Types.Model.Channel.UpdatedMessage
import Calamity.Types.Model.Guild.Guild
import Calamity.Types.Model.User
import Data.Maybe
import GHC.TypeLits
import Optics

class Updateable a where
  type Updated a
  type Updated a = a

  update :: Updated a -> a -> a

-- | sets original field to new field
setF ::
  forall (f :: Symbol) o n b k.
  ( k ~ A_Lens
  , LabelOptic' f k o b
  , LabelOptic' f k n b
  ) =>
  n ->
  o ->
  o
setF :: forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n b) =>
n -> o -> o
setF n
n = forall (name :: Symbol) k s t a b.
LabelOptic name k s t a b =>
Optic k NoIx s t a b
labelOptic @f Optic A_Lens NoIx o o b b -> b -> o -> o
forall k (is :: IxList) s t a b.
Is k A_Setter =>
Optic k is s t a b -> b -> s -> t
.~ n
n n -> Optic' A_Lens NoIx n b -> b
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. forall (name :: Symbol) k s t a b.
LabelOptic name k s t a b =>
Optic k NoIx s t a b
labelOptic @f

-- | sets original field to unwrapped new field if new field is not Nothing
mergeF ::
  forall (f :: Symbol) o n b k.
  ( k ~ A_Lens
  , LabelOptic' f k o b
  , LabelOptic' f k n (Maybe b)
  ) =>
  n ->
  o ->
  o
mergeF :: forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n (Maybe b)) =>
n -> o -> o
mergeF n
n = forall (name :: Symbol) k s t a b.
LabelOptic name k s t a b =>
Optic k NoIx s t a b
labelOptic @f Optic A_Lens NoIx o o b b -> (b -> b) -> o -> o
forall k (is :: IxList) s t a b.
Is k A_Setter =>
Optic k is s t a b -> (a -> b) -> s -> t
%~ \b
oldv -> b -> Maybe b -> b
forall a. a -> Maybe a -> a
fromMaybe b
oldv (n
n n -> Optic' A_Lens NoIx n (Maybe b) -> Maybe b
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. forall (name :: Symbol) k s t a b.
LabelOptic name k s t a b =>
Optic k NoIx s t a b
labelOptic @f)

-- | sets original field to new field if new field is not nothing
mergeF' ::
  forall (f :: Symbol) old new v k.
  ( k ~ A_Lens
  , LabelOptic' f k old (Maybe v)
  , LabelOptic' f k new (Maybe v)
  ) =>
  new ->
  old ->
  old
mergeF' :: forall (f :: Symbol) old new v k.
(k ~ A_Lens, LabelOptic' f k old (Maybe v),
 LabelOptic' f k new (Maybe v)) =>
new -> old -> old
mergeF' new
new = forall (name :: Symbol) k s t a b.
LabelOptic name k s t a b =>
Optic k NoIx s t a b
labelOptic @f Optic A_Lens NoIx old old (Maybe v) (Maybe v)
-> (Maybe v -> Maybe v) -> old -> old
forall k (is :: IxList) s t a b.
Is k A_Setter =>
Optic k is s t a b -> (a -> b) -> s -> t
%~ \Maybe v
oldv -> Maybe v -> Maybe v -> Maybe v
forall a. Maybe a -> Maybe a -> Maybe a
lastMaybe Maybe v
oldv (new
new new -> Optic' A_Lens NoIx new (Maybe v) -> Maybe v
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. forall (name :: Symbol) k s t a b.
LabelOptic name k s t a b =>
Optic k NoIx s t a b
labelOptic @f)

-- | sets original field to new field if new field was present
updateNullableDest ::
  forall (f :: Symbol) old new v k.
  ( k ~ A_Lens
  , LabelOptic' f k old (Maybe v)
  , LabelOptic' f k new (Maybe (MaybeNull v))
  ) =>
  new ->
  old ->
  old
updateNullableDest :: forall (f :: Symbol) old new v k.
(k ~ A_Lens, LabelOptic' f k old (Maybe v),
 LabelOptic' f k new (Maybe (MaybeNull v))) =>
new -> old -> old
updateNullableDest new
new = case new
new new
-> Optic' A_Lens NoIx new (Maybe (MaybeNull v))
-> Maybe (MaybeNull v)
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. forall (name :: Symbol) k s t a b.
LabelOptic name k s t a b =>
Optic k NoIx s t a b
labelOptic @f of
  Just (NotNull v
x) -> forall (name :: Symbol) k s t a b.
LabelOptic name k s t a b =>
Optic k NoIx s t a b
labelOptic @f Optic A_Lens NoIx old old (Maybe v) (Maybe v) -> v -> old -> old
forall k (is :: IxList) s t a b.
Is k A_Setter =>
Optic k is s t a (Maybe b) -> b -> s -> t
?~ v
x
  Just MaybeNull v
WasNull -> forall (name :: Symbol) k s t a b.
LabelOptic name k s t a b =>
Optic k NoIx s t a b
labelOptic @f Optic A_Lens NoIx old old (Maybe v) (Maybe v)
-> Maybe v -> old -> old
forall k (is :: IxList) s t a b.
Is k A_Setter =>
Optic k is s t a b -> b -> s -> t
.~ Maybe v
forall a. Maybe a
Nothing
  Maybe (MaybeNull v)
Nothing -> old -> old
forall a. a -> a
Prelude.id

-- NOTE: afaik only messages get partial updates
instance Updateable Message where
  type Updated Message = UpdatedMessage

  update :: Updated Message -> Message -> Message
update Updated Message
n Message
o =
    Message
o
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n (Maybe b)) =>
n -> o -> o
mergeF @"content" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) old new v k.
(k ~ A_Lens, LabelOptic' f k old (Maybe v),
 LabelOptic' f k new (Maybe (MaybeNull v))) =>
new -> old -> old
updateNullableDest @"editedTimestamp" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n (Maybe b)) =>
n -> o -> o
mergeF @"tts" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n (Maybe b)) =>
n -> o -> o
mergeF @"mentionEveryone" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n (Maybe b)) =>
n -> o -> o
mergeF @"mentions" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n (Maybe b)) =>
n -> o -> o
mergeF @"mentionRoles" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n (Maybe b)) =>
n -> o -> o
mergeF @"mentionChannels" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n (Maybe b)) =>
n -> o -> o
mergeF @"attachments" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n (Maybe b)) =>
n -> o -> o
mergeF @"embeds" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n (Maybe b)) =>
n -> o -> o
mergeF @"reactions" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n (Maybe b)) =>
n -> o -> o
mergeF @"pinned" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n (Maybe b)) =>
n -> o -> o
mergeF @"type_" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) old new v k.
(k ~ A_Lens, LabelOptic' f k old (Maybe v),
 LabelOptic' f k new (Maybe (MaybeNull v))) =>
new -> old -> old
updateNullableDest @"activity" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) old new v k.
(k ~ A_Lens, LabelOptic' f k old (Maybe v),
 LabelOptic' f k new (Maybe (MaybeNull v))) =>
new -> old -> old
updateNullableDest @"application" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) old new v k.
(k ~ A_Lens, LabelOptic' f k old (Maybe v),
 LabelOptic' f k new (Maybe (MaybeNull v))) =>
new -> old -> old
updateNullableDest @"messageReference" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n (Maybe b)) =>
n -> o -> o
mergeF @"flags" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) old new v k.
(k ~ A_Lens, LabelOptic' f k old (Maybe v),
 LabelOptic' f k new (Maybe (MaybeNull v))) =>
new -> old -> old
updateNullableDest @"referencedMessage" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) old new v k.
(k ~ A_Lens, LabelOptic' f k old (Maybe v),
 LabelOptic' f k new (Maybe (MaybeNull v))) =>
new -> old -> old
updateNullableDest @"interaction" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n (Maybe b)) =>
n -> o -> o
mergeF @"components" UpdatedMessage
Updated Message
n

instance Updateable Channel where
  update :: Updated Channel -> Channel -> Channel
update Updated Channel
n Channel
_ = Channel
Updated Channel
n

instance Updateable DMChannel where
  update :: Updated DMChannel -> DMChannel -> DMChannel
update Updated DMChannel
n DMChannel
_ = DMChannel
Updated DMChannel
n

instance Updateable GuildChannel where
  update :: Updated GuildChannel -> GuildChannel -> GuildChannel
update Updated GuildChannel
n GuildChannel
_ = GuildChannel
Updated GuildChannel
n

instance Updateable Guild where
  type Updated Guild = UpdatedGuild

  update :: Updated Guild -> Guild -> Guild
update Updated Guild
n Guild
o =
    Guild
o
      Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n b) =>
n -> o -> o
setF @"name" UpdatedGuild
Updated Guild
n
      Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n b) =>
n -> o -> o
setF @"icon" UpdatedGuild
Updated Guild
n
      Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n b) =>
n -> o -> o
setF @"splash" UpdatedGuild
Updated Guild
n
      Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n b) =>
n -> o -> o
setF @"discoverySplash" UpdatedGuild
Updated Guild
n
      Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n b) =>
n -> o -> o
setF @"banner" UpdatedGuild
Updated Guild
n
      Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n b) =>
n -> o -> o
setF @"owner" UpdatedGuild
Updated Guild
n
      Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n b) =>
n -> o -> o
setF @"ownerID" UpdatedGuild
Updated Guild
n
      Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n (Maybe b)) =>
n -> o -> o
mergeF @"permissions" UpdatedGuild
Updated Guild
n
      Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n b) =>
n -> o -> o
setF @"afkChannelID" UpdatedGuild
Updated Guild
n
      Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n b) =>
n -> o -> o
setF @"afkTimeout" UpdatedGuild
Updated Guild
n
      Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n (Maybe b)) =>
n -> o -> o
mergeF @"embedEnabled" UpdatedGuild
Updated Guild
n
      Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n b) =>
n -> o -> o
setF @"embedChannelID" UpdatedGuild
Updated Guild
n
      Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n b) =>
n -> o -> o
setF @"verificationLevel" UpdatedGuild
Updated Guild
n
      Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n b) =>
n -> o -> o
setF @"defaultMessageNotifications" UpdatedGuild
Updated Guild
n
      Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n b) =>
n -> o -> o
setF @"explicitContentFilter" UpdatedGuild
Updated Guild
n
      Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n b) =>
n -> o -> o
setF @"roles" UpdatedGuild
Updated Guild
n
      Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n b) =>
n -> o -> o
setF @"features" UpdatedGuild
Updated Guild
n
      Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n b) =>
n -> o -> o
setF @"mfaLevel" UpdatedGuild
Updated Guild
n
      Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n b) =>
n -> o -> o
setF @"applicationID" UpdatedGuild
Updated Guild
n
      Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n (Maybe b)) =>
n -> o -> o
mergeF @"widgetEnabled" UpdatedGuild
Updated Guild
n
      Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n b) =>
n -> o -> o
setF @"widgetChannelID" UpdatedGuild
Updated Guild
n
      Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n b) =>
n -> o -> o
setF @"systemChannelID" UpdatedGuild
Updated Guild
n
      Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n b) =>
n -> o -> o
setF @"preferredLocale" UpdatedGuild
Updated Guild
n

instance Updateable User where
  update :: Updated User -> User -> User
update Updated User
n User
o =
    User
o
      User -> (User -> User) -> User
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n b) =>
n -> o -> o
setF @"username" User
Updated User
n
      User -> (User -> User) -> User
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n b) =>
n -> o -> o
setF @"discriminator" User
Updated User
n
      User -> (User -> User) -> User
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) old new v k.
(k ~ A_Lens, LabelOptic' f k old (Maybe v),
 LabelOptic' f k new (Maybe v)) =>
new -> old -> old
mergeF' @"bot" User
Updated User
n
      User -> (User -> User) -> User
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) o n b k.
(k ~ A_Lens, LabelOptic' f k o b, LabelOptic' f k n b) =>
n -> o -> o
setF @"avatar" User
Updated User
n
      User -> (User -> User) -> User
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) old new v k.
(k ~ A_Lens, LabelOptic' f k old (Maybe v),
 LabelOptic' f k new (Maybe v)) =>
new -> old -> old
mergeF' @"mfaEnabled" User
Updated User
n
      User -> (User -> User) -> User
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) old new v k.
(k ~ A_Lens, LabelOptic' f k old (Maybe v),
 LabelOptic' f k new (Maybe v)) =>
new -> old -> old
mergeF' @"verified" User
Updated User
n
      User -> (User -> User) -> User
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) old new v k.
(k ~ A_Lens, LabelOptic' f k old (Maybe v),
 LabelOptic' f k new (Maybe v)) =>
new -> old -> old
mergeF' @"email" User
Updated User
n
      User -> (User -> User) -> User
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) old new v k.
(k ~ A_Lens, LabelOptic' f k old (Maybe v),
 LabelOptic' f k new (Maybe v)) =>
new -> old -> old
mergeF' @"flags" User
Updated User
n
      User -> (User -> User) -> User
forall a b. a -> (a -> b) -> b
& forall (f :: Symbol) old new v k.
(k ~ A_Lens, LabelOptic' f k old (Maybe v),
 LabelOptic' f k new (Maybe v)) =>
new -> old -> old
mergeF' @"premiumType" User
Updated User
n