"""ThreadPool executor for running task functions."""
from __future__ import annotations

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

logger = logging.getLogger(__name__)


class ThreadExecutor:
    """Runs task callables in a thread pool."""

    def __init__(self, max_workers: int = 4):
        self._pool = ThreadPoolExecutor(max_workers=max_workers)
        self._loop: asyncio.AbstractEventLoop | None = None

    async def submit(self, fn: Callable, *args: Any, **kwargs: Any) -> Any:
        """Submit a function to the thread pool and await result."""
        loop = asyncio.get_running_loop()
        return await loop.run_in_executor(self._pool, lambda: fn(*args, **kwargs))

    async def shutdown(self) -> None:
        """Shutdown the thread pool."""
        self._pool.shutdown(wait=False)
        logger.info("ThreadExecutor shut down")
