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

import typing
from ...core.client_wrapper import SyncClientWrapper
from .raw_client import RawRefundsClient
from ...requests.terminal_refund import TerminalRefundParams
from ...core.request_options import RequestOptions
from ...types.create_terminal_refund_response import CreateTerminalRefundResponse
from ...requests.terminal_refund_query import TerminalRefundQueryParams
from ...types.search_terminal_refunds_response import SearchTerminalRefundsResponse
from ...types.get_terminal_refund_response import GetTerminalRefundResponse
from ...types.cancel_terminal_refund_response import CancelTerminalRefundResponse
from ...core.client_wrapper import AsyncClientWrapper
from .raw_client import AsyncRawRefundsClient

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


class RefundsClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawRefundsClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawRefundsClient
        """
        return self._raw_client

    def create(
        self,
        *,
        idempotency_key: str,
        refund: typing.Optional[TerminalRefundParams] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> CreateTerminalRefundResponse:
        """
        Creates a request to refund an Interac payment completed on a Square Terminal. Refunds for Interac payments on a Square Terminal are supported only for Interac debit cards in Canada. Other refunds for Terminal payments should use the Refunds API. For more information, see [Refunds API](api:Refunds).

        Parameters
        ----------
        idempotency_key : str
            A unique string that identifies this `CreateRefund` request. Keys can be any valid string but
            must be unique for every `CreateRefund` request.

            See [Idempotency keys](https://developer.squareup.com/docs/build-basics/common-api-patterns/idempotency) for more information.

        refund : typing.Optional[TerminalRefundParams]
            The refund to create.

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

        Returns
        -------
        CreateTerminalRefundResponse
            Success

        Examples
        --------
        from square import Square

        client = Square(
            token="YOUR_TOKEN",
        )
        client.terminal.refunds.create(
            idempotency_key="402a640b-b26f-401f-b406-46f839590c04",
            refund={
                "payment_id": "5O5OvgkcNUhl7JBuINflcjKqUzXZY",
                "amount_money": {"amount": 111, "currency": "CAD"},
                "reason": "Returning items",
                "device_id": "f72dfb8e-4d65-4e56-aade-ec3fb8d33291",
            },
        )
        """
        response = self._raw_client.create(
            idempotency_key=idempotency_key, refund=refund, request_options=request_options
        )
        return response.data

    def search(
        self,
        *,
        query: typing.Optional[TerminalRefundQueryParams] = OMIT,
        cursor: typing.Optional[str] = OMIT,
        limit: typing.Optional[int] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> SearchTerminalRefundsResponse:
        """
        Retrieves a filtered list of Interac Terminal refund requests created by the seller making the request. Terminal refund requests are available for 30 days.

        Parameters
        ----------
        query : typing.Optional[TerminalRefundQueryParams]
            Queries the Terminal refunds based on given conditions and the sort order. Calling
            `SearchTerminalRefunds` without an explicit query parameter returns all available
            refunds with the default sort order.

        cursor : typing.Optional[str]
            A pagination cursor returned by a previous call to this endpoint.
            Provide this cursor to retrieve the next set of results for the original query.

        limit : typing.Optional[int]
            Limits the number of results returned for a single request.

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

        Returns
        -------
        SearchTerminalRefundsResponse
            Success

        Examples
        --------
        from square import Square

        client = Square(
            token="YOUR_TOKEN",
        )
        client.terminal.refunds.search(
            query={"filter": {"status": "COMPLETED"}},
            limit=1,
        )
        """
        response = self._raw_client.search(query=query, cursor=cursor, limit=limit, request_options=request_options)
        return response.data

    def get(
        self, terminal_refund_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> GetTerminalRefundResponse:
        """
        Retrieves an Interac Terminal refund object by ID. Terminal refund objects are available for 30 days.

        Parameters
        ----------
        terminal_refund_id : str
            The unique ID for the desired `TerminalRefund`.

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

        Returns
        -------
        GetTerminalRefundResponse
            Success

        Examples
        --------
        from square import Square

        client = Square(
            token="YOUR_TOKEN",
        )
        client.terminal.refunds.get(
            terminal_refund_id="terminal_refund_id",
        )
        """
        response = self._raw_client.get(terminal_refund_id, request_options=request_options)
        return response.data

    def cancel(
        self, terminal_refund_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> CancelTerminalRefundResponse:
        """
        Cancels an Interac Terminal refund request by refund request ID if the status of the request permits it.

        Parameters
        ----------
        terminal_refund_id : str
            The unique ID for the desired `TerminalRefund`.

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

        Returns
        -------
        CancelTerminalRefundResponse
            Success

        Examples
        --------
        from square import Square

        client = Square(
            token="YOUR_TOKEN",
        )
        client.terminal.refunds.cancel(
            terminal_refund_id="terminal_refund_id",
        )
        """
        response = self._raw_client.cancel(terminal_refund_id, request_options=request_options)
        return response.data


class AsyncRefundsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawRefundsClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawRefundsClient
        """
        return self._raw_client

    async def create(
        self,
        *,
        idempotency_key: str,
        refund: typing.Optional[TerminalRefundParams] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> CreateTerminalRefundResponse:
        """
        Creates a request to refund an Interac payment completed on a Square Terminal. Refunds for Interac payments on a Square Terminal are supported only for Interac debit cards in Canada. Other refunds for Terminal payments should use the Refunds API. For more information, see [Refunds API](api:Refunds).

        Parameters
        ----------
        idempotency_key : str
            A unique string that identifies this `CreateRefund` request. Keys can be any valid string but
            must be unique for every `CreateRefund` request.

            See [Idempotency keys](https://developer.squareup.com/docs/build-basics/common-api-patterns/idempotency) for more information.

        refund : typing.Optional[TerminalRefundParams]
            The refund to create.

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

        Returns
        -------
        CreateTerminalRefundResponse
            Success

        Examples
        --------
        import asyncio

        from square import AsyncSquare

        client = AsyncSquare(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.terminal.refunds.create(
                idempotency_key="402a640b-b26f-401f-b406-46f839590c04",
                refund={
                    "payment_id": "5O5OvgkcNUhl7JBuINflcjKqUzXZY",
                    "amount_money": {"amount": 111, "currency": "CAD"},
                    "reason": "Returning items",
                    "device_id": "f72dfb8e-4d65-4e56-aade-ec3fb8d33291",
                },
            )


        asyncio.run(main())
        """
        response = await self._raw_client.create(
            idempotency_key=idempotency_key, refund=refund, request_options=request_options
        )
        return response.data

    async def search(
        self,
        *,
        query: typing.Optional[TerminalRefundQueryParams] = OMIT,
        cursor: typing.Optional[str] = OMIT,
        limit: typing.Optional[int] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> SearchTerminalRefundsResponse:
        """
        Retrieves a filtered list of Interac Terminal refund requests created by the seller making the request. Terminal refund requests are available for 30 days.

        Parameters
        ----------
        query : typing.Optional[TerminalRefundQueryParams]
            Queries the Terminal refunds based on given conditions and the sort order. Calling
            `SearchTerminalRefunds` without an explicit query parameter returns all available
            refunds with the default sort order.

        cursor : typing.Optional[str]
            A pagination cursor returned by a previous call to this endpoint.
            Provide this cursor to retrieve the next set of results for the original query.

        limit : typing.Optional[int]
            Limits the number of results returned for a single request.

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

        Returns
        -------
        SearchTerminalRefundsResponse
            Success

        Examples
        --------
        import asyncio

        from square import AsyncSquare

        client = AsyncSquare(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.terminal.refunds.search(
                query={"filter": {"status": "COMPLETED"}},
                limit=1,
            )


        asyncio.run(main())
        """
        response = await self._raw_client.search(
            query=query, cursor=cursor, limit=limit, request_options=request_options
        )
        return response.data

    async def get(
        self, terminal_refund_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> GetTerminalRefundResponse:
        """
        Retrieves an Interac Terminal refund object by ID. Terminal refund objects are available for 30 days.

        Parameters
        ----------
        terminal_refund_id : str
            The unique ID for the desired `TerminalRefund`.

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

        Returns
        -------
        GetTerminalRefundResponse
            Success

        Examples
        --------
        import asyncio

        from square import AsyncSquare

        client = AsyncSquare(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.terminal.refunds.get(
                terminal_refund_id="terminal_refund_id",
            )


        asyncio.run(main())
        """
        response = await self._raw_client.get(terminal_refund_id, request_options=request_options)
        return response.data

    async def cancel(
        self, terminal_refund_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> CancelTerminalRefundResponse:
        """
        Cancels an Interac Terminal refund request by refund request ID if the status of the request permits it.

        Parameters
        ----------
        terminal_refund_id : str
            The unique ID for the desired `TerminalRefund`.

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

        Returns
        -------
        CancelTerminalRefundResponse
            Success

        Examples
        --------
        import asyncio

        from square import AsyncSquare

        client = AsyncSquare(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.terminal.refunds.cancel(
                terminal_refund_id="terminal_refund_id",
            )


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