# This file is auto-generated by oagen. Do not edit.

from __future__ import annotations

from dataclasses import dataclass
from typing import Any, ClassVar, Union, cast

from workos._types import _raise_deserialize_error

from .connect_application_m2m import ConnectApplicationM2M
from .connect_application_oauth import ConnectApplicationOAuth


@dataclass(slots=True)
class ConnectApplicationUnknown:
    """Unknown variant of ConnectApplication not yet recognized by this SDK version."""

    raw_data: dict[str, Any]
    """The raw payload, preserved so callers can still inspect the data."""

    @classmethod
    def from_dict(cls, data: dict[str, Any]) -> ConnectApplicationUnknown:
        """Wrap raw data in an unknown variant."""
        return cls(raw_data=data)

    def to_dict(self) -> dict[str, Any]:
        """Return the original raw data."""
        return dict(self.raw_data)


ConnectApplicationVariant = Union[
    ConnectApplicationM2M,
    ConnectApplicationOAuth,
    ConnectApplicationUnknown,
]


class ConnectApplication:
    """Discriminated union dispatcher (discriminated by 'application_type')."""

    _DISPATCH: ClassVar[dict[str, type]] = {
        "m2m": ConnectApplicationM2M,
        "oauth": ConnectApplicationOAuth,
    }

    @classmethod
    def from_dict(cls, data: dict[str, Any]) -> ConnectApplicationVariant:
        """Deserialize from a dictionary, dispatching to the correct variant."""
        if "application_type" not in data:
            _raise_deserialize_error(
                "ConnectApplication",
                ValueError("Missing required field 'application_type'"),
            )
        disc_value = data["application_type"]
        if disc_value is None:
            _raise_deserialize_error(
                "ConnectApplication", ValueError("application_type must not be None")
            )
        dispatch_cls = cls._DISPATCH.get(disc_value)
        if dispatch_cls is not None:
            return cast("ConnectApplicationVariant", dispatch_cls.from_dict(data))
        return ConnectApplicationUnknown.from_dict(data)
