from typing import Dict
from typing import List
from typing import Tuple
from typing import Optional


def deconstruct_entry(entry: Dict[str, str],
                      username_fields: List[str],
                      sightings_fields: List[str]) -> Tuple[Optional[str],
                                                            Optional[str],
                                                            Optional[int]]:
    """
    deconstruct_entry
    Extracts device relevant fields from a log entry.

    :type entry: ``Dict[str, str]``
    :param entry: Log entry as dictionary of fields.

    :type sightings_fields: ``List[str]``
    :param sightings_fields: List of possible field names in log entry to be considered as number of occurences.

    :type username_fields: ``List[str]``
    :param username_fields: List of possible field names in log entry to be considered as username.

    :return: Tuple where the first element is the username or None, the second is the domain extracted from the
        username field and the third element is the number of occurences of the event.
    :rtype: ``Tuple[Optional[str], Optional[str], Optional[int]]``
    """
    username = next((entry[field] for field in username_fields if field in entry), None)
    sightings = next((int(entry[field]) for field in sightings_fields if field in entry), 1)

    domain = None
    if username is not None and "\\" in username:
        domain, username = username.split("\\", 1)

    return username, domain, sightings