from __future__ import annotations

class EventBus:
    def __init__(self):
        self._handlers = {}

    def on(self, name, callback):
        self._handlers.setdefault(name, []).append(callback)

    def emit(self, name, *args, **kwargs):
        results = []
        for callback in self._handlers.get(name, []):
            results.append(callback(*args, **kwargs))
        return results

class PluginBase:
    name = "plugin"
    version = "0.0.1"
    enabled = True

    def setup(self, app_or_window):
        return None

class WidgetRegistry:
    def __init__(self):
        self._widgets = {}

    def register(self, name, renderer=None, handler=None):
        self._widgets[name] = {"renderer": renderer, "handler": handler}

    def list(self):
        return dict(self._widgets)

class PluginManager:
    def __init__(self, event_bus=None):
        self.event_bus = event_bus or EventBus()
        self.plugins = {}

    def register(self, plugin: PluginBase):
        self.plugins[plugin.name] = plugin
        return plugin.name

    def enable(self, name):
        if name in self.plugins:
            self.plugins[name].enabled = True

    def disable(self, name):
        if name in self.plugins:
            self.plugins[name].enabled = False

    def setup_all(self, target):
        for plugin in self.plugins.values():
            if getattr(plugin, "enabled", True):
                plugin.setup(target)
