from typing import Dict
from typing import List


def update_enrollments(
    tracked_entity: Dict,
    enrollments_with_new_events: List,
) -> List:
    """
    Adds new events to current program enrollments and adds new
    enrollments. Returns a complete list of enrollments.
    """
    current_enrollments = tracked_entity.get("enrollments", [])
    enrollments_by_program_id = {e["program"]: e for e in current_enrollments}
    for enrol in enrollments_with_new_events:
        program_id = enrol["program"]
        if program_id in enrollments_by_program_id:
            enrollments_by_program_id[program_id]["events"].extend(enrol["events"])
            enrollments_by_program_id[program_id]["status"] = enrol["status"]
        else:
            enrollments_by_program_id[program_id] = enrol
    return list(enrollments_by_program_id.values())