from __future__ import annotations

from typing import Dict
import threading

# Simple in-process cancel flags storage (per pipeline_id)
# Thread-safe for FastAPI workers in same process
_cancel_flags: Dict[str, bool] = {}
_lock = threading.Lock()


def request_cancel(pipeline_id: str) -> None:
    """Set cancel flag for given pipeline id."""
    pid = str(pipeline_id or "pipeline_editor")
    with _lock:
        _cancel_flags[pid] = True


def clear_cancel(pipeline_id: str) -> None:
    """Clear cancel flag for given pipeline id."""
    pid = str(pipeline_id or "pipeline_editor")
    with _lock:
        _cancel_flags.pop(pid, None)


def is_cancelled(pipeline_id: str) -> bool:
    """Check cancel flag for given pipeline id."""
    pid = str(pipeline_id or "pipeline_editor")
    with _lock:
        return bool(_cancel_flags.get(pid, False))