"""Fast parallel proxy checks for the GUI (per-hop callbacks)."""
from __future__ import annotations

import asyncio
from typing import Callable

from .validator import _check_one

# High concurrency for interactive chain tests
GUI_VALIDATE_CONCURRENCY = 48


async def validate_each_parallel(
    urls: list[str],
    check_url: str,
    concurrency: int,
    timeout_seconds: float,
    on_result: Callable[[str, bool], None],
) -> None:
    if not urls:
        return
    sem = asyncio.Semaphore(max(1, concurrency))

    async def one(u: str) -> None:
        async with sem:
            ok = await _check_one(u, check_url, timeout_seconds)
        on_result(u, ok)

    await asyncio.gather(*(one(u) for u in urls))


def run_validate_each_sync(
    urls: list[str],
    check_url: str,
    timeout_seconds: float,
    on_result: Callable[[str, bool], None],
    concurrency: int = GUI_VALIDATE_CONCURRENCY,
) -> None:
    asyncio.run(
        validate_each_parallel(urls, check_url, concurrency, timeout_seconds, on_result)
    )
