module Charon.Backend.Fdo.BackendArgs
  ( backendArgs,
    backendArgsDirectorySizes,
  )
where

import Charon.Backend.Data (Backend (BackendFdo))
import Charon.Backend.Default.BackendArgs
  ( BackendArgs
      ( MkBackendArgs,
        backend,
        fromCorePathData,
        makePathData,
        toCorePathData
      ),
  )
import Charon.Backend.Fdo.DirectorySizes (DirectorySizesEntry)
import Charon.Backend.Fdo.PathData qualified as Fdo.PathData
import Charon.Prelude

backendArgs ::
  ( MonadAsync m,
    MonadCatch m,
    MonadLoggerNS m,
    MonadPathReader m,
    MonadPosixCompat m,
    MonadTerminal m
  ) =>
  BackendArgs m Fdo.PathData.PathData
backendArgs :: forall (m :: * -> *).
(MonadAsync m, MonadCatch m, MonadLoggerNS m, MonadPathReader m,
 MonadPosixCompat m, MonadTerminal m) =>
BackendArgs m PathData
backendArgs =
  MkBackendArgs
    { $sel:backend:MkBackendArgs :: Backend
backend = Backend
BackendFdo,
      $sel:makePathData:MkBackendArgs :: Timestamp
-> PathI 'TrashHome
-> PathI 'TrashEntryOriginalPath
-> m (PathData, PathTypeW)
makePathData = Timestamp
-> PathI 'TrashHome
-> PathI 'TrashEntryOriginalPath
-> m (PathData, PathTypeW)
forall (m :: * -> *).
(HasCallStack, MonadCatch m, MonadLoggerNS m, MonadPathReader m) =>
Timestamp
-> PathI 'TrashHome
-> PathI 'TrashEntryOriginalPath
-> m (PathData, PathTypeW)
Fdo.PathData.toPathData,
      $sel:toCorePathData:MkBackendArgs :: PathI 'TrashHome -> PathData -> m PathData
toCorePathData = PathI 'TrashHome -> PathData -> m PathData
forall (m :: * -> *).
(HasCallStack, MonadAsync m, MonadCatch m, MonadLoggerNS m,
 MonadPathReader m, MonadPosixCompat m, MonadTerminal m) =>
PathI 'TrashHome -> PathData -> m PathData
Fdo.PathData.toCorePathData,
      $sel:fromCorePathData:MkBackendArgs :: PathData -> PathData
fromCorePathData = PathData -> PathData
Fdo.PathData.fromCorePathData
    }

-- | Like backendArgs, but uses directorysizes to transform
-- Fdo PathData -> Core PathData instead of calculating size on the fly.
-- This has the potential to be faster when we expect to do many calculations
-- (e.g. reading the index).
backendArgsDirectorySizes ::
  ( MonadAsync m,
    MonadCatch m,
    MonadLoggerNS m,
    MonadPathReader m,
    MonadPosixCompat m,
    MonadTerminal m
  ) =>
  HashMap ByteString DirectorySizesEntry ->
  BackendArgs m Fdo.PathData.PathData
backendArgsDirectorySizes :: forall (m :: * -> *).
(MonadAsync m, MonadCatch m, MonadLoggerNS m, MonadPathReader m,
 MonadPosixCompat m, MonadTerminal m) =>
HashMap ByteString DirectorySizesEntry -> BackendArgs m PathData
backendArgsDirectorySizes HashMap ByteString DirectorySizesEntry
dsizeMap =
  MkBackendArgs
    { $sel:backend:MkBackendArgs :: Backend
backend = Backend
BackendFdo,
      $sel:makePathData:MkBackendArgs :: Timestamp
-> PathI 'TrashHome
-> PathI 'TrashEntryOriginalPath
-> m (PathData, PathTypeW)
makePathData = Timestamp
-> PathI 'TrashHome
-> PathI 'TrashEntryOriginalPath
-> m (PathData, PathTypeW)
forall (m :: * -> *).
(HasCallStack, MonadCatch m, MonadLoggerNS m, MonadPathReader m) =>
Timestamp
-> PathI 'TrashHome
-> PathI 'TrashEntryOriginalPath
-> m (PathData, PathTypeW)
Fdo.PathData.toPathData,
      $sel:toCorePathData:MkBackendArgs :: PathI 'TrashHome -> PathData -> m PathData
toCorePathData = HashMap ByteString DirectorySizesEntry
-> PathI 'TrashHome -> PathData -> m PathData
forall (m :: * -> *).
(HasCallStack, MonadAsync m, MonadCatch m, MonadLoggerNS m,
 MonadPathReader m, MonadPosixCompat m, MonadTerminal m) =>
HashMap ByteString DirectorySizesEntry
-> PathI 'TrashHome -> PathData -> m PathData
Fdo.PathData.toCorePathDataDirectorySizes HashMap ByteString DirectorySizesEntry
dsizeMap,
      $sel:fromCorePathData:MkBackendArgs :: PathData -> PathData
fromCorePathData = PathData -> PathData
Fdo.PathData.fromCorePathData
    }