"""Polytropos helpers for Odoo addons.

This library is meant to be used by Odoo addons at runtime.
"""

from typing import Any

from packaging.specifiers import SpecifierSet
from packaging.version import Version


def _odoo() -> Any:
    """Get the Odoo package (lazy import).

    Returns:
        The odoo module

    Raises:
        ModuleNotFoundError: If Odoo is not installed or not importable.
    """
    import odoo

    return odoo


def rel(spec: str) -> bool:
    """Check if current Odoo release matches a PEP 440 version specifier.

    This function checks the Odoo release (e.g., "17.0", "18.0") against
    a PEP 440 specifier, useful for conditional code execution.

    Args:
        spec: PEP 440 version specifier (e.g., ">=17.0", ">=18.0,<19.0")

    Returns:
        True if current Odoo release matches the spec, False otherwise.
    """
    current = Version(_odoo().release.series)
    return current in SpecifierSet(spec)
