"""Process-level lru_cache for default MouseModel / ScrollModel instances."""
from __future__ import annotations

from collections.abc import Sequence
from functools import lru_cache
from pathlib import Path

from ai_mouse.mouse import MouseModel
from ai_mouse.scroll import ScrollModel


@lru_cache(maxsize=4)
def _get_mouse_model(
    model_key: str,
    providers_key: tuple[str, ...],
) -> MouseModel:
    path = None if model_key == "__bundled__" else Path(model_key)
    providers = list(providers_key) if providers_key else None
    return MouseModel(model_path=path, providers=providers)


@lru_cache(maxsize=4)
def _get_scroll_model(
    model_key: str,
    providers_key: tuple[str, ...],
) -> ScrollModel:
    path = None if model_key == "__bundled__" else Path(model_key)
    providers = list(providers_key) if providers_key else None
    return ScrollModel(model_path=path, providers=providers)


def get_mouse_model(
    model_path: str | Path | None,
    providers: Sequence[str] | None,
) -> MouseModel:
    key = "__bundled__" if model_path is None else str(model_path)
    return _get_mouse_model(key, tuple(providers or ()))


def get_scroll_model(
    model_path: str | Path | None,
    providers: Sequence[str] | None,
) -> ScrollModel:
    key = "__bundled__" if model_path is None else str(model_path)
    return _get_scroll_model(key, tuple(providers or ()))
