"""Private helpers for legacy Azure callback compatibility."""

from __future__ import annotations

import inspect
from collections.abc import Callable
from typing import Any


def _supports_raise_on_error(callable_obj: Callable[..., Any]) -> bool:
    """Return whether ``callable_obj`` accepts ``raise_on_error``."""

    try:
        parameters = inspect.signature(callable_obj).parameters.values()
    except (TypeError, ValueError):
        return True
    return any(
        parameter.kind is inspect.Parameter.VAR_KEYWORD or parameter.name == "raise_on_error"
        for parameter in parameters
    )


def _call_with_raise_on_error(callable_obj: Callable[..., Any], *args: Any, raise_on_error: bool) -> Any:
    """Call a legacy hook without masking internal ``TypeError`` failures."""

    if _supports_raise_on_error(callable_obj):
        return callable_obj(*args, raise_on_error=raise_on_error)
    return callable_obj(*args)
