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

from ..core.client_wrapper import SyncClientWrapper
from .raw_client import RawEmployeesClient
import typing
from ..types.employee_status import EmployeeStatus
from ..core.request_options import RequestOptions
from ..core.pagination import SyncPager
from ..types.employee import Employee
from ..types.list_employees_response import ListEmployeesResponse
from ..core.unchecked_base_model import construct_type
from json.decoder import JSONDecodeError
from ..core.api_error import ApiError
from ..types.get_employee_response import GetEmployeeResponse
from ..core.client_wrapper import AsyncClientWrapper
from .raw_client import AsyncRawEmployeesClient
from ..core.pagination import AsyncPager


class EmployeesClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawEmployeesClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawEmployeesClient
        """
        return self._raw_client

    def list(
        self,
        *,
        location_id: typing.Optional[str] = None,
        status: typing.Optional[EmployeeStatus] = None,
        limit: typing.Optional[int] = None,
        cursor: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> SyncPager[Employee]:
        """


        Parameters
        ----------
        location_id : typing.Optional[str]


        status : typing.Optional[EmployeeStatus]
            Specifies the EmployeeStatus to filter the employee by.

        limit : typing.Optional[int]
            The number of employees to be returned on each page.

        cursor : typing.Optional[str]
            The token required to retrieve the specified page of results.

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

        Returns
        -------
        SyncPager[Employee]
            Success

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

        client = Square(
            token="YOUR_TOKEN",
        )
        response = client.employees.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/employees",
            method="GET",
            params={
                "location_id": location_id,
                "status": status,
                "limit": limit,
                "cursor": cursor,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _parsed_response = typing.cast(
                    ListEmployeesResponse,
                    construct_type(
                        type_=ListEmployeesResponse,  # 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,
                    status=status,
                    limit=limit,
                    cursor=_parsed_next,
                    request_options=request_options,
                )
                _items = _parsed_response.employees
                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 get(self, id: str, *, request_options: typing.Optional[RequestOptions] = None) -> GetEmployeeResponse:
        """


        Parameters
        ----------
        id : str
            UUID for the employee that was requested.

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

        Returns
        -------
        GetEmployeeResponse
            Success

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

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


class AsyncEmployeesClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawEmployeesClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawEmployeesClient
        """
        return self._raw_client

    async def list(
        self,
        *,
        location_id: typing.Optional[str] = None,
        status: typing.Optional[EmployeeStatus] = None,
        limit: typing.Optional[int] = None,
        cursor: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncPager[Employee]:
        """


        Parameters
        ----------
        location_id : typing.Optional[str]


        status : typing.Optional[EmployeeStatus]
            Specifies the EmployeeStatus to filter the employee by.

        limit : typing.Optional[int]
            The number of employees to be returned on each page.

        cursor : typing.Optional[str]
            The token required to retrieve the specified page of results.

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

        Returns
        -------
        AsyncPager[Employee]
            Success

        Examples
        --------
        import asyncio

        from square import AsyncSquare

        client = AsyncSquare(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            response = await client.employees.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/employees",
            method="GET",
            params={
                "location_id": location_id,
                "status": status,
                "limit": limit,
                "cursor": cursor,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _parsed_response = typing.cast(
                    ListEmployeesResponse,
                    construct_type(
                        type_=ListEmployeesResponse,  # 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,
                    status=status,
                    limit=limit,
                    cursor=_parsed_next,
                    request_options=request_options,
                )
                _items = _parsed_response.employees
                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 get(self, id: str, *, request_options: typing.Optional[RequestOptions] = None) -> GetEmployeeResponse:
        """


        Parameters
        ----------
        id : str
            UUID for the employee that was requested.

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

        Returns
        -------
        GetEmployeeResponse
            Success

        Examples
        --------
        import asyncio

        from square import AsyncSquare

        client = AsyncSquare(
            token="YOUR_TOKEN",
        )


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


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