module TD.Data.FixedText
  (FixedText(..)) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import qualified TD.Data.FormattedText as FormattedText
import qualified TD.Data.DiffText as DiffText

data FixedText
  = FixedText -- ^ A text fixed using fixTextWithAi
    { FixedText -> Maybe FormattedText
text      :: Maybe FormattedText.FormattedText -- ^ The resulting text
    , FixedText -> Maybe DiffText
diff_text :: Maybe DiffText.DiffText           -- ^ Changes made to the original text
    }
  deriving (FixedText -> FixedText -> Bool
(FixedText -> FixedText -> Bool)
-> (FixedText -> FixedText -> Bool) -> Eq FixedText
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FixedText -> FixedText -> Bool
== :: FixedText -> FixedText -> Bool
$c/= :: FixedText -> FixedText -> Bool
/= :: FixedText -> FixedText -> Bool
Eq, Int -> FixedText -> ShowS
[FixedText] -> ShowS
FixedText -> String
(Int -> FixedText -> ShowS)
-> (FixedText -> String)
-> ([FixedText] -> ShowS)
-> Show FixedText
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FixedText -> ShowS
showsPrec :: Int -> FixedText -> ShowS
$cshow :: FixedText -> String
show :: FixedText -> String
$cshowList :: [FixedText] -> ShowS
showList :: [FixedText] -> ShowS
Show)

instance I.ShortShow FixedText where
  shortShow :: FixedText -> String
shortShow FixedText
    { text :: FixedText -> Maybe FormattedText
text      = Maybe FormattedText
text_
    , diff_text :: FixedText -> Maybe DiffText
diff_text = Maybe DiffText
diff_text_
    }
      = String
"FixedText"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"text"      String -> Maybe FormattedText -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe FormattedText
text_
        , String
"diff_text" String -> Maybe DiffText -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe DiffText
diff_text_
        ]

instance AT.FromJSON FixedText where
  parseJSON :: Value -> Parser FixedText
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
"fixedText" -> Value -> Parser FixedText
parseFixedText Value
v
      String
_           -> Parser FixedText
forall a. Monoid a => a
mempty
    
    where
      parseFixedText :: A.Value -> AT.Parser FixedText
      parseFixedText :: Value -> Parser FixedText
parseFixedText = String -> (Object -> Parser FixedText) -> Value -> Parser FixedText
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"FixedText" ((Object -> Parser FixedText) -> Value -> Parser FixedText)
-> (Object -> Parser FixedText) -> Value -> Parser FixedText
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe FormattedText
text_      <- Object
o Object -> Key -> Parser (Maybe FormattedText)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"text"
        Maybe DiffText
diff_text_ <- Object
o Object -> Key -> Parser (Maybe DiffText)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"diff_text"
        FixedText -> Parser FixedText
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (FixedText -> Parser FixedText) -> FixedText -> Parser FixedText
forall a b. (a -> b) -> a -> b
$ FixedText
          { text :: Maybe FormattedText
text      = Maybe FormattedText
text_
          , diff_text :: Maybe DiffText
diff_text = Maybe DiffText
diff_text_
          }
  parseJSON Value
_ = Parser FixedText
forall a. Monoid a => a
mempty