import re


def findFiletypes(filelist):
    """
    Function interprets the list of files in the snapshot and determines which file extensions are present.
    Returns a unique list of file extensions.
    Some extensions are special cases. For now: gz can be anything just packed, so the extension before .gz is
    Just as relevant. Slurm "out" files are different from general "out" files (like BLAST outfiles) 
    and need to be placed together with the slurm output files. Slurm core files have the format core.[numbers]
    These core files need to be grouped together to be cleaned properly. 
    """

    # Switch dict for special cases. May be expanded along the way
    switch = {
    "gz":lambda x: (x.split(".")[-1] if len(x.split(".")) <= 2 else ".".join(x.split(".")[-2:])),
    "out" : lambda x: ("slurm" if re.search("slurm-.*\.out",x) else x.split(".")[-1]),
    "core": lambda x: (x.split(".")[0] if re.search("core.[0-9]*$",x) else x.split(".")[-1])
    }

    extensions = []
    # Slurm core files have a different format. This accounts for that
    for file in filelist:
        regex = "core.[0-9]*$" # core.23487
        if re.search(regex,file):
            ext = "core"
        else:
            ext = file.split(".")[-1]
        # If the extension is a special case, it gets the proper treatment. If not, it uses the default extension.
        extensions.append(switch.get(ext,lambda x: x.split(".")[-1])(file))
    return(list(set(extensions)))