from typing import Callable, Dict, Optional, Union

from edpconfigprovider import ConfigProvider, ConfigProviderType
from edpconfigprovider.impl.aws.builder import build_appconfig_agent_config_provider, build_appconfig_config_provider, build_ssm_config_provider
from edpconfigprovider.impl.json.builder import build_json_config_provider
from edpconfigprovider.parameters import ProviderParameters

_PROVIDER_TYPE = Union[ConfigProvider, Callable[[ProviderParameters], ConfigProvider]]

_PROVIDER_REGISTRY: Dict[str, _PROVIDER_TYPE] = {
    ConfigProviderType.JSON: build_json_config_provider,
    ConfigProviderType.AWS_APPCONFIG: build_appconfig_config_provider,
    ConfigProviderType.AWS_APPCONFIG_AGENT: build_appconfig_agent_config_provider,
    ConfigProviderType.AWS_SSM: build_ssm_config_provider,
}


def set_provider(provider_type: str, provider: _PROVIDER_TYPE):
    _PROVIDER_REGISTRY[provider_type] = provider


def get_provider(provider_type: str) -> Optional[_PROVIDER_TYPE]:
    return _PROVIDER_REGISTRY.get(provider_type)
