"""Optional uWSGI spool support with a safe non-uWSGI fallback.

The search index tasks are explicitly deferred work.  Running them inline when
uWSGI is unavailable makes ordinary request/response signals perform index
updates synchronously.  The fallback therefore preserves normal direct calls
to the decorated function, while treating ``.spool()`` as unavailable.
"""

try:
    from uwsgidecorators import spool  # type: ignore[import-not-found]
except ImportError:

    class _UnavailableSpool:
        def __init__(self, function, *, pass_arguments):
            self.function = function
            self.pass_arguments = pass_arguments

        def __call__(self, *args, **kwargs):
            return self.function(*args, **kwargs)

        def spool(self, *args, **kwargs):
            """Do not run deferred search work inline outside uWSGI."""
            return None

    def spool(function=None, *, pass_arguments=False):
        def decorate(callback):
            return _UnavailableSpool(callback, pass_arguments=pass_arguments)

        return decorate if function is None else decorate(function)
