from typing import Union


def parse_unique_ids_by_keys(
    bib_data: list, keys: Union[str, list], unique_id_prefix: dict
) -> list:
    """
    parse_unique_ids_by_keys parses bib data based on keys.

    :param bib_data: list of bib records loaded from a bib file
    :param keys: list of keys as the lookup order, e.g., ["doi", "arxivid", "pmid"]
    :param unique_id_prefix: a dictionary to specify what prefix to use for each key
    """

    ids = []
    for i in bib_data:
        i_unique_id = ""
        for k in keys:
            k_prefix = unique_id_prefix.get(k, "")
            i_k_value = i.get(k, "")
            # We do not need the version in the arxivids
            if k == "arxivid":
                i_k_value = i_k_value.split("v")[0]
            if i_k_value:
                i_unique_id = f"{k_prefix}{i_k_value}"
                break
        ids.append(i_unique_id)

    return ids