module Darcs.Patch.Inspect
       ( PatchInspect(..)
       )
       where

import Darcs.Prelude

import Darcs.Patch.Witnesses.Ordered ( FL, RL, reverseRL, mapFL )
import Darcs.Util.Path ( AnchoredPath )

import qualified Data.ByteString.Char8 as BC
import Data.List ( nub )

-- TODO Whether a patch touches a given file is not an invariant property of a
-- patch: it depends on the context i.e. it changes when we re-order patches.
-- Can we define an interface where this becomes an invariant property?

-- TODO This interface only makes sense if @ApplyState p ~ Tree@. To support
-- other ApplyStates we need to devise an abstraction for "objects" of the
-- ApplyState.

class PatchInspect p where
    listTouchedFiles :: p wX wY -> [AnchoredPath]
    hunkMatches :: (BC.ByteString -> Bool) -> p wX wY -> Bool

instance PatchInspect p => PatchInspect (FL p) where
    listTouchedFiles :: forall wX wY. FL p wX wY -> [AnchoredPath]
listTouchedFiles FL p wX wY
xs = forall a. Eq a => [a] -> [a]
nub forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ forall (a :: * -> * -> *) b wX wY.
(forall wW wZ. a wW wZ -> b) -> FL a wX wY -> [b]
mapFL forall (p :: * -> * -> *) wX wY.
PatchInspect p =>
p wX wY -> [AnchoredPath]
listTouchedFiles FL p wX wY
xs
    hunkMatches :: forall wX wY. (ByteString -> Bool) -> FL p wX wY -> Bool
hunkMatches ByteString -> Bool
f = forall (t :: * -> *). Foldable t => t Bool -> Bool
or forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: * -> * -> *) b wX wY.
(forall wW wZ. a wW wZ -> b) -> FL a wX wY -> [b]
mapFL (forall (p :: * -> * -> *) wX wY.
PatchInspect p =>
(ByteString -> Bool) -> p wX wY -> Bool
hunkMatches ByteString -> Bool
f)

instance PatchInspect p => PatchInspect (RL p) where
    listTouchedFiles :: forall wX wY. RL p wX wY -> [AnchoredPath]
listTouchedFiles = forall (p :: * -> * -> *) wX wY.
PatchInspect p =>
p wX wY -> [AnchoredPath]
listTouchedFiles forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: * -> * -> *) wX wZ. RL a wX wZ -> FL a wX wZ
reverseRL
    hunkMatches :: forall wX wY. (ByteString -> Bool) -> RL p wX wY -> Bool
hunkMatches ByteString -> Bool
f = forall (p :: * -> * -> *) wX wY.
PatchInspect p =>
(ByteString -> Bool) -> p wX wY -> Bool
hunkMatches ByteString -> Bool
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: * -> * -> *) wX wZ. RL a wX wZ -> FL a wX wZ
reverseRL