-- | Modify existing message to include completion message
module Sound.Sc3.Server.Command.Completion where

import Sound.Osc.Core {- hosc -}

import qualified Sound.Sc3.Server.Command.Enum as Server.Command.Enum {- hsc3 -}

-- | Add a completion packet to an existing asynchronous command.
with_completion_packet :: Message -> PacketOf Message -> Message
with_completion_packet :: Message -> PacketOf Message -> Message
with_completion_packet (Message Address_Pattern
c [Datum]
xs) PacketOf Message
cm =
  if Address_Pattern
c Address_Pattern -> [Address_Pattern] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Address_Pattern]
Server.Command.Enum.async_cmds
    then
      let xs' :: [Datum]
xs' = [Datum]
xs [Datum] -> [Datum] -> [Datum]
forall a. [a] -> [a] -> [a]
++ [Blob -> Datum
Blob (PacketOf Message -> Blob
encodePacket PacketOf Message
cm)]
      in Address_Pattern -> [Datum] -> Message
Message Address_Pattern
c [Datum]
xs'
    else Address_Pattern -> Message
forall a. HasCallStack => Address_Pattern -> a
error (Address_Pattern
"with_completion_packet: not async: " Address_Pattern -> Address_Pattern -> Address_Pattern
forall a. [a] -> [a] -> [a]
++ Address_Pattern
c)

{- | Add a completion message to an existing asynchronous command.

>>> import Sound.Sc3.Server.Command.Plain
>>> let m = n_set1 0 "0" 0
>>> let e = encodeMessage m
>>> withCm (b_close 0) m == Message "/b_close" [Int32 0,Blob e]
True
-}
withCm :: Message -> Message -> Message
withCm :: Message -> Message -> Message
withCm Message
m Message
cm = Message -> PacketOf Message -> Message
with_completion_packet Message
m (Message -> PacketOf Message
forall t. Message -> PacketOf t
Packet_Message Message
cm)