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

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

    class _SynchronousSpool:
        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):
            if self.pass_arguments:
                return self.function(*args, **kwargs)
            return self.function(kwargs)

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

        return decorate if function is None else decorate(function)
