"""Detect whether packages are installed and install them during run-time as needed."""

# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/star.ipynb.

# %% auto 0
__all__ = ['check_package_installed', 'check_package_version_with_version_attribute', 'install_package',
           'ensure_package_installed']

# %% ../../nbs/star.ipynb 2
import importlib.util
import subprocess
import sys

# %% ../../nbs/star.ipynb 4
def check_package_installed(
        package_name: str
        ) -> bool:
    if importlib.util.find_spec(package_name) is not None:
        return True
        # print(f"{package_name} is installed.")
    else:
        # print(f"{package_name} is not installed.")
        return False


# %% ../../nbs/star.ipynb 6
def check_package_version_with_version_attribute(package_name):
    """Checks and returns the version of the specified package if available.
    
    Args:
        package_name (str): The name of the package to check.

    Returns:
        str or None: The version of the package or None if the package is not available.
    """
    # Check if the package is already imported
    if package_name in sys.modules:
        pkg = sys.modules[package_name]
    else:
        try:
            # Import the package dynamically
            pkg = __import__(package_name)
        except ImportError:
            return None  # Return None if the package is not available

    # Attempt to return the version attribute
    return getattr(pkg, '__version__', None)

# %% ../../nbs/star.ipynb 10
def install_package(package_name: str) -> None:
    """Install a package using pip."""
    subprocess.check_call([sys.executable, '-m', 'pip', 'install', package_name])

def ensure_package_installed(package_name: str) -> None:
    """Check if a package is installed and install it if not."""
    if check_package_installed(package_name):
        print(f"{package_name} is already installed.")
    else:
        print(f"{package_name} is not installed. Installing...")
        install_package(package_name)
        print(f"{package_name} has been installed.")

