Prepare service for catalog endpoint

    Parameters:
        data (Union[str, dict]): Service ID or service definition
    Returns:
        Tuple[str, dict]: str is ID and dict is service

    Transform ``/v1/health/state/<state>``::

        {
            "Node": "foobar",
            "CheckID": "service:redis",
            "Name": "Service 'redis' check",
            "Status": "passing",
            "Notes": "",
            "Output": "",
            "ServiceID": "redis1",
            "ServiceName": "redis"
        }

    to::

        {
            "ID": "redis1",
            "Service": "redis"
        }

    Extract from /v1/health/service/<service>::

        {
            "Node": {...},
            "Service": {
                "ID": "redis1",
                "Service": "redis",
                "Tags": None,
                "Address": "10.1.10.12",
                "Port": 8000
            },
            "Checks": [...]
        }

def prepare_service(data):
    """Prepare service for catalog endpoint

    Parameters:
        data (Union[str, dict]): Service ID or service definition
    Returns:
        Tuple[str, dict]: str is ID and dict is service

    Transform ``/v1/health/state/<state>``::

        {
            "Node": "foobar",
            "CheckID": "service:redis",
            "Name": "Service 'redis' check",
            "Status": "passing",
            "Notes": "",
            "Output": "",
            "ServiceID": "redis1",
            "ServiceName": "redis"
        }

    to::

        {
            "ID": "redis1",
            "Service": "redis"
        }

    Extract from /v1/health/service/<service>::

        {
            "Node": {...},
            "Service": {
                "ID": "redis1",
                "Service": "redis",
                "Tags": None,
                "Address": "10.1.10.12",
                "Port": 8000
            },
            "Checks": [...]
        }

    """
    if not data:
        return None, {}

    if isinstance(data, str):
        return data, {}

    # from /v1/health/service/<service>
    if all(field in data for field in ("Node", "Service", "Checks")):
        return data["Service"]["ID"], data["Service"]

    # from /v1/health/checks/<service>
    # from /v1/health/node/<node>
    # from /v1/health/state/<state>
    # from /v1/catalog/service/<service>
    if all(field in data for field in ("ServiceName", "ServiceID")):
        return data["ServiceID"], {
            "ID": data["ServiceID"],
            "Service": data["ServiceName"],
            "Tags": data.get("ServiceTags"),
            "Address": data.get("ServiceAddress"),
            "Port": data.get("ServicePort"),
        }

    if list(data) == ["ID"]:
        return data["ID"], {}

    result = {}
    if "Name" in data:
        result["Service"] = data["Name"]

    for k in ("Service", "ID", "Tags", "Address", "Port"):
        if k in data:
            result[k] = data[k]
    return result.get("ID"), result