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

import typing
from ...core.client_wrapper import SyncClientWrapper
from .raw_client import RawBreakTypesClient
from ...core.request_options import RequestOptions
from ...core.pagination import SyncPager
from ...types.break_type import BreakType
from ...types.list_break_types_response import ListBreakTypesResponse
from ...core.unchecked_base_model import construct_type
from json.decoder import JSONDecodeError
from ...core.api_error import ApiError
from ...requests.break_type import BreakTypeParams
from ...types.create_break_type_response import CreateBreakTypeResponse
from ...types.get_break_type_response import GetBreakTypeResponse
from ...types.update_break_type_response import UpdateBreakTypeResponse
from ...types.delete_break_type_response import DeleteBreakTypeResponse
from ...core.client_wrapper import AsyncClientWrapper
from .raw_client import AsyncRawBreakTypesClient
from ...core.pagination import AsyncPager

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


class BreakTypesClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawBreakTypesClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawBreakTypesClient
        """
        return self._raw_client

    def list(
        self,
        *,
        location_id: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        cursor: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> SyncPager[BreakType]:
        """
        Returns a paginated list of `BreakType` instances for a business.

        Parameters
        ----------
        location_id : typing.Optional[str]
            Filter the returned `BreakType` results to only those that are associated with the
            specified location.

        limit : typing.Optional[int]
            The maximum number of `BreakType` results to return per page. The number can range between 1
            and 200. The default is 200.

        cursor : typing.Optional[str]
            A pointer to the next page of `BreakType` results to fetch.

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

        Returns
        -------
        SyncPager[BreakType]
            Success

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

        client = Square(
            token="YOUR_TOKEN",
        )
        response = client.labor.break_types.list()
        for item in response:
            yield item
        # alternatively, you can paginate page-by-page
        for page in response.iter_pages():
            yield page
        """
        _response = self._raw_client._client_wrapper.httpx_client.request(
            "v2/labor/break-types",
            method="GET",
            params={
                "location_id": location_id,
                "limit": limit,
                "cursor": cursor,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _parsed_response = typing.cast(
                    ListBreakTypesResponse,
                    construct_type(
                        type_=ListBreakTypesResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                _parsed_next = _parsed_response.cursor
                _has_next = _parsed_next is not None and _parsed_next != ""
                _get_next = lambda: self.list(
                    location_id=location_id,
                    limit=limit,
                    cursor=_parsed_next,
                    request_options=request_options,
                )
                _items = _parsed_response.break_types
                return SyncPager(has_next=_has_next, items=_items, get_next=_get_next)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def create(
        self,
        *,
        break_type: BreakTypeParams,
        idempotency_key: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> CreateBreakTypeResponse:
        """
        Creates a new `BreakType`.

        A `BreakType` is a template for creating `Break` objects.
        You must provide the following values in your request to this
        endpoint:

        - `location_id`
        - `break_name`
        - `expected_duration`
        - `is_paid`

        You can only have three `BreakType` instances per location. If you attempt to add a fourth
        `BreakType` for a location, an `INVALID_REQUEST_ERROR` "Exceeded limit of 3 breaks per location."
        is returned.

        Parameters
        ----------
        break_type : BreakTypeParams
            The `BreakType` to be created.

        idempotency_key : typing.Optional[str]
            A unique string value to ensure the idempotency of the operation.

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

        Returns
        -------
        CreateBreakTypeResponse
            Success

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

        client = Square(
            token="YOUR_TOKEN",
        )
        client.labor.break_types.create(
            idempotency_key="PAD3NG5KSN2GL",
            break_type={
                "location_id": "CGJN03P1D08GF",
                "break_name": "Lunch Break",
                "expected_duration": "PT30M",
                "is_paid": True,
            },
        )
        """
        response = self._raw_client.create(
            break_type=break_type, idempotency_key=idempotency_key, request_options=request_options
        )
        return response.data

    def get(self, id: str, *, request_options: typing.Optional[RequestOptions] = None) -> GetBreakTypeResponse:
        """
        Returns a single `BreakType` specified by `id`.

        Parameters
        ----------
        id : str
            The UUID for the `BreakType` being retrieved.

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

        Returns
        -------
        GetBreakTypeResponse
            Success

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

        client = Square(
            token="YOUR_TOKEN",
        )
        client.labor.break_types.get(
            id="id",
        )
        """
        response = self._raw_client.get(id, request_options=request_options)
        return response.data

    def update(
        self, id: str, *, break_type: BreakTypeParams, request_options: typing.Optional[RequestOptions] = None
    ) -> UpdateBreakTypeResponse:
        """
        Updates an existing `BreakType`.

        Parameters
        ----------
        id : str
             The UUID for the `BreakType` being updated.

        break_type : BreakTypeParams
            The updated `BreakType`.

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

        Returns
        -------
        UpdateBreakTypeResponse
            Success

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

        client = Square(
            token="YOUR_TOKEN",
        )
        client.labor.break_types.update(
            id="id",
            break_type={
                "location_id": "26M7H24AZ9N6R",
                "break_name": "Lunch",
                "expected_duration": "PT50M",
                "is_paid": True,
                "version": 1,
            },
        )
        """
        response = self._raw_client.update(id, break_type=break_type, request_options=request_options)
        return response.data

    def delete(self, id: str, *, request_options: typing.Optional[RequestOptions] = None) -> DeleteBreakTypeResponse:
        """
        Deletes an existing `BreakType`.

        A `BreakType` can be deleted even if it is referenced from a `Shift`.

        Parameters
        ----------
        id : str
            The UUID for the `BreakType` being deleted.

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

        Returns
        -------
        DeleteBreakTypeResponse
            Success

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

        client = Square(
            token="YOUR_TOKEN",
        )
        client.labor.break_types.delete(
            id="id",
        )
        """
        response = self._raw_client.delete(id, request_options=request_options)
        return response.data


class AsyncBreakTypesClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawBreakTypesClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawBreakTypesClient
        """
        return self._raw_client

    async def list(
        self,
        *,
        location_id: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        cursor: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncPager[BreakType]:
        """
        Returns a paginated list of `BreakType` instances for a business.

        Parameters
        ----------
        location_id : typing.Optional[str]
            Filter the returned `BreakType` results to only those that are associated with the
            specified location.

        limit : typing.Optional[int]
            The maximum number of `BreakType` results to return per page. The number can range between 1
            and 200. The default is 200.

        cursor : typing.Optional[str]
            A pointer to the next page of `BreakType` results to fetch.

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

        Returns
        -------
        AsyncPager[BreakType]
            Success

        Examples
        --------
        import asyncio

        from square import AsyncSquare

        client = AsyncSquare(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            response = await client.labor.break_types.list()
            async for item in response:
                yield item
            # alternatively, you can paginate page-by-page
            async for page in response.iter_pages():
                yield page


        asyncio.run(main())
        """
        _response = await self._raw_client._client_wrapper.httpx_client.request(
            "v2/labor/break-types",
            method="GET",
            params={
                "location_id": location_id,
                "limit": limit,
                "cursor": cursor,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _parsed_response = typing.cast(
                    ListBreakTypesResponse,
                    construct_type(
                        type_=ListBreakTypesResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                _parsed_next = _parsed_response.cursor
                _has_next = _parsed_next is not None and _parsed_next != ""
                _get_next = lambda: self.list(
                    location_id=location_id,
                    limit=limit,
                    cursor=_parsed_next,
                    request_options=request_options,
                )
                _items = _parsed_response.break_types
                return AsyncPager(has_next=_has_next, items=_items, get_next=_get_next)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def create(
        self,
        *,
        break_type: BreakTypeParams,
        idempotency_key: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> CreateBreakTypeResponse:
        """
        Creates a new `BreakType`.

        A `BreakType` is a template for creating `Break` objects.
        You must provide the following values in your request to this
        endpoint:

        - `location_id`
        - `break_name`
        - `expected_duration`
        - `is_paid`

        You can only have three `BreakType` instances per location. If you attempt to add a fourth
        `BreakType` for a location, an `INVALID_REQUEST_ERROR` "Exceeded limit of 3 breaks per location."
        is returned.

        Parameters
        ----------
        break_type : BreakTypeParams
            The `BreakType` to be created.

        idempotency_key : typing.Optional[str]
            A unique string value to ensure the idempotency of the operation.

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

        Returns
        -------
        CreateBreakTypeResponse
            Success

        Examples
        --------
        import asyncio

        from square import AsyncSquare

        client = AsyncSquare(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.labor.break_types.create(
                idempotency_key="PAD3NG5KSN2GL",
                break_type={
                    "location_id": "CGJN03P1D08GF",
                    "break_name": "Lunch Break",
                    "expected_duration": "PT30M",
                    "is_paid": True,
                },
            )


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

    async def get(self, id: str, *, request_options: typing.Optional[RequestOptions] = None) -> GetBreakTypeResponse:
        """
        Returns a single `BreakType` specified by `id`.

        Parameters
        ----------
        id : str
            The UUID for the `BreakType` being retrieved.

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

        Returns
        -------
        GetBreakTypeResponse
            Success

        Examples
        --------
        import asyncio

        from square import AsyncSquare

        client = AsyncSquare(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.labor.break_types.get(
                id="id",
            )


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

    async def update(
        self, id: str, *, break_type: BreakTypeParams, request_options: typing.Optional[RequestOptions] = None
    ) -> UpdateBreakTypeResponse:
        """
        Updates an existing `BreakType`.

        Parameters
        ----------
        id : str
             The UUID for the `BreakType` being updated.

        break_type : BreakTypeParams
            The updated `BreakType`.

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

        Returns
        -------
        UpdateBreakTypeResponse
            Success

        Examples
        --------
        import asyncio

        from square import AsyncSquare

        client = AsyncSquare(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.labor.break_types.update(
                id="id",
                break_type={
                    "location_id": "26M7H24AZ9N6R",
                    "break_name": "Lunch",
                    "expected_duration": "PT50M",
                    "is_paid": True,
                    "version": 1,
                },
            )


        asyncio.run(main())
        """
        response = await self._raw_client.update(id, break_type=break_type, request_options=request_options)
        return response.data

    async def delete(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> DeleteBreakTypeResponse:
        """
        Deletes an existing `BreakType`.

        A `BreakType` can be deleted even if it is referenced from a `Shift`.

        Parameters
        ----------
        id : str
            The UUID for the `BreakType` being deleted.

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

        Returns
        -------
        DeleteBreakTypeResponse
            Success

        Examples
        --------
        import asyncio

        from square import AsyncSquare

        client = AsyncSquare(
            token="YOUR_TOKEN",
        )


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


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