from __future__ import annotations

import asyncio
import time

_draining: bool = False
_bridge_drain_active: bool = False
_in_flight: int = 0


def reset() -> None:
    global _draining, _bridge_drain_active, _in_flight
    _draining = False
    _bridge_drain_active = False
    _in_flight = 0


def set_draining(val: bool = True) -> None:
    global _draining
    _draining = val


def is_draining() -> bool:
    return _draining


def set_bridge_drain_active(val: bool = True) -> None:
    global _bridge_drain_active
    _bridge_drain_active = val


def is_bridge_drain_active() -> bool:
    return _bridge_drain_active


def increment_in_flight() -> None:
    global _in_flight
    _in_flight += 1


def decrement_in_flight() -> None:
    global _in_flight
    _in_flight = max(0, _in_flight - 1)


def get_in_flight() -> int:
    return _in_flight


async def wait_for_in_flight_drain(timeout_seconds: float, poll_interval_seconds: float = 0.1) -> bool:
    deadline = time.monotonic() + timeout_seconds
    while get_in_flight() > 0 and time.monotonic() < deadline:
        await asyncio.sleep(poll_interval_seconds)
    return get_in_flight() == 0
