"""Common utilities shared between sync and async clients."""

from typing import Any


def calculate_backoff_delay(attempt: int) -> float:
    """Calculate exponential backoff delay.

    Args:
        attempt: The current attempt number (0-indexed)

    Returns:
        Delay in seconds (2^attempt)
    """
    return 2.0**attempt


def mask_sensitive_params(params: dict[str, Any]) -> dict[str, Any]:
    """Filter sensitive data from parameters for logging.

    Args:
        params: Dictionary of parameters

    Returns:
        Dictionary with sensitive values masked
    """
    return {k: v for k, v in params.items() if k not in ("password",)}


def should_retry_on_error(status_code: int, attempt: int, max_retries: int) -> bool:
    """Determine if request should be retried.

    Args:
        status_code: HTTP status code
        attempt: Current attempt number (0-indexed)
        max_retries: Maximum number of retries

    Returns:
        True if should retry, False otherwise
    """
    # Don't retry on 401 (handled separately)
    if status_code == 401:
        return False

    # Don't retry if we've exhausted attempts
    if attempt >= max_retries - 1:
        return False

    # Retry on server errors (5xx) and rate limiting (429)
    if status_code >= 500 or status_code == 429:
        return True

    # Retry on other 4xx errors (except 401, 404)
    if 400 <= status_code < 500 and status_code not in (401, 404):
        return True

    return False


def validate_session_id(session_id: str | None) -> bool:
    """Check if session ID is valid.

    Args:
        session_id: Session ID to validate

    Returns:
        True if valid, False otherwise
    """
    return session_id is not None and bool(session_id.strip())
