module Nbparts.Pack.Mime where

import Data.ByteString qualified as ByteString
import Data.Coerce (coerce)
import Data.Ipynb qualified as Ipynb
import Nbparts.Types
  ( UnembeddedMimeAttachments (UnembeddedMimeAttachments),
    UnembeddedMimeBundle (UnembeddedMimeBundle),
    UnembeddedMimeData (BinaryData, JsonData, TextualData),
  )
import System.FilePath ((</>))

embedMimeAttachments :: FilePath -> UnembeddedMimeAttachments -> IO Ipynb.MimeAttachments
embedMimeAttachments :: FilePath -> UnembeddedMimeAttachments -> IO MimeAttachments
embedMimeAttachments FilePath
readDir (UnembeddedMimeAttachments Map MimeType UnembeddedMimeBundle
unembedded) =
  IO (Map MimeType MimeBundle) -> IO MimeAttachments
forall a b. Coercible a b => a -> b
coerce (IO (Map MimeType MimeBundle) -> IO MimeAttachments)
-> IO (Map MimeType MimeBundle) -> IO MimeAttachments
forall a b. (a -> b) -> a -> b
$
    (UnembeddedMimeBundle -> IO MimeBundle)
-> Map MimeType UnembeddedMimeBundle
-> IO (Map MimeType MimeBundle)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Map MimeType a -> f (Map MimeType b)
traverse
      (FilePath -> UnembeddedMimeBundle -> IO MimeBundle
embedMimeBundle FilePath
readDir)
      Map MimeType UnembeddedMimeBundle
unembedded

embedMimeBundle :: FilePath -> UnembeddedMimeBundle -> IO Ipynb.MimeBundle
embedMimeBundle :: FilePath -> UnembeddedMimeBundle -> IO MimeBundle
embedMimeBundle FilePath
readDir = (Map MimeType UnembeddedMimeData -> IO MimeBundle)
-> UnembeddedMimeBundle -> IO MimeBundle
forall a b. Coercible a b => a -> b
coerce ((Map MimeType MimeData -> MimeBundle)
-> IO (Map MimeType MimeData) -> IO MimeBundle
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Map MimeType MimeData -> MimeBundle
Ipynb.MimeBundle (IO (Map MimeType MimeData) -> IO MimeBundle)
-> (Map MimeType UnembeddedMimeData -> IO (Map MimeType MimeData))
-> Map MimeType UnembeddedMimeData
-> IO MimeBundle
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (UnembeddedMimeData -> IO MimeData)
-> Map MimeType UnembeddedMimeData -> IO (Map MimeType MimeData)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Map MimeType a -> f (Map MimeType b)
traverse (FilePath -> UnembeddedMimeData -> IO MimeData
embedMimeData FilePath
readDir))

embedMimeData :: FilePath -> UnembeddedMimeData -> IO Ipynb.MimeData
embedMimeData :: FilePath -> UnembeddedMimeData -> IO MimeData
embedMimeData FilePath
readDir (BinaryData FilePath
path) = do
  ByteString
bytes <- FilePath -> IO ByteString
ByteString.readFile (FilePath -> IO ByteString) -> FilePath -> IO ByteString
forall a b. (a -> b) -> a -> b
$ FilePath
readDir FilePath -> FilePath -> FilePath
</> FilePath
path
  MimeData -> IO MimeData
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (MimeData -> IO MimeData) -> MimeData -> IO MimeData
forall a b. (a -> b) -> a -> b
$ ByteString -> MimeData
Ipynb.BinaryData ByteString
bytes
embedMimeData FilePath
_ (TextualData MimeType
text) = MimeData -> IO MimeData
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MimeData -> IO MimeData) -> MimeData -> IO MimeData
forall a b. (a -> b) -> a -> b
$ MimeType -> MimeData
Ipynb.TextualData MimeType
text
embedMimeData FilePath
_ (JsonData Value
value) = MimeData -> IO MimeData
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MimeData -> IO MimeData) -> MimeData -> IO MimeData
forall a b. (a -> b) -> a -> b
$ Value -> MimeData
Ipynb.JsonData Value
value