module TD.Data.TonConnectConnectItem
  (TonConnectConnectItem(..)) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import qualified Data.Text as T

-- | Represents a data item to share with a dApp
data TonConnectConnectItem
  = TonConnectConnectItemAddress -- ^ Address of the wallet
    { TonConnectConnectItem -> Maybe Text
network :: Maybe T.Text -- ^ Stringified TON network global_id (e.g. "-239" for mainnet); may be empty
    }
  | TonConnectConnectItemProof -- ^ Proof of the wallet ownership
    { TonConnectConnectItem -> Maybe Text
payload :: Maybe T.Text -- ^ Payload to be used for the proof (e.g. nonce + expiration timestamp)
    }
  deriving (TonConnectConnectItem -> TonConnectConnectItem -> Bool
(TonConnectConnectItem -> TonConnectConnectItem -> Bool)
-> (TonConnectConnectItem -> TonConnectConnectItem -> Bool)
-> Eq TonConnectConnectItem
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TonConnectConnectItem -> TonConnectConnectItem -> Bool
== :: TonConnectConnectItem -> TonConnectConnectItem -> Bool
$c/= :: TonConnectConnectItem -> TonConnectConnectItem -> Bool
/= :: TonConnectConnectItem -> TonConnectConnectItem -> Bool
Eq, Int -> TonConnectConnectItem -> ShowS
[TonConnectConnectItem] -> ShowS
TonConnectConnectItem -> String
(Int -> TonConnectConnectItem -> ShowS)
-> (TonConnectConnectItem -> String)
-> ([TonConnectConnectItem] -> ShowS)
-> Show TonConnectConnectItem
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TonConnectConnectItem -> ShowS
showsPrec :: Int -> TonConnectConnectItem -> ShowS
$cshow :: TonConnectConnectItem -> String
show :: TonConnectConnectItem -> String
$cshowList :: [TonConnectConnectItem] -> ShowS
showList :: [TonConnectConnectItem] -> ShowS
Show)

instance I.ShortShow TonConnectConnectItem where
  shortShow :: TonConnectConnectItem -> String
shortShow TonConnectConnectItemAddress
    { network :: TonConnectConnectItem -> Maybe Text
network = Maybe Text
network_
    }
      = String
"TonConnectConnectItemAddress"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"network" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
network_
        ]
  shortShow TonConnectConnectItemProof
    { payload :: TonConnectConnectItem -> Maybe Text
payload = Maybe Text
payload_
    }
      = String
"TonConnectConnectItemProof"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"payload" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
payload_
        ]

instance AT.FromJSON TonConnectConnectItem where
  parseJSON :: Value -> Parser TonConnectConnectItem
parseJSON v :: Value
v@(AT.Object Object
obj) = do
    String
t <- Object
obj Object -> Key -> Parser String
forall a. FromJSON a => Object -> Key -> Parser a
A..: Key
"@type" :: AT.Parser String

    case String
t of
      String
"tonConnectConnectItemAddress" -> Value -> Parser TonConnectConnectItem
parseTonConnectConnectItemAddress Value
v
      String
"tonConnectConnectItemProof"   -> Value -> Parser TonConnectConnectItem
parseTonConnectConnectItemProof Value
v
      String
_                              -> Parser TonConnectConnectItem
forall a. Monoid a => a
mempty
    
    where
      parseTonConnectConnectItemAddress :: A.Value -> AT.Parser TonConnectConnectItem
      parseTonConnectConnectItemAddress :: Value -> Parser TonConnectConnectItem
parseTonConnectConnectItemAddress = String
-> (Object -> Parser TonConnectConnectItem)
-> Value
-> Parser TonConnectConnectItem
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TonConnectConnectItemAddress" ((Object -> Parser TonConnectConnectItem)
 -> Value -> Parser TonConnectConnectItem)
-> (Object -> Parser TonConnectConnectItem)
-> Value
-> Parser TonConnectConnectItem
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
network_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"network"
        TonConnectConnectItem -> Parser TonConnectConnectItem
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TonConnectConnectItem -> Parser TonConnectConnectItem)
-> TonConnectConnectItem -> Parser TonConnectConnectItem
forall a b. (a -> b) -> a -> b
$ TonConnectConnectItemAddress
          { network :: Maybe Text
network = Maybe Text
network_
          }
      parseTonConnectConnectItemProof :: A.Value -> AT.Parser TonConnectConnectItem
      parseTonConnectConnectItemProof :: Value -> Parser TonConnectConnectItem
parseTonConnectConnectItemProof = String
-> (Object -> Parser TonConnectConnectItem)
-> Value
-> Parser TonConnectConnectItem
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TonConnectConnectItemProof" ((Object -> Parser TonConnectConnectItem)
 -> Value -> Parser TonConnectConnectItem)
-> (Object -> Parser TonConnectConnectItem)
-> Value
-> Parser TonConnectConnectItem
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
payload_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"payload"
        TonConnectConnectItem -> Parser TonConnectConnectItem
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TonConnectConnectItem -> Parser TonConnectConnectItem)
-> TonConnectConnectItem -> Parser TonConnectConnectItem
forall a b. (a -> b) -> a -> b
$ TonConnectConnectItemProof
          { payload :: Maybe Text
payload = Maybe Text
payload_
          }
  parseJSON Value
_ = Parser TonConnectConnectItem
forall a. Monoid a => a
mempty

instance AT.ToJSON TonConnectConnectItem where
  toJSON :: TonConnectConnectItem -> Value
toJSON TonConnectConnectItemAddress
    { network :: TonConnectConnectItem -> Maybe Text
network = Maybe Text
network_
    }
      = [Pair] -> Value
A.object
        [ Key
"@type"   Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"tonConnectConnectItemAddress"
        , Key
"network" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
network_
        ]
  toJSON TonConnectConnectItemProof
    { payload :: TonConnectConnectItem -> Maybe Text
payload = Maybe Text
payload_
    }
      = [Pair] -> Value
A.object
        [ Key
"@type"   Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"tonConnectConnectItemProof"
        , Key
"payload" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
payload_
        ]