# 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.split_config import SplitConfigParams
from ..types.max_page_size import MaxPageSize
from ..types.next_page_token import NextPageToken
from ..types.processor_run_status import ProcessorRunStatus
from ..types.run_metadata import RunMetadata
from ..types.run_priority import RunPriority
from ..types.run_source import RunSource
from ..types.run_source_id import RunSourceId
from ..types.sort_by import SortBy
from ..types.sort_dir import SortDir
from ..types.split_run import SplitRun
from .raw_client import AsyncRawSplitRunsClient, RawSplitRunsClient
from .requests.split_runs_create_request_file import SplitRunsCreateRequestFileParams
from .requests.split_runs_create_request_splitter import SplitRunsCreateRequestSplitterParams
from .types.split_runs_delete_response import SplitRunsDeleteResponse
from .types.split_runs_list_response import SplitRunsListResponse

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


class SplitRunsClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawSplitRunsClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawSplitRunsClient
        """
        return self._raw_client

    def list(
        self,
        *,
        status: typing.Optional[ProcessorRunStatus] = None,
        splitter_id: typing.Optional[str] = None,
        source_id: typing.Optional[RunSourceId] = None,
        source: typing.Optional[RunSource] = None,
        file_name_contains: typing.Optional[str] = None,
        sort_by: typing.Optional[SortBy] = None,
        sort_dir: typing.Optional[SortDir] = None,
        next_page_token: typing.Optional[NextPageToken] = None,
        max_page_size: typing.Optional[MaxPageSize] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> SplitRunsListResponse:
        """
        List all split runs.

        Returns a summary of each run. Use `GET /split_runs/{id}` to retrieve the full object including `output` and `config`.

        Parameters
        ----------
        status : typing.Optional[ProcessorRunStatus]

        splitter_id : typing.Optional[str]
            Filters split runs by the splitter ID. If not provided, all split runs are returned.

            Example: `"spl_BMdfq_yWM3sT-ZzvCnA3f"`

        source_id : typing.Optional[RunSourceId]
            Filters runs by the source ID.

        source : typing.Optional[RunSource]
            Filters runs by the source that created them. If not provided, runs from all sources are returned.

        file_name_contains : typing.Optional[str]
            Filters runs by the name of the file. Only returns runs where the file name contains this string.

            Example: `"invoice"`

        sort_by : typing.Optional[SortBy]

        sort_dir : typing.Optional[SortDir]

        next_page_token : typing.Optional[NextPageToken]

        max_page_size : typing.Optional[MaxPageSize]

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

        Returns
        -------
        SplitRunsListResponse
            You will get a list of summaries for each split run. These are shortened versions of the full split run object.

            To get the full object, use the [Get Split Run](https://docs.extend.ai/2026-02-09/developers/api-reference/endpoints/split/get-split-run) endpoint.

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

        client = Extend(
            token="YOUR_TOKEN",
        )
        client.split_runs.list(
            next_page_token="xK9mLPqRtN3vS8wF5hB2cQ==:zWvUxYjM4nKpL7aDgE9HbTcR2mAyX3/Q+CNkfBSw1dZ=",
        )
        """
        _response = self._raw_client.list(
            status=status,
            splitter_id=splitter_id,
            source_id=source_id,
            source=source,
            file_name_contains=file_name_contains,
            sort_by=sort_by,
            sort_dir=sort_dir,
            next_page_token=next_page_token,
            max_page_size=max_page_size,
            request_options=request_options,
        )
        return _response.data

    def create(
        self,
        *,
        file: SplitRunsCreateRequestFileParams,
        splitter: typing.Optional[SplitRunsCreateRequestSplitterParams] = OMIT,
        config: typing.Optional[SplitConfigParams] = OMIT,
        priority: typing.Optional[RunPriority] = OMIT,
        metadata: typing.Optional[RunMetadata] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> SplitRun:
        """
        Split a document into multiple parts using an existing splitter or an inline configuration.

        The request returns immediately with a `PROCESSING` status. Use webhooks or poll the [Get Split Run](https://docs.extend.ai/2026-02-09/developers/api-reference/endpoints/split/get-split-run) endpoint for results.

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

        splitter : typing.Optional[SplitRunsCreateRequestSplitterParams]
            Reference to an existing splitter. One of `splitter` or `config` must be provided.

        config : typing.Optional[SplitConfigParams]
            Inline splitter configuration. One of `splitter` or `config` must be provided.

        priority : typing.Optional[RunPriority]

        metadata : typing.Optional[RunMetadata]

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

        Returns
        -------
        SplitRun
            Split completed successfully

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

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

    def retrieve(self, id: str, *, request_options: typing.Optional[RequestOptions] = None) -> SplitRun:
        """
        Retrieve details about a specific split run, including its status and outputs.

        A common use case for this endpoint is to poll for the status and final output of a split run when using the [Create Split Run](https://docs.extend.ai/2026-02-09/developers/api-reference/endpoints/split/create-split-run) endpoint. For instance, if you do not want to not configure webhooks to receive the output via completion/failure events.

        Parameters
        ----------
        id : str
            The unique identifier for this split run.

            Example: `"spl_Xj8mK2pL9nR4vT7qY5wZ"`

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

        Returns
        -------
        SplitRun
            Successfully retrieved split run

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

        client = Extend(
            token="YOUR_TOKEN",
        )
        client.split_runs.retrieve(
            id="split_run_id_here",
        )
        """
        _response = self._raw_client.retrieve(id, request_options=request_options)
        return _response.data

    def delete(self, id: str, *, request_options: typing.Optional[RequestOptions] = None) -> SplitRunsDeleteResponse:
        """
        Delete a split 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 split run.

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

        Returns
        -------
        SplitRunsDeleteResponse
            Splitter run deleted successfully

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

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

    def cancel(self, id: str, *, request_options: typing.Optional[RequestOptions] = None) -> SplitRun:
        """
        Cancel an in-progress split run.

        Note: Only split runs with a status of `"PROCESSING"` can be cancelled. Splitter runs that have already completed, failed, or been cancelled cannot be cancelled again.

        Parameters
        ----------
        id : str
            The ID of the split run to cancel.

            Example: `"spl_Xj8mK2pL9nR4vT7qY5wZ"`

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

        Returns
        -------
        SplitRun
            Splitter run cancelled successfully

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

        client = Extend(
            token="YOUR_TOKEN",
        )
        client.split_runs.cancel(
            id="split_run_id_here",
        )
        """
        _response = self._raw_client.cancel(id, request_options=request_options)
        return _response.data


class AsyncSplitRunsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawSplitRunsClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawSplitRunsClient
        """
        return self._raw_client

    async def list(
        self,
        *,
        status: typing.Optional[ProcessorRunStatus] = None,
        splitter_id: typing.Optional[str] = None,
        source_id: typing.Optional[RunSourceId] = None,
        source: typing.Optional[RunSource] = None,
        file_name_contains: typing.Optional[str] = None,
        sort_by: typing.Optional[SortBy] = None,
        sort_dir: typing.Optional[SortDir] = None,
        next_page_token: typing.Optional[NextPageToken] = None,
        max_page_size: typing.Optional[MaxPageSize] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> SplitRunsListResponse:
        """
        List all split runs.

        Returns a summary of each run. Use `GET /split_runs/{id}` to retrieve the full object including `output` and `config`.

        Parameters
        ----------
        status : typing.Optional[ProcessorRunStatus]

        splitter_id : typing.Optional[str]
            Filters split runs by the splitter ID. If not provided, all split runs are returned.

            Example: `"spl_BMdfq_yWM3sT-ZzvCnA3f"`

        source_id : typing.Optional[RunSourceId]
            Filters runs by the source ID.

        source : typing.Optional[RunSource]
            Filters runs by the source that created them. If not provided, runs from all sources are returned.

        file_name_contains : typing.Optional[str]
            Filters runs by the name of the file. Only returns runs where the file name contains this string.

            Example: `"invoice"`

        sort_by : typing.Optional[SortBy]

        sort_dir : typing.Optional[SortDir]

        next_page_token : typing.Optional[NextPageToken]

        max_page_size : typing.Optional[MaxPageSize]

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

        Returns
        -------
        SplitRunsListResponse
            You will get a list of summaries for each split run. These are shortened versions of the full split run object.

            To get the full object, use the [Get Split Run](https://docs.extend.ai/2026-02-09/developers/api-reference/endpoints/split/get-split-run) endpoint.

        Examples
        --------
        import asyncio

        from extend_ai import AsyncExtend

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.split_runs.list(
                next_page_token="xK9mLPqRtN3vS8wF5hB2cQ==:zWvUxYjM4nKpL7aDgE9HbTcR2mAyX3/Q+CNkfBSw1dZ=",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.list(
            status=status,
            splitter_id=splitter_id,
            source_id=source_id,
            source=source,
            file_name_contains=file_name_contains,
            sort_by=sort_by,
            sort_dir=sort_dir,
            next_page_token=next_page_token,
            max_page_size=max_page_size,
            request_options=request_options,
        )
        return _response.data

    async def create(
        self,
        *,
        file: SplitRunsCreateRequestFileParams,
        splitter: typing.Optional[SplitRunsCreateRequestSplitterParams] = OMIT,
        config: typing.Optional[SplitConfigParams] = OMIT,
        priority: typing.Optional[RunPriority] = OMIT,
        metadata: typing.Optional[RunMetadata] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> SplitRun:
        """
        Split a document into multiple parts using an existing splitter or an inline configuration.

        The request returns immediately with a `PROCESSING` status. Use webhooks or poll the [Get Split Run](https://docs.extend.ai/2026-02-09/developers/api-reference/endpoints/split/get-split-run) endpoint for results.

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

        splitter : typing.Optional[SplitRunsCreateRequestSplitterParams]
            Reference to an existing splitter. One of `splitter` or `config` must be provided.

        config : typing.Optional[SplitConfigParams]
            Inline splitter configuration. One of `splitter` or `config` must be provided.

        priority : typing.Optional[RunPriority]

        metadata : typing.Optional[RunMetadata]

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

        Returns
        -------
        SplitRun
            Split completed successfully

        Examples
        --------
        import asyncio

        from extend_ai import AsyncExtend

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


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


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

    async def retrieve(self, id: str, *, request_options: typing.Optional[RequestOptions] = None) -> SplitRun:
        """
        Retrieve details about a specific split run, including its status and outputs.

        A common use case for this endpoint is to poll for the status and final output of a split run when using the [Create Split Run](https://docs.extend.ai/2026-02-09/developers/api-reference/endpoints/split/create-split-run) endpoint. For instance, if you do not want to not configure webhooks to receive the output via completion/failure events.

        Parameters
        ----------
        id : str
            The unique identifier for this split run.

            Example: `"spl_Xj8mK2pL9nR4vT7qY5wZ"`

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

        Returns
        -------
        SplitRun
            Successfully retrieved split run

        Examples
        --------
        import asyncio

        from extend_ai import AsyncExtend

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


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


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

    async def delete(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> SplitRunsDeleteResponse:
        """
        Delete a split 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 split run.

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

        Returns
        -------
        SplitRunsDeleteResponse
            Splitter run deleted successfully

        Examples
        --------
        import asyncio

        from extend_ai import AsyncExtend

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


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


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

    async def cancel(self, id: str, *, request_options: typing.Optional[RequestOptions] = None) -> SplitRun:
        """
        Cancel an in-progress split run.

        Note: Only split runs with a status of `"PROCESSING"` can be cancelled. Splitter runs that have already completed, failed, or been cancelled cannot be cancelled again.

        Parameters
        ----------
        id : str
            The ID of the split run to cancel.

            Example: `"spl_Xj8mK2pL9nR4vT7qY5wZ"`

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

        Returns
        -------
        SplitRun
            Splitter run cancelled successfully

        Examples
        --------
        import asyncio

        from extend_ai import AsyncExtend

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.split_runs.cancel(
                id="split_run_id_here",
            )


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