import json


def prediction_class_names(predictions, class_to_idx, category_names):
    """convert indeces to named classesself.

    Args:
        predictions ([int]): predicted class indices
        class_to_idx (dict): mapping of indices to classess
        cat_to_name (dict): mapping of numbered classes to class names

    Returns:
        class_names ([str]): list of class names for predictions
                             Returns empty list if category_names are not
                             provided
    """
    class_dict = {val: key for key, val in class_to_idx.items()}
    class_idxs = [class_dict[pred] for pred in predictions]

    if not category_names:
        class_names = class_idxs
    else:
        with open(category_names, 'r') as f:
            cat_to_name = json.load(f)
        class_names = [cat_to_name[idx] for idx in class_idxs]

    return class_names