import logging


def apply_mapping(
    transaction_data: dict[str, list], mapping: dict[str, dict[str, str]]
) -> dict[str, dict[str, list]]:
    """
    Create a dictionary of budget_ids mapped to a dictionary of transactions.
    Add an account_id to each transaction.

    :param transaction_data: dictionary of bank names to transaction data
    :type transaction_data: dict[str, list]
    :param mapping: dictionary mapping bank names to budget ID and account ID
    :type mapping: dict[str, dict[str, str]]
    :return: dictionary of budget_id mapped to a dictionary of transactions
    :rtype: dict[str, dict]
    """
    logging.info("Adding budget and account IDs to transactions...")
    budget_transaction_dict: dict[str, dict[str, list]] = dict()

    # get transactions for each bank
    for bank in transaction_data.keys():
        account_transactions = transaction_data[bank]
        budget_id = mapping[bank]["budget_id"]
        account_id = mapping[bank]["account_id"]

        # insert account_id into each transaction
        for transaction in account_transactions:
            transaction["account_id"] = account_id
        # add transaction list into entry for relevant budget
        if budget_id in budget_transaction_dict:
            budget_transaction_dict[budget_id][
                "transactions"
            ] += account_transactions
        else:
            budget_transaction_dict.setdefault(
                budget_id, {"transactions": account_transactions}
            )

    return budget_transaction_dict