import copy


def flat_ingredients_list_DFS(product):
    """
    Recursive function to search the ingredients graph by doing a Depth First Search and return it as a flat list of
    all nodes.
    Sub ingredients are placed right after their parents.

    Args:
        product (dict): Dict corresponding to a product or a compound ingredient.

    Returns:
        list: List containing all the ingredients graph nodes.
    """
    if 'ingredients' in product:
        product_without_ingredients = copy.deepcopy(product)
        del product_without_ingredients['ingredients']

        if '_id' in product:  # It is a product and not a compound ingredient:
            return [y for x in product['ingredients'] for y in flat_ingredients_list_DFS(x)]
        else:
            return [product_without_ingredients] + [y for x in product['ingredients'] for y in
                                                    flat_ingredients_list_DFS(x)]
    else:
        return [product]