{-# LANGUAGE TemplateHaskell #-}

module Hledger.Cli.Commands.Printunique (
  printuniquemode
 ,printunique
)
where

import Data.List
import Hledger
import Hledger.Cli.CliOptions
import Hledger.Cli.Commands.Print

printuniquemode :: Mode RawOpts
printuniquemode = CommandDoc
-> [Flag RawOpts]
-> [(CommandDoc, [Flag RawOpts])]
-> [Flag RawOpts]
-> ([Arg RawOpts], Maybe (Arg RawOpts))
-> Mode RawOpts
hledgerCommandMode
  $(embedFileRelative "Hledger/Cli/Commands/Printunique.txt")
  []
  [(CommandDoc, [Flag RawOpts])
generalflagsgroup1]
  [Flag RawOpts]
hiddenflags
  ([], Maybe (Arg RawOpts)
forall a. Maybe a
Nothing)

printunique :: CliOpts -> Journal -> IO ()
printunique opts :: CliOpts
opts j :: Journal
j@Journal{jtxns :: Journal -> [Transaction]
jtxns=[Transaction]
ts} = do
  CliOpts -> Journal -> IO ()
print' CliOpts
opts Journal
j{jtxns :: [Transaction]
jtxns=[Transaction] -> [Transaction]
uniquify [Transaction]
ts}
  where
    uniquify :: [Transaction] -> [Transaction]
uniquify = (Transaction -> Transaction -> Bool)
-> [Transaction] -> [Transaction]
forall a. (a -> a -> Bool) -> [a] -> [a]
nubBy (\t1 :: Transaction
t1 t2 :: Transaction
t2 -> Transaction -> Text
thingToCompare Transaction
t1 Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Transaction -> Text
thingToCompare Transaction
t2) ([Transaction] -> [Transaction])
-> ([Transaction] -> [Transaction])
-> [Transaction]
-> [Transaction]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Transaction -> Text) -> [Transaction] -> [Transaction]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn Transaction -> Text
thingToCompare
    thingToCompare :: Transaction -> Text
thingToCompare = Transaction -> Text
tdescription
    -- thingToCompare = tdate