"""
This module provides a global dependency loader for the application.

It ensures that a single instance of the DependencyLoader is created and used
throughout the application's lifecycle.
"""
from threading import Lock
from typing import Optional

from edpdependencyinjection import DependencyLoader

_GLOBAL_DEPENDENCY_LOADER: Optional[DependencyLoader] = None
_LOCK = Lock()


def get_dependency_loader() -> DependencyLoader:
    """Returns the global instance of the DependencyLoader.

    If the instance has not been created yet, it will be instantiated
    in a thread-safe manner.

    Returns:
            AnyType: The built dependency instance.
    """
    global _GLOBAL_DEPENDENCY_LOADER
    if _GLOBAL_DEPENDENCY_LOADER is None:
        with _LOCK:
            if _GLOBAL_DEPENDENCY_LOADER is None:
                _GLOBAL_DEPENDENCY_LOADER = DependencyLoader()
    return _GLOBAL_DEPENDENCY_LOADER
