module TD.Query.SearchQuote
  (SearchQuote(..)
  , defaultSearchQuote
  ) 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

-- | Searches for a given quote in a text. Returns found quote start position in UTF-16 code units. Returns a 404 error if the quote is not found. Can be called synchronously. Returns 'TD.Data.FoundPosition.FoundPosition'
data SearchQuote
  = SearchQuote
    { SearchQuote -> Maybe FormattedText
text           :: Maybe FormattedText.FormattedText -- ^ Text in which to search for the quote
    , SearchQuote -> Maybe FormattedText
quote          :: Maybe FormattedText.FormattedText -- ^ Quote to search for
    , SearchQuote -> Maybe Int
quote_position :: Maybe Int                         -- ^ Approximate quote position in UTF-16 code units
    }
  deriving (SearchQuote -> SearchQuote -> Bool
(SearchQuote -> SearchQuote -> Bool)
-> (SearchQuote -> SearchQuote -> Bool) -> Eq SearchQuote
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SearchQuote -> SearchQuote -> Bool
== :: SearchQuote -> SearchQuote -> Bool
$c/= :: SearchQuote -> SearchQuote -> Bool
/= :: SearchQuote -> SearchQuote -> Bool
Eq, Int -> SearchQuote -> ShowS
[SearchQuote] -> ShowS
SearchQuote -> String
(Int -> SearchQuote -> ShowS)
-> (SearchQuote -> String)
-> ([SearchQuote] -> ShowS)
-> Show SearchQuote
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SearchQuote -> ShowS
showsPrec :: Int -> SearchQuote -> ShowS
$cshow :: SearchQuote -> String
show :: SearchQuote -> String
$cshowList :: [SearchQuote] -> ShowS
showList :: [SearchQuote] -> ShowS
Show)

instance I.ShortShow SearchQuote where
  shortShow :: SearchQuote -> String
shortShow
    SearchQuote
      { text :: SearchQuote -> Maybe FormattedText
text           = Maybe FormattedText
text_
      , quote :: SearchQuote -> Maybe FormattedText
quote          = Maybe FormattedText
quote_
      , quote_position :: SearchQuote -> Maybe Int
quote_position = Maybe Int
quote_position_
      }
        = String
"SearchQuote"
          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
"quote"          String -> Maybe FormattedText -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe FormattedText
quote_
          , String
"quote_position" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
quote_position_
          ]

instance AT.ToJSON SearchQuote where
  toJSON :: SearchQuote -> Value
toJSON
    SearchQuote
      { text :: SearchQuote -> Maybe FormattedText
text           = Maybe FormattedText
text_
      , quote :: SearchQuote -> Maybe FormattedText
quote          = Maybe FormattedText
quote_
      , quote_position :: SearchQuote -> Maybe Int
quote_position = Maybe Int
quote_position_
      }
        = [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
"searchQuote"
          , Key
"text"           Key -> Maybe FormattedText -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe FormattedText
text_
          , Key
"quote"          Key -> Maybe FormattedText -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe FormattedText
quote_
          , Key
"quote_position" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
quote_position_
          ]

defaultSearchQuote :: SearchQuote
defaultSearchQuote :: SearchQuote
defaultSearchQuote =
  SearchQuote
    { text :: Maybe FormattedText
text           = Maybe FormattedText
forall a. Maybe a
Nothing
    , quote :: Maybe FormattedText
quote          = Maybe FormattedText
forall a. Maybe a
Nothing
    , quote_position :: Maybe Int
quote_position = Maybe Int
forall a. Maybe a
Nothing
    }