"""Futures indicator calculator factory."""

from .base import FuturesIndicatorCalculator


class FuturesIndicatorFactory:
    """Factory for creating FuturesIndicatorCalculator instances.

    期货指标计算器工厂。
    """

    _calculators: dict[str, type[FuturesIndicatorCalculator]] = {}

    @classmethod
    def get_calculator(cls, calculator_type: str = "simple") -> FuturesIndicatorCalculator:
        """Get a FuturesIndicatorCalculator instance.

        Args:
            calculator_type: Calculator backend type, default 'simple'.

        Returns:
            FuturesIndicatorCalculator: Calculator instance.

        Raises:
            ValueError: If calculator type is not found.
        """
        if not cls._calculators:
            cls._register_defaults()

        calculator_class = cls._calculators.get(calculator_type)
        if not calculator_class:
            raise ValueError(f"Unknown futures indicator calculator type: {calculator_type}")

        return calculator_class()

    @classmethod
    def register_calculator(
        cls, name: str, calculator_class: type[FuturesIndicatorCalculator]
    ) -> None:
        """Register a new calculator backend.

        Args:
            name: Backend name.
            calculator_class: Calculator class.
        """
        cls._calculators[name] = calculator_class

    @classmethod
    def _register_defaults(cls) -> None:
        """Lazily register default calculator backends."""
        from .simple import SimpleFuturesIndicatorCalculator

        cls._calculators.setdefault("simple", SimpleFuturesIndicatorCalculator)
