# This file was auto-generated by Fern from our API Definition.

import typing

from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.request_options import RequestOptions
from ..requests.parse_config import ParseConfigParams
from ..types.parse_run import ParseRun
from ..types.run_metadata import RunMetadata
from .raw_client import AsyncRawParseRunsClient, RawParseRunsClient
from .requests.parse_runs_create_request_file import ParseRunsCreateRequestFileParams
from .types.parse_runs_delete_response import ParseRunsDeleteResponse
from .types.parse_runs_retrieve_request_response_type import ParseRunsRetrieveRequestResponseType

# this is used as the default value for optional parameters
OMIT = typing.cast(typing.Any, ...)


class ParseRunsClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawParseRunsClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> RawParseRunsClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        RawParseRunsClient
        """
        return self._raw_client

    def create(
        self,
        *,
        file: ParseRunsCreateRequestFileParams,
        config: typing.Optional[ParseConfigParams] = OMIT,
        metadata: typing.Optional[RunMetadata] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ParseRun:
        """
        Parse files to get cleaned, chunked target content (e.g. markdown).

        The Parse endpoint allows you to convert documents into structured, machine-readable formats with fine-grained control over the parsing process. This endpoint is ideal for extracting cleaned document content to be used as context for downstream processing, e.g. RAG pipelines, custom ingestion pipelines, embeddings classification, etc.

        For more details, see the [Parse File guide](https://docs.extend.ai/2026-02-09/product/parsing/parse).

        Parameters
        ----------
        file : ParseRunsCreateRequestFileParams
            The file to be parsed. Files can be provided as a URL or an Extend file ID.

        config : typing.Optional[ParseConfigParams]

        metadata : typing.Optional[RunMetadata]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        ParseRun
            Successfully parsed file

        Examples
        --------
        from extend_ai import Extend

        client = Extend(
            token="YOUR_TOKEN",
        )
        client.parse_runs.create(
            file={"url": "url"},
        )
        """
        _response = self._raw_client.create(
            file=file, config=config, metadata=metadata, request_options=request_options
        )
        return _response.data

    def retrieve(
        self,
        id: str,
        *,
        response_type: typing.Optional[ParseRunsRetrieveRequestResponseType] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ParseRun:
        """
        Retrieve the status and results of a parse run.

        Use this endpoint to get results for a parse run that has already completed, or to check on the status of a parse run initiated by the [Create Parse Run](https://docs.extend.ai/2026-02-09/developers/api-reference/endpoints/parse/create-parse-run) endpoint.

        Parameters
        ----------
        id : str
            The unique identifier for the parse run.

            Example: `"pr_xK9mLPqRtN3vS8wF5hB2cQ"`

        response_type : typing.Optional[ParseRunsRetrieveRequestResponseType]
            Controls how the output is delivered. Defaults to `inline`.
            * `json` - Returns the output directly in the `output` field of the response body.
            * `url` - Returns a presigned URL in the `outputUrl` field to download the output as a JSON file. The URL expires after 15 minutes. Useful for large outputs.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        ParseRun
            Successfully retrieved parse run

        Examples
        --------
        from extend_ai import Extend

        client = Extend(
            token="YOUR_TOKEN",
        )
        client.parse_runs.retrieve(
            id="parse_run_id_here",
        )
        """
        _response = self._raw_client.retrieve(id, response_type=response_type, request_options=request_options)
        return _response.data

    def delete(self, id: str, *, request_options: typing.Optional[RequestOptions] = None) -> ParseRunsDeleteResponse:
        """
        Delete a parse run and all associated data from Extend. This operation is permanent and cannot be undone.

        This endpoint can be used if you'd like to manage data retention on your own rather than automated data retention policies. Or make one-off deletions for your downstream customers.

        Parameters
        ----------
        id : str
            The ID of the parse run to delete.

            Example: `"pr_xK9mLPqRtN3vS8wF5hB2cQ"`

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        ParseRunsDeleteResponse
            Successfully deleted parse run

        Examples
        --------
        from extend_ai import Extend

        client = Extend(
            token="YOUR_TOKEN",
        )
        client.parse_runs.delete(
            id="parse_run_id_here",
        )
        """
        _response = self._raw_client.delete(id, request_options=request_options)
        return _response.data


class AsyncParseRunsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawParseRunsClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> AsyncRawParseRunsClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        AsyncRawParseRunsClient
        """
        return self._raw_client

    async def create(
        self,
        *,
        file: ParseRunsCreateRequestFileParams,
        config: typing.Optional[ParseConfigParams] = OMIT,
        metadata: typing.Optional[RunMetadata] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ParseRun:
        """
        Parse files to get cleaned, chunked target content (e.g. markdown).

        The Parse endpoint allows you to convert documents into structured, machine-readable formats with fine-grained control over the parsing process. This endpoint is ideal for extracting cleaned document content to be used as context for downstream processing, e.g. RAG pipelines, custom ingestion pipelines, embeddings classification, etc.

        For more details, see the [Parse File guide](https://docs.extend.ai/2026-02-09/product/parsing/parse).

        Parameters
        ----------
        file : ParseRunsCreateRequestFileParams
            The file to be parsed. Files can be provided as a URL or an Extend file ID.

        config : typing.Optional[ParseConfigParams]

        metadata : typing.Optional[RunMetadata]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        ParseRun
            Successfully parsed file

        Examples
        --------
        import asyncio

        from extend_ai import AsyncExtend

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.parse_runs.create(
                file={"url": "url"},
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.create(
            file=file, config=config, metadata=metadata, request_options=request_options
        )
        return _response.data

    async def retrieve(
        self,
        id: str,
        *,
        response_type: typing.Optional[ParseRunsRetrieveRequestResponseType] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ParseRun:
        """
        Retrieve the status and results of a parse run.

        Use this endpoint to get results for a parse run that has already completed, or to check on the status of a parse run initiated by the [Create Parse Run](https://docs.extend.ai/2026-02-09/developers/api-reference/endpoints/parse/create-parse-run) endpoint.

        Parameters
        ----------
        id : str
            The unique identifier for the parse run.

            Example: `"pr_xK9mLPqRtN3vS8wF5hB2cQ"`

        response_type : typing.Optional[ParseRunsRetrieveRequestResponseType]
            Controls how the output is delivered. Defaults to `inline`.
            * `json` - Returns the output directly in the `output` field of the response body.
            * `url` - Returns a presigned URL in the `outputUrl` field to download the output as a JSON file. The URL expires after 15 minutes. Useful for large outputs.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        ParseRun
            Successfully retrieved parse run

        Examples
        --------
        import asyncio

        from extend_ai import AsyncExtend

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.parse_runs.retrieve(
                id="parse_run_id_here",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.retrieve(id, response_type=response_type, request_options=request_options)
        return _response.data

    async def delete(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> ParseRunsDeleteResponse:
        """
        Delete a parse run and all associated data from Extend. This operation is permanent and cannot be undone.

        This endpoint can be used if you'd like to manage data retention on your own rather than automated data retention policies. Or make one-off deletions for your downstream customers.

        Parameters
        ----------
        id : str
            The ID of the parse run to delete.

            Example: `"pr_xK9mLPqRtN3vS8wF5hB2cQ"`

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        ParseRunsDeleteResponse
            Successfully deleted parse run

        Examples
        --------
        import asyncio

        from extend_ai import AsyncExtend

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.parse_runs.delete(
                id="parse_run_id_here",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.delete(id, request_options=request_options)
        return _response.data
