from typing import Dict

from edpdependencyinjection import DependencyContext
from messageprocessor import EventMessageProcessorBuilder, MessageProcessor

from ._utils import AcceptableProcessorDefinition


class DiEventMessageProcessorBuilder(EventMessageProcessorBuilder):
    def __init__(
        self,
        dependency_context: DependencyContext,
        processor_definitions: Dict[str, AcceptableProcessorDefinition]
    ):
        self.dependency_context = dependency_context
        self.processor_definitions = processor_definitions

    def build_message_processor(self, event_key: str):
        processor = self.processor_definitions[event_key]
        if isinstance(processor, MessageProcessor):
            return processor
        return self.dependency_context.get_instance(processor)

    def build_message_processor_dict(self) -> Dict[str, EventMessageProcessorBuilder]:
        ret = {}
        for key in self.processor_definitions.keys():
            ret[key] = self
        return ret