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

import typing
from ...core.client_wrapper import SyncClientWrapper
from .raw_client import RawCheckoutsClient
from ...requests.terminal_checkout import TerminalCheckoutParams
from ...core.request_options import RequestOptions
from ...types.create_terminal_checkout_response import CreateTerminalCheckoutResponse
from ...requests.terminal_checkout_query import TerminalCheckoutQueryParams
from ...types.search_terminal_checkouts_response import SearchTerminalCheckoutsResponse
from ...types.get_terminal_checkout_response import GetTerminalCheckoutResponse
from ...types.cancel_terminal_checkout_response import CancelTerminalCheckoutResponse
from ...core.client_wrapper import AsyncClientWrapper
from .raw_client import AsyncRawCheckoutsClient

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


class CheckoutsClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawCheckoutsClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawCheckoutsClient
        """
        return self._raw_client

    def create(
        self,
        *,
        idempotency_key: str,
        checkout: TerminalCheckoutParams,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> CreateTerminalCheckoutResponse:
        """
        Creates a Terminal checkout request and sends it to the specified device to take a payment
        for the requested amount.

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

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

        checkout : TerminalCheckoutParams
            The checkout to create.

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

        Returns
        -------
        CreateTerminalCheckoutResponse
            Success

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

        client = Square(
            token="YOUR_TOKEN",
        )
        client.terminal.checkouts.create(
            idempotency_key="28a0c3bc-7839-11ea-bc55-0242ac130003",
            checkout={
                "amount_money": {"amount": 2610, "currency": "USD"},
                "reference_id": "id11572",
                "note": "A brief note",
                "device_options": {"device_id": "dbb5d83a-7838-11ea-bc55-0242ac130003"},
            },
        )
        """
        response = self._raw_client.create(
            idempotency_key=idempotency_key, checkout=checkout, request_options=request_options
        )
        return response.data

    def search(
        self,
        *,
        query: typing.Optional[TerminalCheckoutQueryParams] = OMIT,
        cursor: typing.Optional[str] = OMIT,
        limit: typing.Optional[int] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> SearchTerminalCheckoutsResponse:
        """
        Returns a filtered list of Terminal checkout requests created by the application making the request. Only Terminal checkout requests created for the merchant scoped to the OAuth token are returned. Terminal checkout requests are available for 30 days.

        Parameters
        ----------
        query : typing.Optional[TerminalCheckoutQueryParams]
            Queries Terminal checkouts based on given conditions and the sort order.
            Leaving these unset returns all checkouts 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.
            See [Pagination](https://developer.squareup.com/docs/build-basics/common-api-patterns/pagination) for more information.

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

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

        Returns
        -------
        SearchTerminalCheckoutsResponse
            Success

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

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

    def get(
        self, checkout_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> GetTerminalCheckoutResponse:
        """
        Retrieves a Terminal checkout request by `checkout_id`. Terminal checkout requests are available for 30 days.

        Parameters
        ----------
        checkout_id : str
            The unique ID for the desired `TerminalCheckout`.

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

        Returns
        -------
        GetTerminalCheckoutResponse
            Success

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

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

    def cancel(
        self, checkout_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> CancelTerminalCheckoutResponse:
        """
        Cancels a Terminal checkout request if the status of the request permits it.

        Parameters
        ----------
        checkout_id : str
            The unique ID for the desired `TerminalCheckout`.

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

        Returns
        -------
        CancelTerminalCheckoutResponse
            Success

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

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


class AsyncCheckoutsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawCheckoutsClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawCheckoutsClient
        """
        return self._raw_client

    async def create(
        self,
        *,
        idempotency_key: str,
        checkout: TerminalCheckoutParams,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> CreateTerminalCheckoutResponse:
        """
        Creates a Terminal checkout request and sends it to the specified device to take a payment
        for the requested amount.

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

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

        checkout : TerminalCheckoutParams
            The checkout to create.

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

        Returns
        -------
        CreateTerminalCheckoutResponse
            Success

        Examples
        --------
        import asyncio

        from square import AsyncSquare

        client = AsyncSquare(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.terminal.checkouts.create(
                idempotency_key="28a0c3bc-7839-11ea-bc55-0242ac130003",
                checkout={
                    "amount_money": {"amount": 2610, "currency": "USD"},
                    "reference_id": "id11572",
                    "note": "A brief note",
                    "device_options": {
                        "device_id": "dbb5d83a-7838-11ea-bc55-0242ac130003"
                    },
                },
            )


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

    async def search(
        self,
        *,
        query: typing.Optional[TerminalCheckoutQueryParams] = OMIT,
        cursor: typing.Optional[str] = OMIT,
        limit: typing.Optional[int] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> SearchTerminalCheckoutsResponse:
        """
        Returns a filtered list of Terminal checkout requests created by the application making the request. Only Terminal checkout requests created for the merchant scoped to the OAuth token are returned. Terminal checkout requests are available for 30 days.

        Parameters
        ----------
        query : typing.Optional[TerminalCheckoutQueryParams]
            Queries Terminal checkouts based on given conditions and the sort order.
            Leaving these unset returns all checkouts 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.
            See [Pagination](https://developer.squareup.com/docs/build-basics/common-api-patterns/pagination) for more information.

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

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

        Returns
        -------
        SearchTerminalCheckoutsResponse
            Success

        Examples
        --------
        import asyncio

        from square import AsyncSquare

        client = AsyncSquare(
            token="YOUR_TOKEN",
        )


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


        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, checkout_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> GetTerminalCheckoutResponse:
        """
        Retrieves a Terminal checkout request by `checkout_id`. Terminal checkout requests are available for 30 days.

        Parameters
        ----------
        checkout_id : str
            The unique ID for the desired `TerminalCheckout`.

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

        Returns
        -------
        GetTerminalCheckoutResponse
            Success

        Examples
        --------
        import asyncio

        from square import AsyncSquare

        client = AsyncSquare(
            token="YOUR_TOKEN",
        )


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


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

    async def cancel(
        self, checkout_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> CancelTerminalCheckoutResponse:
        """
        Cancels a Terminal checkout request if the status of the request permits it.

        Parameters
        ----------
        checkout_id : str
            The unique ID for the desired `TerminalCheckout`.

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

        Returns
        -------
        CancelTerminalCheckoutResponse
            Success

        Examples
        --------
        import asyncio

        from square import AsyncSquare

        client = AsyncSquare(
            token="YOUR_TOKEN",
        )


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


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