import logging
import json


def match_profile(profile_data, profile_dict, profile_matches):
    """
    Match current profiles to any previously created profiles
    """
    # If the profile_data dictionary was not populated in the read_profiles methods, there is nothing to match
    if profile_data:
        logging.info('Matching new profiles against profile file')
        # Extract the sequence type and allele dictionary from the profile file
        for st, allele_comprehension in profile_data.items():
            # Freeze the allele comprehension as above
            frozen_allele_comprehension = json.dumps(allele_comprehension, sort_keys=True)
            try:
                # Extract the samples that match this profile
                matches = profile_dict[hash(frozen_allele_comprehension)]
                # Update the dictionary with the matching samples
                profile_matches[st] = matches
            # The profile will not necessarily match any of the profiles found in the analysis
            except KeyError:
                pass
    return profile_matches