from typing import Callable, Optional

import pyrogram


class OnRawUpdate:
    def on_raw_update(
        self: Optional["OnRawUpdate"] = None,
        group: int = 0,
    ) -> Callable:

        def decorator(func: Callable) -> Callable:
            if isinstance(self, pyrogram.Client):
                self.add_handler(pyrogram.handlers.RawUpdateHandler(func), group)
            else:
                if not hasattr(func, "handlers"):
                    func.handlers = []

                func.handlers.append((pyrogram.handlers.RawUpdateHandler(func), group))

            return func

        return decorator
