"""Exchange client factory."""

from __future__ import annotations

from typing import TYPE_CHECKING

from indiaopt.exceptions import ConfigurationError
from indiaopt.exchanges.base import ExchangeClient
from indiaopt.exchanges.bse import BSEClient
from indiaopt.exchanges.nse import NSEClient

if TYPE_CHECKING:
    from indiaopt.circuit import CircuitBreaker
    from indiaopt.config.settings import Settings


def get_client(
    exchange: str = "NSE",
    settings: Settings | None = None,
    breaker: CircuitBreaker | None = None,
) -> ExchangeClient:
    """Return the appropriate :class:`ExchangeClient` for *exchange*.

    Args:
        exchange: ``"NSE"`` or ``"BSE"`` (case-insensitive).
        settings: Optional :class:`~indiaopt.config.settings.Settings` override.
        breaker:  Optional :class:`~indiaopt.circuit.CircuitBreaker` override.

    Returns:
        :class:`~indiaopt.exchanges.nse.NSEClient` or
        :class:`~indiaopt.exchanges.bse.BSEClient`
    """
    ex = exchange.strip().upper()
    if ex == "NSE":
        return NSEClient(settings=settings, breaker=breaker)
    if ex == "BSE":
        return BSEClient(settings=settings, breaker=breaker)
    raise ConfigurationError(
        f"Unsupported exchange {exchange!r}. Must be 'NSE' or 'BSE'.",
        recovery="Pass exchange='NSE' or exchange='BSE'.",
    )


__all__ = ["get_client"]
