    {-# LANGUAGE OverloadedStrings    #-}
    {- |
    Copyright               : © 2021-2026 Albert Krewinkel
    SPDX-License-Identifier : MIT
    Maintainer              : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>

    Marshaling/unmarshaling functions and constructor for 'Citation' values.
    -}
    module Text.Pandoc.Lua.Marshal.Citation
      ( -- * Citation
        peekCitation
      , pushCitation
      , typeCitation
      , mkCitation
      ) where

    import Control.Applicative (optional)
    import Data.Aeson (encode)
    import Data.Maybe (fromMaybe)
    import HsLua as Lua
    import Text.Pandoc.Definition (Citation (..))
    import Text.Pandoc.Lua.Marshal.CitationMode (peekCitationMode, pushCitationMode)
    import {-# SOURCE #-} Text.Pandoc.Lua.Marshal.Inline
      ( peekInlinesFuzzy, pushInlines )

    -- | Pushes a Citation value as userdata object.
    pushCitation :: LuaError e
                 => Pusher e Citation
    pushCitation :: forall e. LuaError e => Pusher e Citation
    pushCitation = DocumentedTypeWithList e Citation (ZonkAny 3)
    -> Citation -> LuaE e ()
    forall e a itemtype.
    LuaError e =>
    DocumentedTypeWithList e a itemtype -> a -> LuaE e ()
    pushUD DocumentedTypeWithList e Citation (ZonkAny 3)
    forall e. LuaError e => DocumentedType e Citation
    typeCitation
    {-# INLINE pushCitation #-}

    -- | Retrieves a Citation value.
    peekCitation :: LuaError e
                 => Peeker e Citation
    peekCitation :: forall e. LuaError e => Peeker e Citation
    peekCitation = DocumentedTypeWithList e Citation (ZonkAny 0) -> Peeker e Citation
    forall e a itemtype.
    LuaError e =>
    DocumentedTypeWithList e a itemtype -> Peeker e a
    peekUD  DocumentedTypeWithList e Citation (ZonkAny 0)
    forall e. LuaError e => DocumentedType e Citation
    typeCitation
    {-# INLINE peekCitation #-}

    -- | Citation object type.
    typeCitation :: LuaError e
                 => DocumentedType e Citation
    typeCitation :: forall e. LuaError e => DocumentedType e Citation
    typeCitation = Name
    -> [(Operation, DocumentedFunction e)]
    -> [Member e (DocumentedFunction e) Citation]
    -> DocumentedType e Citation
    forall e a.
    LuaError e =>
    Name
    -> [(Operation, DocumentedFunction e)]
    -> [Member e (DocumentedFunction e) a]
    -> DocumentedType e a
    deftype Name
    "Citation"
      [ Operation
    -> DocumentedFunction e -> (Operation, DocumentedFunction e)
    forall e.
    Operation
    -> DocumentedFunction e -> (Operation, DocumentedFunction e)
    operation Operation
    Eq (DocumentedFunction e -> (Operation, DocumentedFunction e))
    -> DocumentedFunction e -> (Operation, DocumentedFunction e)
    forall a b. (a -> b) -> a -> b
    $ (Maybe Citation -> Maybe Citation -> LuaE e Bool)
    -> HsFnPrecursor
         e (Maybe Citation -> Maybe Citation -> LuaE e Bool)
    forall a e. a -> HsFnPrecursor e a
    lambda
        ### liftPure2 (\a b -> fromMaybe False ((==) <$> a <*> b))
        HsFnPrecursor e (Maybe Citation -> Maybe Citation -> LuaE e Bool)
    -> Parameter e (Maybe Citation)
    -> HsFnPrecursor e (Maybe Citation -> LuaE e Bool)
    forall e a b.
    HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
    <#> Peeker e (Maybe Citation)
    -> TypeSpec -> Text -> Text -> Parameter e (Maybe Citation)
    forall e a. Peeker e a -> TypeSpec -> Text -> Text -> Parameter e a
    parameter (Peek e Citation -> Peek e (Maybe Citation)
    forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
    optional (Peek e Citation -> Peek e (Maybe Citation))
    -> (StackIndex -> Peek e Citation) -> Peeker e (Maybe Citation)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . StackIndex -> Peek e Citation
    forall e. LuaError e => Peeker e Citation
    peekCitation) TypeSpec
    "Citation" Text
    "a" Text
    ""
        HsFnPrecursor e (Maybe Citation -> LuaE e Bool)
    -> Parameter e (Maybe Citation) -> HsFnPrecursor e (LuaE e Bool)
    forall e a b.
    HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
    <#> Peeker e (Maybe Citation)
    -> TypeSpec -> Text -> Text -> Parameter e (Maybe Citation)
    forall e a. Peeker e a -> TypeSpec -> Text -> Text -> Parameter e a
    parameter (Peek e Citation -> Peek e (Maybe Citation)
    forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
    optional (Peek e Citation -> Peek e (Maybe Citation))
    -> (StackIndex -> Peek e Citation) -> Peeker e (Maybe Citation)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . StackIndex -> Peek e Citation
    forall e. LuaError e => Peeker e Citation
    peekCitation) TypeSpec
    "Citation" Text
    "b" Text
    ""
        HsFnPrecursor e (LuaE e Bool)
    -> FunctionResults e Bool -> DocumentedFunction e
    forall e a.
    HsFnPrecursor e (LuaE e a)
    -> FunctionResults e a -> DocumentedFunction e
    =#> Pusher e Bool -> TypeSpec -> Text -> FunctionResults e Bool
    forall e a. Pusher e a -> TypeSpec -> Text -> FunctionResults e a
    functionResult Pusher e Bool
    forall e. Pusher e Bool
    pushBool TypeSpec
    "boolean" Text
    "true iff the citations are equal"

      , Operation
    -> DocumentedFunction e -> (Operation, DocumentedFunction e)
    forall e.
    Operation
    -> DocumentedFunction e -> (Operation, DocumentedFunction e)
    operation Operation
    Tostring (DocumentedFunction e -> (Operation, DocumentedFunction e))
    -> DocumentedFunction e -> (Operation, DocumentedFunction e)
    forall a b. (a -> b) -> a -> b
    $ (Citation -> LuaE e String)
    -> HsFnPrecursor e (Citation -> LuaE e String)
    forall a e. a -> HsFnPrecursor e a
    lambda
        ### liftPure show
        HsFnPrecursor e (Citation -> LuaE e String)
    -> Parameter e Citation -> HsFnPrecursor e (LuaE e String)
    forall e a b.
    HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
    <#> (StackIndex -> Peek e Citation)
    -> TypeSpec -> Text -> Text -> Parameter e Citation
    forall e a. Peeker e a -> TypeSpec -> Text -> Text -> Parameter e a
    parameter StackIndex -> Peek e Citation
    forall e. LuaError e => Peeker e Citation
    peekCitation TypeSpec
    "Citation" Text
    "citation" Text
    ""
        HsFnPrecursor e (LuaE e String)
    -> FunctionResults e String -> DocumentedFunction e
    forall e a.
    HsFnPrecursor e (LuaE e a)
    -> FunctionResults e a -> DocumentedFunction e
    =#> Pusher e String -> TypeSpec -> Text -> FunctionResults e String
    forall e a. Pusher e a -> TypeSpec -> Text -> FunctionResults e a
    functionResult Pusher e String
    forall e. String -> LuaE e ()
    pushString TypeSpec
    "string" Text
    "native Haskell representation"
      , Operation
    -> DocumentedFunction e -> (Operation, DocumentedFunction e)
    forall e.
    Operation
    -> DocumentedFunction e -> (Operation, DocumentedFunction e)
    operation (Name -> Operation
    CustomOperation Name
    "__tojson") (DocumentedFunction e -> (Operation, DocumentedFunction e))
    -> DocumentedFunction e -> (Operation, DocumentedFunction e)
    forall a b. (a -> b) -> a -> b
    $ (Citation -> LuaE e ByteString)
    -> HsFnPrecursor e (Citation -> LuaE e ByteString)
    forall a e. a -> HsFnPrecursor e a
    lambda
        ### liftPure encode
        HsFnPrecursor e (Citation -> LuaE e ByteString)
    -> Parameter e Citation -> HsFnPrecursor e (LuaE e ByteString)
    forall e a b.
    HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
    <#> DocumentedType e Citation -> Text -> Text -> Parameter e Citation
    forall e a itemtype.
    LuaError e =>
    DocumentedTypeWithList e a itemtype
    -> Text -> Text -> Parameter e a
    udparam DocumentedType e Citation
    forall e. LuaError e => DocumentedType e Citation
    typeCitation Text
    "self" Text
    ""
        HsFnPrecursor e (LuaE e ByteString)
    -> FunctionResults e ByteString -> DocumentedFunction e
    forall e a.
    HsFnPrecursor e (LuaE e a)
    -> FunctionResults e a -> DocumentedFunction e
    =#> Pusher e ByteString
    -> TypeSpec -> Text -> FunctionResults e ByteString
    forall e a. Pusher e a -> TypeSpec -> Text -> FunctionResults e a
    functionResult Pusher e ByteString
    forall e. Pusher e ByteString
    pushLazyByteString TypeSpec
    "string" Text
    "JSON representation"
      ]
      [ Name
    -> Text
    -> (Pusher e Text, Citation -> Text)
    -> (Peeker e Text, Citation -> Text -> Citation)
    -> Member e (DocumentedFunction e) Citation
    forall e b a fn.
    LuaError e =>
    Name
    -> Text
    -> (Pusher e b, a -> b)
    -> (Peeker e b, a -> b -> a)
    -> Member e fn a
    property Name
    "id" Text
    "citation ID / key"
          (Pusher e Text
    forall e. Pusher e Text
    pushText, Citation -> Text
    citationId)
          (Peeker e Text
    forall e. Peeker e Text
    peekText, \Citation
    citation Text
    cid -> Citation
    citation{ citationId = cid })
      , Name
    -> Text
    -> (Pusher e CitationMode, Citation -> CitationMode)
    -> (Peeker e CitationMode, Citation -> CitationMode -> Citation)
    -> Member e (DocumentedFunction e) Citation
    forall e b a fn.
    LuaError e =>
    Name
    -> Text
    -> (Pusher e b, a -> b)
    -> (Peeker e b, a -> b -> a)
    -> Member e fn a
    property Name
    "mode" Text
    "citation mode"
          (Pusher e CitationMode
    forall e. Pusher e CitationMode
    pushCitationMode, Citation -> CitationMode
    citationMode)
          (Peeker e CitationMode
    forall e. Peeker e CitationMode
    peekCitationMode, \Citation
    citation CitationMode
    mode -> Citation
    citation{ citationMode = mode })
      , Name
    -> Text
    -> (Pusher e [Inline], Citation -> [Inline])
    -> (Peeker e [Inline], Citation -> [Inline] -> Citation)
    -> Member e (DocumentedFunction e) Citation
    forall e b a fn.
    LuaError e =>
    Name
    -> Text
    -> (Pusher e b, a -> b)
    -> (Peeker e b, a -> b -> a)
    -> Member e fn a
    property Name
    "prefix" Text
    "citation prefix"
          (Pusher e [Inline]
    forall e. LuaError e => Pusher e [Inline]
    pushInlines, Citation -> [Inline]
    citationPrefix)
          (Peeker e [Inline]
    forall e. LuaError e => Peeker e [Inline]
    peekInlinesFuzzy, \Citation
    citation [Inline]
    prefix -> Citation
    citation{ citationPrefix = prefix })
      , Name
    -> Text
    -> (Pusher e [Inline], Citation -> [Inline])
    -> (Peeker e [Inline], Citation -> [Inline] -> Citation)
    -> Member e (DocumentedFunction e) Citation
    forall e b a fn.
    LuaError e =>
    Name
    -> Text
    -> (Pusher e b, a -> b)
    -> (Peeker e b, a -> b -> a)
    -> Member e fn a
    property Name
    "suffix" Text
    "citation suffix"
          (Pusher e [Inline]
    forall e. LuaError e => Pusher e [Inline]
    pushInlines, Citation -> [Inline]
    citationSuffix)
          (Peeker e [Inline]
    forall e. LuaError e => Peeker e [Inline]
    peekInlinesFuzzy, \Citation
    citation [Inline]
    suffix -> Citation
    citation{ citationSuffix = suffix })
      , Name
    -> Text
    -> (Pusher e Int, Citation -> Int)
    -> (Peeker e Int, Citation -> Int -> Citation)
    -> Member e (DocumentedFunction e) Citation
    forall e b a fn.
    LuaError e =>
    Name
    -> Text
    -> (Pusher e b, a -> b)
    -> (Peeker e b, a -> b -> a)
    -> Member e fn a
    property Name
    "note_num" Text
    "note number"
          (Pusher e Int
    forall a e. (Integral a, Show a) => a -> LuaE e ()
    pushIntegral, Citation -> Int
    citationNoteNum)
          (Peeker e Int
    forall a e. (Integral a, Read a) => Peeker e a
    peekIntegral, \Citation
    citation Int
    noteNum -> Citation
    citation{ citationNoteNum = noteNum })
      , Name
    -> Text
    -> (Pusher e Int, Citation -> Int)
    -> (Peeker e Int, Citation -> Int -> Citation)
    -> Member e (DocumentedFunction e) Citation
    forall e b a fn.
    LuaError e =>
    Name
    -> Text
    -> (Pusher e b, a -> b)
    -> (Peeker e b, a -> b -> a)
    -> Member e fn a
    property Name
    "hash" Text
    "hash number"
          (Pusher e Int
    forall a e. (Integral a, Show a) => a -> LuaE e ()
    pushIntegral, Citation -> Int
    citationHash)
          (Peeker e Int
    forall a e. (Integral a, Read a) => Peeker e a
    peekIntegral, \Citation
    citation Int
    hash -> Citation
    citation{ citationHash = hash })
      , DocumentedFunction e -> Member e (DocumentedFunction e) Citation
    forall e a.
    DocumentedFunction e -> Member e (DocumentedFunction e) a
    method (DocumentedFunction e -> Member e (DocumentedFunction e) Citation)
    -> DocumentedFunction e -> Member e (DocumentedFunction e) Citation
    forall a b. (a -> b) -> a -> b
    $ Name
    -> (Citation -> LuaE e Citation)
    -> HsFnPrecursor e (Citation -> LuaE e Citation)
    forall a e. Name -> a -> HsFnPrecursor e a
    defun Name
    "clone"
        ### return
        HsFnPrecursor e (Citation -> LuaE e Citation)
    -> Parameter e Citation -> HsFnPrecursor e (LuaE e Citation)
    forall e a b.
    HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
    <#> DocumentedType e Citation -> Text -> Text -> Parameter e Citation
    forall e a itemtype.
    LuaError e =>
    DocumentedTypeWithList e a itemtype
    -> Text -> Text -> Parameter e a
    udparam DocumentedType e Citation
    forall e. LuaError e => DocumentedType e Citation
    typeCitation Text
    "obj" Text
    ""
        HsFnPrecursor e (LuaE e Citation)
    -> FunctionResults e Citation -> DocumentedFunction e
    forall e a.
    HsFnPrecursor e (LuaE e a)
    -> FunctionResults e a -> DocumentedFunction e
    =#> Pusher e Citation -> TypeSpec -> Text -> FunctionResults e Citation
    forall e a. Pusher e a -> TypeSpec -> Text -> FunctionResults e a
    functionResult Pusher e Citation
    forall e. LuaError e => Pusher e Citation
    pushCitation TypeSpec
    "Citation" Text
    "copy of obj"
      ]
    {-# INLINABLE typeCitation #-}

    -- | Constructor function for 'Citation' elements.
    mkCitation :: LuaError e => DocumentedFunction e
    mkCitation :: forall e. LuaError e => DocumentedFunction e
    mkCitation = Name
    -> (Text
        -> CitationMode
        -> Maybe [Inline]
        -> Maybe [Inline]
        -> Maybe Int
        -> Maybe Int
        -> LuaE e Citation)
    -> HsFnPrecursor
         e
         (Text
          -> CitationMode
          -> Maybe [Inline]
          -> Maybe [Inline]
          -> Maybe Int
          -> Maybe Int
          -> LuaE e Citation)
    forall a e. Name -> a -> HsFnPrecursor e a
    defun Name
    "Citation"
      ### (\cid mode mprefix msuffix mnote_num mhash ->
             cid `seq` mode `seq` mprefix `seq` msuffix `seq`
             mnote_num `seq` mhash `seq` return $! Citation
               { citationId = cid
               , citationMode = mode
               , citationPrefix = fromMaybe mempty mprefix
               , citationSuffix = fromMaybe mempty msuffix
               , citationNoteNum = fromMaybe 0 mnote_num
               , citationHash = fromMaybe 0 mhash
               })
      HsFnPrecursor
      e
      (Text
       -> CitationMode
       -> Maybe [Inline]
       -> Maybe [Inline]
       -> Maybe Int
       -> Maybe Int
       -> LuaE e Citation)
    -> Parameter e Text
    -> HsFnPrecursor
         e
         (CitationMode
          -> Maybe [Inline]
          -> Maybe [Inline]
          -> Maybe Int
          -> Maybe Int
          -> LuaE e Citation)
    forall e a b.
    HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
    <#> Text -> Text -> Parameter e Text
    forall e. Text -> Text -> Parameter e Text
    textParam Text
    "id" Text
    "citation ID (e.g. BibTeX key)"
      HsFnPrecursor
      e
      (CitationMode
       -> Maybe [Inline]
       -> Maybe [Inline]
       -> Maybe Int
       -> Maybe Int
       -> LuaE e Citation)
    -> Parameter e CitationMode
    -> HsFnPrecursor
         e
         (Maybe [Inline]
          -> Maybe [Inline] -> Maybe Int -> Maybe Int -> LuaE e Citation)
    forall e a b.
    HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
    <#> Peeker e CitationMode
    -> TypeSpec -> Text -> Text -> Parameter e CitationMode
    forall e a. Peeker e a -> TypeSpec -> Text -> Text -> Parameter e a
    parameter Peeker e CitationMode
    forall e. Peeker e CitationMode
    peekCitationMode TypeSpec
    "CitationMode" Text
    "mode" Text
    "citation rendering mode"
      HsFnPrecursor
      e
      (Maybe [Inline]
       -> Maybe [Inline] -> Maybe Int -> Maybe Int -> LuaE e Citation)
    -> Parameter e (Maybe [Inline])
    -> HsFnPrecursor
         e (Maybe [Inline] -> Maybe Int -> Maybe Int -> LuaE e Citation)
    forall e a b.
    HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
    <#> Parameter e [Inline] -> Parameter e (Maybe [Inline])
    forall e a. Parameter e a -> Parameter e (Maybe a)
    opt (Peeker e [Inline]
    -> TypeSpec -> Text -> Text -> Parameter e [Inline]
    forall e a. Peeker e a -> TypeSpec -> Text -> Text -> Parameter e a
    parameter Peeker e [Inline]
    forall e. LuaError e => Peeker e [Inline]
    peekInlinesFuzzy TypeSpec
    "Inlines" Text
    "prefix" Text
    "")
      HsFnPrecursor
      e (Maybe [Inline] -> Maybe Int -> Maybe Int -> LuaE e Citation)
    -> Parameter e (Maybe [Inline])
    -> HsFnPrecursor e (Maybe Int -> Maybe Int -> LuaE e Citation)
    forall e a b.
    HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
    <#> Parameter e [Inline] -> Parameter e (Maybe [Inline])
    forall e a. Parameter e a -> Parameter e (Maybe a)
    opt (Peeker e [Inline]
    -> TypeSpec -> Text -> Text -> Parameter e [Inline]
    forall e a. Peeker e a -> TypeSpec -> Text -> Text -> Parameter e a
    parameter Peeker e [Inline]
    forall e. LuaError e => Peeker e [Inline]
    peekInlinesFuzzy TypeSpec
    "Inlines" Text
    "suffix" Text
    "")
      HsFnPrecursor e (Maybe Int -> Maybe Int -> LuaE e Citation)
    -> Parameter e (Maybe Int)
    -> HsFnPrecursor e (Maybe Int -> LuaE e Citation)
    forall e a b.
    HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
    <#> Parameter e Int -> Parameter e (Maybe Int)
    forall e a. Parameter e a -> Parameter e (Maybe a)
    opt (Text -> Text -> Parameter e Int
    forall a e. (Read a, Integral a) => Text -> Text -> Parameter e a
    integralParam Text
    "note_num" Text
    "note number")
      HsFnPrecursor e (Maybe Int -> LuaE e Citation)
    -> Parameter e (Maybe Int) -> HsFnPrecursor e (LuaE e Citation)
    forall e a b.
    HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
    <#> Parameter e Int -> Parameter e (Maybe Int)
    forall e a. Parameter e a -> Parameter e (Maybe a)
    opt (Text -> Text -> Parameter e Int
    forall a e. (Read a, Integral a) => Text -> Text -> Parameter e a
    integralParam Text
    "hash"     Text
    "hash number")
      HsFnPrecursor e (LuaE e Citation)
    -> FunctionResults e Citation -> DocumentedFunction e
    forall e a.
    HsFnPrecursor e (LuaE e a)
    -> FunctionResults e a -> DocumentedFunction e
    =#> Pusher e Citation -> TypeSpec -> Text -> FunctionResults e Citation
    forall e a. Pusher e a -> TypeSpec -> Text -> FunctionResults e a
    functionResult Pusher e Citation
    forall e. LuaError e => Pusher e Citation
    pushCitation TypeSpec
    "Citation" Text
    "new citation object"
      #? "Creates a single citation."