from __future__ import annotations

from typing import List, Optional

from analogai.deepthink.internal.processors.fact_execution_strategy import (
    FactExecutionMode,
    ParallelFactExecutor,
    FactTaskHandler,
    SequentialFactExecutor,
)


class FactTaskRunner:
    def __init__(self) -> None:
        self._sequential = SequentialFactExecutor()

    def run(
        self,
        segments: List[str],
        handler: FactTaskHandler,
        mode: FactExecutionMode,
        max_workers: int | None = None,
    ) -> List[Optional[str]]:
        if not segments:
            return []
        if mode == FactExecutionMode.SEQUENTIAL or len(segments) <= 1:
            return self._sequential.execute(segments, handler)
        if max_workers is not None and max_workers <= 1:
            return self._sequential.execute(segments, handler)
        return ParallelFactExecutor(max_workers=max_workers).execute(segments, handler)
