"""Test instrumentation dispatcher.

Delegates to sync or async instrumentation paths based on
``FunctionToOptimize.is_async``.
"""

from __future__ import annotations

import logging
from typing import TYPE_CHECKING

from .._model import TestingMode
from ._instrument_async import inject_async_profiling_into_existing_test
from ._instrument_sync import inject_sync_profiling_into_existing_test

if TYPE_CHECKING:
    from pathlib import Path

    from .._model import FunctionToOptimize
    from ..test_discovery.models import CodePosition

log = logging.getLogger(__name__)


def inject_profiling_into_existing_test(
    test_path: Path,
    call_positions: list[CodePosition],
    function_to_optimize: FunctionToOptimize,
    tests_project_root: Path,
    mode: TestingMode = TestingMode.BEHAVIOR,
) -> tuple[bool, str | None]:
    """Inject instrumentation into an existing test file.

    For async functions, delegates to async-specific call-site injection.
    For sync functions, delegates to sync-specific call-site injection.
    Returns *(did_instrument, modified_source)*.
    """
    if function_to_optimize.is_async:
        return inject_async_profiling_into_existing_test(
            test_path,
            call_positions,
            function_to_optimize,
        )
    return inject_sync_profiling_into_existing_test(
        test_path,
        call_positions,
        function_to_optimize,
    )
