"""SourceRegistry — maps tool names to SessionSource instances."""

from session_store.protocol import SessionSource


class SourceRegistry:
    """Maps tool names to SessionSource instances."""

    def __init__(self) -> None:
        self._sources: dict[str, SessionSource] = {}

    def register(self, source: SessionSource) -> None:
        """Register a SessionSource under its tool_name()."""
        self._sources[source.tool_name()] = source

    def get(self, tool: str) -> SessionSource:
        """Get the registered source for a tool name.

        Raises:
            KeyError: If no source is registered for the tool name.
        """
        return self._sources[tool]

    def all_sources(self) -> list[SessionSource]:
        """Return all registered sources."""
        return list(self._sources.values())

    def tool_names(self) -> list[str]:
        """Return all registered tool names."""
        return list(self._sources.keys())


def default_registry() -> SourceRegistry:
    """Create a registry with all default adapter sources registered.

    Uses lazy imports to avoid circular import issues — session-store
    depends on the adapter packages, but the adapters import types from
    session-store. The lazy imports inside this function break the cycle.
    """
    from copilot_session import CopilotSource
    from devin_session import DevinSource
    from opencode_session import OpenCodeSource

    registry = SourceRegistry()
    registry.register(DevinSource())
    registry.register(OpenCodeSource())
    registry.register(CopilotSource())
    return registry
