"""This module implements the default tracker event handler."""

from __future__ import annotations

from typing import TYPE_CHECKING

from .base import EventHandler

if TYPE_CHECKING:
    from collections.abc import Callable

    from ropt.enums import EventType
    from ropt.workflow import Event


class DefaultObserverHandler(EventHandler):
    """The default event handler for observing events.

    This event handler listens for events and forwards them to one or more
    callback functions.
    """

    def __init__(
        self, *, event_types: set[EventType], callback: Callable[[Event], None]
    ) -> None:
        """Initialize a default event handler.

        This event handler responds to events by calling `callback` if the event
        type matches `event_types`.

        Args:
            event_types: The set of event types  to respond to.
            callback:    The callable to call.
        """
        super().__init__()
        self._event_types = event_types
        self._callback = callback

    def handle_event(self, event: Event) -> None:
        """Handle incoming events.

        This method processes events emitted from within the workflow.

        If a event containing results is received, and its type equals the
        stored event type, the stored callback is called.

        Args:
            event: The event object emitted from the workflow.
        """
        if event.event_type in self._event_types:
            self._callback(event)

    @property
    def event_types(self) -> set[EventType]:
        """Return the event types that are handled.

        Returns:
            A set of event types that are handled.
        """
        return self._event_types
