"""For basic functions"""

# AUTOGENERATED! DO NOT EDIT! File to edit: ../00_utils.ipynb.

# %% auto 0
__all__ = ["get_pkg_name", "get_src_path", "get_cache_dir", "log_time_elapsed"]

# %% ../00_utils.ipynb 3
import logging
import sys
import os
from pathlib import Path

import site


def get_pkg_name():
    pkg_name = Path(".").resolve().stem
    if pkg_name == "modules":
        pkg_name = Path(".").parent.resolve().stem
    return pkg_name


def get_src_path(
    pkg_name=None,
) -> str:
    """Get the package's source directory path.

    Returns:
        str: path
    """

    if pkg_name is None:
        pkg_name = get_pkg_name()

    src_path = f"{site.getsitepackages()[0]}/{pkg_name}/"
    if not Path(src_path).exists():
        logging.warning("package is installed in the development mode")
        # import pkg_name
        # src_path=str(Path(pkg_name.__file__).parent)
        if "__file__" in globals():
            # src_path = str(Path(os.path.realpath(__file__)).parent)
            src_path = Path(__file__).resolve().parent.as_posix()
        else:
            src_path = "."
            logging.warning("running the function in a nbdev nb")
    else:
        logging.info(src_path)

    src_path = Path(src_path).resolve().as_posix()
    if src_path.endswith("modules"):
        src_path = Path(src_path).parent.as_posix()
    return src_path


def get_cache_dir(
    pkg_name=None,
):
    if pkg_name is None:
        pkg_name = get_pkg_name()

    if os.name == "nt":  # Windows
        cache_dir = Path(os.getenv("LOCALAPPDATA")) / pkg_name
    elif os.name == "posix":
        if "XDG_CACHE_HOME" in os.environ:  # Unix-like with XDG
            cache_dir = Path(os.getenv("XDG_CACHE_HOME")) / pkg_name
        elif sys.platform == "darwin":  # macOS
            cache_dir = Path.home() / "Library" / "Caches" / pkg_name
        else:  # Default for Linux and other Unix-like systems
            cache_dir = Path.home() / ".cache" / pkg_name
    else:
        raise RuntimeError("Unsupported operating system")

    return str(cache_dir)


def log_time_elapsed(start):
    """Log time elapsed.

    Args:
        start (datetime): start tile

    Returns:
        datetime: difference in time.
    """
    from datetime import datetime

    diff = datetime.now() - start
    return diff
