from typing import Optional
import logging


def extract_dataset_config(model_name: str, accuracy_config: dict) -> Optional[dict]:
    """
        Find and return a dataset config from a given model config, if exists.

    Model config is expected to have "datasets" section,
    which is expected to have one entry with a dataset-related configurations.

    There may be special cases:

    * the section may be absent;
    * the section may be empty;
    * the section may have several enrties.

    These cases should not lead to fatal errors.

    :param model_name: str - name of the model
    :param accuracy_config: dict - content of the accuracy configuration for the model
    :return: dict - the first entry from "datasets" section from accuracy configuration if exists, otherwise None
    """

    datasets_section = accuracy_config.get('datasets')
    if not datasets_section:
        return None

    if len(datasets_section) > 1:
        logging.warning(
            'More then one suitable entry in the "datasets" section of %s config from %s.yml. '
            'Using the first one.',
            accuracy_config['name'],
            model_name
        )
    return datasets_section[0]