{-|
Helpers for Ledger-compatible output.
-}

{-# LANGUAGE OverloadedStrings #-}

module Hledger.Write.Ledger (
  showTransactionLedger,
  ledgerItemRenderer,
)
where

import Data.Char (isDigit)
import Data.Text (Text)
import Data.Text qualified as T
import Data.Text.Lazy qualified as TL
import Data.Text.Lazy.Builder qualified as TB

import Hledger.Data.Amount (defaultFmt, AmountFormat(..))
import Hledger.Data.Posting (postingsAsLines, renderCommentLines)
import Hledger.Data.Transaction (showTransactionLineFirstPart)
import Hledger.Data.Types (Transaction(..), tdescription)
import Hledger.Write.Journal (ItemRenderer(..), journalItemRenderer)

ledgerFmt :: AmountFormat
ledgerFmt :: AmountFormat
ledgerFmt = AmountFormat
defaultFmt{displayLedgerLotSyntax = True}

-- | Like showTransaction, but renders cost basis using Ledger-style lot syntax
-- ({COST} [DATE] (LABEL)) instead of hledger consolidated syntax.
showTransactionLedger :: Transaction -> Text
showTransactionLedger :: Transaction -> Text
showTransactionLedger Transaction
t =
  LazyText -> Text
TL.toStrict (LazyText -> Text) -> (Builder -> LazyText) -> Builder -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> LazyText
TB.toLazyText (Builder -> Text) -> Builder -> Text
forall a b. (a -> b) -> a -> b
$
      Text -> Builder
TB.fromText (Transaction -> Text
tprecedingcomment Transaction
t)
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Text -> Builder
TB.fromText Text
descriptionline Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
newline
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> (Text -> Builder) -> [Text] -> Builder
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ((Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
newline) (Builder -> Builder) -> (Text -> Builder) -> Text -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Builder
TB.fromText) [Text]
newlinecomments
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> (Text -> Builder) -> [Text] -> Builder
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ((Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
newline) (Builder -> Builder) -> (Text -> Builder) -> Text -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Builder
TB.fromText) (AmountFormat -> Bool -> [Posting] -> [Text]
postingsAsLines AmountFormat
ledgerFmt Bool
False ([Posting] -> [Text]) -> [Posting] -> [Text]
forall a b. (a -> b) -> a -> b
$ Transaction -> [Posting]
tpostings Transaction
t)
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
newline
  where
    descriptionline :: Text
descriptionline = Text -> Text
T.stripEnd (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Transaction -> Text
showTransactionLineFirstPart Transaction
t Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Text] -> Text
T.concat [Text
desc, Text
samelinecomment]
    desc :: Text
desc = if Text -> Bool
T.null Text
d then Text
"" else Text
" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
d where d :: Text
d = Transaction -> Text
tdescription Transaction
t
    (Text
samelinecomment, [Text]
newlinecomments) =
      case Text -> [Text]
renderCommentLines (Transaction -> Text
tcomment Transaction
t) of []   -> (Text
"",[])
                                              Text
c:[Text]
cs -> (Text
c,[Text]
cs)
    newline :: Builder
newline = Char -> Builder
TB.singleton Char
'\n'

-- | An item renderer for Ledger output (print --export -O ledger): like the journal one,
-- but transactions use Ledger lot syntax, and directives which hledger accepts but Ledger
-- (detectably) does not are commented out, with an explanatory comment.
ledgerItemRenderer :: ItemRenderer
ledgerItemRenderer :: ItemRenderer
ledgerItemRenderer = ((Transaction -> Text) -> ItemRenderer
journalItemRenderer Transaction -> Text
showTransactionLedger){ irDirective = Just . ledgerDirective }

-- | Reproduce a directive for Ledger, commenting it out with a note if it is one of
-- the hledger directive forms known not to be supported by Ledger.
ledgerDirective :: Text -> Text
ledgerDirective :: Text -> Text
ledgerDirective Text
txt
  | Text -> Bool
isLedgerIncompatible Text
txt = Text
"; not supported as-is:\n" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Text] -> Text
T.unlines ((Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Text
"; " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>) ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ Text -> [Text]
T.lines Text
txt)
  | Bool
otherwise = Text
txt

-- | Detect some hledger directive forms which Ledger does not support:
-- the decimal-mark directive; a one-line commodity directive with an amount
-- (Ledger uses a format subdirective); a periodic transaction rule with a description
-- (Ledger would read it as part of the period expression); and auto posting rules
-- using hledger's *N amount multipliers.
isLedgerIncompatible :: Text -> Bool
isLedgerIncompatible :: Text -> Bool
isLedgerIncompatible Text
txt = case Text -> [Text]
T.lines Text
txt of
  [] -> Bool
False
  Text
firstline : [Text]
otherlines -> case Text -> [Text]
T.words Text
firstline of
    Text
"decimal-mark" : [Text]
_    -> Bool
True
    Text
"commodity" : Text
sym : [Text]
_ -> (Char -> Bool) -> Text -> Bool
T.any Char -> Bool
isDigit Text
sym
    Text
"~" : [Text]
_               -> Text
"  " Text -> Text -> Bool
`T.isInfixOf` (Text -> Text
T.strip (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> Text -> Text
T.takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/=Char
';') (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Int -> Text -> Text
T.drop Int
1 Text
firstline)
    Text
"=" : [Text]
_               -> (Text -> Bool) -> [Text] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Text -> Bool
hasMultiplier [Text]
otherlines
    [Text]
_                     -> Bool
False
  where
    hasMultiplier :: Text -> Bool
hasMultiplier Text
l = (Char -> Bool) -> Text -> Bool
T.any Char -> Bool
isDigit (Text -> Bool) -> Text -> Bool
forall a b. (a -> b) -> a -> b
$ Int -> Text -> Text
T.take Int
1 (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Int -> Text -> Text
T.drop Int
1 (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ (Text, Text) -> Text
forall a b. (a, b) -> b
snd ((Text, Text) -> Text) -> (Text, Text) -> Text
forall a b. (a -> b) -> a -> b
$ HasCallStack => Text -> Text -> (Text, Text)
Text -> Text -> (Text, Text)
T.breakOn Text
"*" Text
l