"""Auto-detect framework result types and dispatch to the right mapper."""

from __future__ import annotations

from typing import Any

from ..models import Span


def extract(result: Any) -> tuple[Any, list[Span]] | None:
    """Try to extract output and spans from a framework result.

    Returns (output, spans) if a mapper matched, None otherwise.
    """
    # Pydantic AI
    try:
        from pydantic_ai import AgentRunResult

        if isinstance(result, AgentRunResult):
            from .pydantic_ai import extract as _extract

            return _extract(result)
    except ImportError:
        pass

    # Anthropic
    try:
        from anthropic.types import Message

        if isinstance(result, Message):
            from .anthropic import extract as _extract

            return _extract(result)
    except ImportError:
        pass

    # OpenAI / OpenRouter (same ChatCompletion type)
    try:
        from openai.types.chat import ChatCompletion

        if isinstance(result, ChatCompletion):
            from .openai import extract as _extract

            return _extract(result)
    except ImportError:
        pass

    # LangChain
    try:
        from langchain_core.messages import AIMessage

        if isinstance(result, AIMessage):
            from .langchain import extract as _extract

            return _extract(result)
    except ImportError:
        pass

    return None
