#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
VWED.event 模块 - 事件系统
"""

from typing import Dict, Any, Callable
from ..script_registry_service import get_global_registry


class VWEDEventModule:
    """VWED.event 模块 - 事件系统"""
    
    def __init__(self, script_id: str):
        self.script_id = script_id
        self.registry = get_global_registry()
    
    def listen(self, event_name: str, handler: Callable, priority: int = 1):
        """注册事件监听器
            
        分离式模式：
        VWED.event.listen(
            event_name="user_login",
            handler=on_user_login
        )
        """
        def decorator(func: Callable):
            self.registry.register_event_listener(
                event_name=event_name,
                handler=func,
                script_id=self.script_id,
                priority=priority
            )
            return func
            
        return decorator(handler)

    
    async def emit(self, event_name: str, data: Dict[str, Any]):
        """触发事件"""
        await self.registry.emit_event(event_name, data)
    
    def on(self, event_name: str, priority: int = 1):
        """listen的别名"""
        return self.listen(event_name, priority=priority)