"""Async wrapper for synchronous app/ services"""

import asyncio
from concurrent.futures import ThreadPoolExecutor
from typing import Callable, Any


class ServiceWrapper:
    """Wraps synchronous services for async FastAPI endpoints."""

    def __init__(self, max_workers: int = 3):
        self._executor = ThreadPoolExecutor(max_workers=max_workers)

    async def run_sync(self, fn: Callable, *args, **kwargs) -> Any:
        loop = asyncio.get_event_loop()
        return await loop.run_in_executor(
            self._executor,
            lambda: fn(*args, **kwargs)
        )
