from http import HTTPStatus
from typing import Any

import httpx

from ... import errors
from ...client import AuthenticatedClient, Client
from ...models.poll_local_business_search_body import PollLocalBusinessSearchBody
from ...models.poll_local_business_search_response_200 import PollLocalBusinessSearchResponse200
from ...models.poll_local_business_search_response_400 import PollLocalBusinessSearchResponse400
from ...models.poll_local_business_search_response_401 import PollLocalBusinessSearchResponse401
from ...models.poll_local_business_search_response_402 import PollLocalBusinessSearchResponse402
from ...models.poll_local_business_search_response_403 import PollLocalBusinessSearchResponse403
from ...models.poll_local_business_search_response_404 import PollLocalBusinessSearchResponse404
from ...models.poll_local_business_search_response_429 import PollLocalBusinessSearchResponse429
from ...models.poll_local_business_search_response_500 import PollLocalBusinessSearchResponse500
from ...models.poll_local_business_search_response_503 import PollLocalBusinessSearchResponse503
from ...types import Response


def _get_kwargs(
    *,
    body: PollLocalBusinessSearchBody,
) -> dict[str, Any]:
    headers: dict[str, Any] = {}

    _kwargs: dict[str, Any] = {
        "method": "post",
        "url": "/v1/local-business-search/poll",
    }

    _kwargs["json"] = body.to_dict()

    headers["Content-Type"] = "application/json"

    _kwargs["headers"] = headers
    return _kwargs


def _parse_response(
    *, client: AuthenticatedClient | Client, response: httpx.Response
) -> (
    PollLocalBusinessSearchResponse200
    | PollLocalBusinessSearchResponse400
    | PollLocalBusinessSearchResponse401
    | PollLocalBusinessSearchResponse402
    | PollLocalBusinessSearchResponse403
    | PollLocalBusinessSearchResponse404
    | PollLocalBusinessSearchResponse429
    | PollLocalBusinessSearchResponse500
    | PollLocalBusinessSearchResponse503
    | None
):
    if response.status_code == 200:
        response_200 = PollLocalBusinessSearchResponse200.from_dict(response.json())

        return response_200

    if response.status_code == 400:
        response_400 = PollLocalBusinessSearchResponse400.from_dict(response.json())

        return response_400

    if response.status_code == 401:
        response_401 = PollLocalBusinessSearchResponse401.from_dict(response.json())

        return response_401

    if response.status_code == 402:
        response_402 = PollLocalBusinessSearchResponse402.from_dict(response.json())

        return response_402

    if response.status_code == 403:
        response_403 = PollLocalBusinessSearchResponse403.from_dict(response.json())

        return response_403

    if response.status_code == 404:
        response_404 = PollLocalBusinessSearchResponse404.from_dict(response.json())

        return response_404

    if response.status_code == 429:
        response_429 = PollLocalBusinessSearchResponse429.from_dict(response.json())

        return response_429

    if response.status_code == 500:
        response_500 = PollLocalBusinessSearchResponse500.from_dict(response.json())

        return response_500

    if response.status_code == 503:
        response_503 = PollLocalBusinessSearchResponse503.from_dict(response.json())

        return response_503

    if client.raise_on_unexpected_status:
        raise errors.UnexpectedStatus(response.status_code, response.content)
    else:
        return None


def _build_response(
    *, client: AuthenticatedClient | Client, response: httpx.Response
) -> Response[
    PollLocalBusinessSearchResponse200
    | PollLocalBusinessSearchResponse400
    | PollLocalBusinessSearchResponse401
    | PollLocalBusinessSearchResponse402
    | PollLocalBusinessSearchResponse403
    | PollLocalBusinessSearchResponse404
    | PollLocalBusinessSearchResponse429
    | PollLocalBusinessSearchResponse500
    | PollLocalBusinessSearchResponse503
]:
    return Response(
        status_code=HTTPStatus(response.status_code),
        content=response.content,
        headers=response.headers,
        parsed=_parse_response(client=client, response=response),
    )


def sync_detailed(
    *,
    client: AuthenticatedClient | Client,
    body: PollLocalBusinessSearchBody,
) -> Response[
    PollLocalBusinessSearchResponse200
    | PollLocalBusinessSearchResponse400
    | PollLocalBusinessSearchResponse401
    | PollLocalBusinessSearchResponse402
    | PollLocalBusinessSearchResponse403
    | PollLocalBusinessSearchResponse404
    | PollLocalBusinessSearchResponse429
    | PollLocalBusinessSearchResponse500
    | PollLocalBusinessSearchResponse503
]:
    """Poll local business search

     Coming Soon! Poll a local business search task

    <span>⚡ <strong>Rate limit:</strong> 60 requests per 1 minute</span>

    Args:
        body (PollLocalBusinessSearchBody):

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        Response[PollLocalBusinessSearchResponse200 | PollLocalBusinessSearchResponse400 | PollLocalBusinessSearchResponse401 | PollLocalBusinessSearchResponse402 | PollLocalBusinessSearchResponse403 | PollLocalBusinessSearchResponse404 | PollLocalBusinessSearchResponse429 | PollLocalBusinessSearchResponse500 | PollLocalBusinessSearchResponse503]
    """

    kwargs = _get_kwargs(
        body=body,
    )

    response = client.get_httpx_client().request(
        **kwargs,
    )

    return _build_response(client=client, response=response)


def sync(
    *,
    client: AuthenticatedClient | Client,
    body: PollLocalBusinessSearchBody,
) -> (
    PollLocalBusinessSearchResponse200
    | PollLocalBusinessSearchResponse400
    | PollLocalBusinessSearchResponse401
    | PollLocalBusinessSearchResponse402
    | PollLocalBusinessSearchResponse403
    | PollLocalBusinessSearchResponse404
    | PollLocalBusinessSearchResponse429
    | PollLocalBusinessSearchResponse500
    | PollLocalBusinessSearchResponse503
    | None
):
    """Poll local business search

     Coming Soon! Poll a local business search task

    <span>⚡ <strong>Rate limit:</strong> 60 requests per 1 minute</span>

    Args:
        body (PollLocalBusinessSearchBody):

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        PollLocalBusinessSearchResponse200 | PollLocalBusinessSearchResponse400 | PollLocalBusinessSearchResponse401 | PollLocalBusinessSearchResponse402 | PollLocalBusinessSearchResponse403 | PollLocalBusinessSearchResponse404 | PollLocalBusinessSearchResponse429 | PollLocalBusinessSearchResponse500 | PollLocalBusinessSearchResponse503
    """

    return sync_detailed(
        client=client,
        body=body,
    ).parsed


async def asyncio_detailed(
    *,
    client: AuthenticatedClient | Client,
    body: PollLocalBusinessSearchBody,
) -> Response[
    PollLocalBusinessSearchResponse200
    | PollLocalBusinessSearchResponse400
    | PollLocalBusinessSearchResponse401
    | PollLocalBusinessSearchResponse402
    | PollLocalBusinessSearchResponse403
    | PollLocalBusinessSearchResponse404
    | PollLocalBusinessSearchResponse429
    | PollLocalBusinessSearchResponse500
    | PollLocalBusinessSearchResponse503
]:
    """Poll local business search

     Coming Soon! Poll a local business search task

    <span>⚡ <strong>Rate limit:</strong> 60 requests per 1 minute</span>

    Args:
        body (PollLocalBusinessSearchBody):

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        Response[PollLocalBusinessSearchResponse200 | PollLocalBusinessSearchResponse400 | PollLocalBusinessSearchResponse401 | PollLocalBusinessSearchResponse402 | PollLocalBusinessSearchResponse403 | PollLocalBusinessSearchResponse404 | PollLocalBusinessSearchResponse429 | PollLocalBusinessSearchResponse500 | PollLocalBusinessSearchResponse503]
    """

    kwargs = _get_kwargs(
        body=body,
    )

    response = await client.get_async_httpx_client().request(**kwargs)

    return _build_response(client=client, response=response)


async def asyncio(
    *,
    client: AuthenticatedClient | Client,
    body: PollLocalBusinessSearchBody,
) -> (
    PollLocalBusinessSearchResponse200
    | PollLocalBusinessSearchResponse400
    | PollLocalBusinessSearchResponse401
    | PollLocalBusinessSearchResponse402
    | PollLocalBusinessSearchResponse403
    | PollLocalBusinessSearchResponse404
    | PollLocalBusinessSearchResponse429
    | PollLocalBusinessSearchResponse500
    | PollLocalBusinessSearchResponse503
    | None
):
    """Poll local business search

     Coming Soon! Poll a local business search task

    <span>⚡ <strong>Rate limit:</strong> 60 requests per 1 minute</span>

    Args:
        body (PollLocalBusinessSearchBody):

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        PollLocalBusinessSearchResponse200 | PollLocalBusinessSearchResponse400 | PollLocalBusinessSearchResponse401 | PollLocalBusinessSearchResponse402 | PollLocalBusinessSearchResponse403 | PollLocalBusinessSearchResponse404 | PollLocalBusinessSearchResponse429 | PollLocalBusinessSearchResponse500 | PollLocalBusinessSearchResponse503
    """

    return (
        await asyncio_detailed(
            client=client,
            body=body,
        )
    ).parsed
