import yaml
import logging


def load_manifest_metadata(metadata_file, metadata_str):
    """
    Loads the manifest metadata from a file and raw string; if multiple value
    are provied for the same key, the raw string takes precedence

    Parameters
    ----------
    metadata_file: str
        path to a YAML metadata file containing dict, or None
    metadata_str: str
        raw YAML metadata dict string, or None

    Returns
    -------
    manifest_metadata: dict
        manifest metadata dictionary
    """
    manifest_metadata = {}

    if metadata_file is not None:
        with open(metadata_file, 'r') as f:
            metadata = yaml.safe_load(f)
        manifest_metadata.update(metadata)

    if metadata_str is not None:
        metadata = yaml.safe_load(metadata_str)

        # Add warning for any keys that will be overridden
        replaced_keys = [
            k for k in metadata.keys()
            if k in manifest_metadata
        ]
        if len(replaced_keys):
            logging.warning(f'The following manifest metadata keys will be overridden: {replaced_keys}')

        manifest_metadata.update(metadata)

    return manifest_metadata