"""Module to manage static values defined in Python packages."""

import logging


class ValueStorage:
    """Class to store static values."""

    def __init__(self):
        self.__values = {}

    def add_values(self, group, values):
        """Add values to the storage.

        :param group: string group name for the values.
        :param values: dictionary with string names as keys and iterables
            with arbitrary objects as values.
        :raises: :class:`ValueError` if values object is invalid.
        """
        group = str(group)

        logger = logging.getLogger(__name__)
        logger.debug("Try to add values to the group '%s' into the storage %s.", group, self)

        try:
            values_dict = dict(values)
        except (TypeError, ValueError) as error:
            logger.exception("Values map is not a valid dictionary.")
            raise ValueError("Values map is not a valid dictionary") from error

        refined_values = {}
        for name, values_list in values_dict.items():
            name = str(name)

            if name in refined_values:
                logger.error("Values map has conflicting names.")
                raise ValueError("Values map has conflicting names")

            try:
                refined_values_list = tuple(values_list)
            except TypeError as error:
                logger.exception("Can't iterate over values for one of the names.")
                raise ValueError("Can't iterate over values for one of the names") from error

            refined_values[name] = refined_values_list

        values_group = self.__values.setdefault(group, {})

        for name, values_list in refined_values.items():
            values_group.setdefault(name, []).extend(values_list)

        logger.info("Added values to the group '%s' into the storage %s.", group, self)

    def get_values(self, group):
        """Get stored values.

        :param group: string group name with values.
        :returns: dictionary with string names as keys and tuples with stored objects as values.
        """
        group = str(group)
        values_group = self.__values.get(group, {})
        values = {name: tuple(values) for name, values in values_group.items()}

        logging.getLogger(__name__).info(
            "Got values of group '%s' from the storage %s ",
            group,
            self,
        )

        return values
