from __future__ import annotations

import anyio

from idxr.text_extraction import read_file_by_mime_type

from ..interfaces import TextExtractor
from ..models import DownloadedDocument


class MimeTypeTextExtractor(TextExtractor):
    """
    Dispatches to existing attachment readers based on MIME type.
    """

    async def extract(self, document: DownloadedDocument) -> str:
        return await anyio.to_thread.run_sync(
            read_file_by_mime_type,
            document.content,
            document.mime_type or "application/pdf",
        )
