from core.database.abc.data_access_object_abc import DataAccessObjectABC

from core.typing import T


async def differential_update_lists(
    new: list[T], old: list[T], dao: DataAccessObjectABC[T]
):
    new_ids = {x.id for x in new}
    old_ids = {x.id for x in old}

    to_delete = [entry for entry in old if entry.id not in new_ids]
    to_update = [entry for entry in new if entry.id in old_ids]

    if len(to_delete) > 0:
        await dao.delete_many(to_delete)

    if len(to_update) > 0:
        await dao.update_many(to_update)
