"""引擎注册表：config.yaml 里的 type 映射到具体引擎类（懒加载，避免未装的重依赖被导入）。"""

from __future__ import annotations

from .base import Engine, Transcript


def build_engine(cfg: dict) -> Engine:
    """按 config 的单个 engine 条目构造引擎实例。"""
    etype = cfg["type"]
    name = cfg.get("name", etype)

    if etype == "gummy":
        from .gummy import GummyEngine

        return GummyEngine(
            name=name,
            api_key=cfg["api_key"],
            model=cfg.get("model", "gummy-realtime-v1"),
            cost_per_min=cfg.get("cost_per_min"),
            realtime_factor=cfg.get("realtime_factor", 2.0),
        )
    if etype == "whisper":
        from .whisper import WhisperEngine

        return WhisperEngine(
            name=name,
            model_size=cfg.get("model_size", "small"),
            device=cfg.get("device", "cpu"),
            compute_type=cfg.get("compute_type", "int8"),
            model_dir=cfg.get("model_dir"),
        )
    if etype == "sensevoice":
        from .funasr import SenseVoiceEngine

        return SenseVoiceEngine(name=name, device=cfg.get("device", "cpu"), model_dir=cfg.get("model_dir"))
    if etype == "funasr":
        from .funasr import ParaformerEngine

        return ParaformerEngine(name=name, device=cfg.get("device", "cpu"), model_dir=cfg.get("model_dir"))

    raise ValueError(f"未知引擎类型: {etype}")


__all__ = ["Engine", "Transcript", "build_engine"]
