    {-# LANGUAGE OverloadedStrings #-}
    {- |
       Module      : Text.Pandoc.Readers.Pptx
       Copyright   : © 2025 Anton Antic
       License     : GNU GPL, version 2 or above

       Maintainer  : Anton Antic <anton@everworker.ai>
       Stability   : alpha
       Portability : portable

    Conversion of PPTX (PowerPoint) documents to 'Pandoc' document.
    -}
    module Text.Pandoc.Readers.Pptx (readPptx) where

    import qualified Data.ByteString.Lazy as B
    import qualified Data.Text as T
    import Codec.Archive.Zip (toArchiveOrFail)
    import Control.Monad.Except (throwError)
    import Text.Pandoc.Class.PandocMonad (PandocMonad)
    import Text.Pandoc.Definition (Pandoc(..))
    import Text.Pandoc.Error (PandocError(..))
    import Text.Pandoc.Options (ReaderOptions)
    import Text.Pandoc.Readers.Pptx.Parse (archiveToPptx)
    import Text.Pandoc.Readers.Pptx.Slides (pptxToOutput)

    -- | Read PPTX file into Pandoc AST
    readPptx :: PandocMonad m => ReaderOptions -> B.ByteString -> m Pandoc
    readPptx :: forall (m :: * -> *).
    PandocMonad m =>
    ReaderOptions -> ByteString -> m Pandoc
    readPptx ReaderOptions
    opts ByteString
    bytes =
      case ByteString -> Either String Archive
    toArchiveOrFail ByteString
    bytes of
        Right Archive
    archive ->
          case Archive -> Either Text Pptx
    archiveToPptx Archive
    archive of
            Right Pptx
    pptx -> do
              -- Convert Pptx intermediate to Pandoc AST
              (meta, blocks) <- ReaderOptions -> Pptx -> m (Meta, [Block])
    forall (m :: * -> *).
    PandocMonad m =>
    ReaderOptions -> Pptx -> m (Meta, [Block])
    pptxToOutput ReaderOptions
    opts Pptx
    pptx
              return $ Pandoc meta blocks

            Left Text
    err ->
              PandocError -> m Pandoc
    forall a. PandocError -> m a
    forall e (m :: * -> *) a. MonadError e m => e -> m a
    throwError (PandocError -> m Pandoc) -> PandocError -> m Pandoc
    forall a b. (a -> b) -> a -> b
    $ Text -> PandocError
    PandocParseError (Text -> PandocError) -> Text -> PandocError
    forall a b. (a -> b) -> a -> b
    $
                Text
    "Failed to parse PPTX: " Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    err

        Left String
    err ->
          PandocError -> m Pandoc
    forall a. PandocError -> m a
    forall e (m :: * -> *) a. MonadError e m => e -> m a
    throwError (PandocError -> m Pandoc) -> PandocError -> m Pandoc
    forall a b. (a -> b) -> a -> b
    $ Text -> PandocError
    PandocParseError (Text -> PandocError) -> Text -> PandocError
    forall a b. (a -> b) -> a -> b
    $
            Text
    "Failed to unpack PPTX archive: " Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> String -> Text
    T.pack String
    err