from typing import Dict
from typing import List
from typing import Union


def _normalize_exception_codes(
    exception_codes: Dict,
    parent_key: List[str] = [],
) -> List[Dict[str, Union[str, List[str]]]]:
    """
    Returns a normalized one-level dictionary of exception attributes and codes. Used to
    standardize multiple exceptions and complex nested exceptions.
    Example:
     => [
         {
             "parsed_keys": ["form", "password"],
             "exception_code": ["required"]
         },
         {
             "parsed_keys": ["form", "password"],
             "exception_code": "min_length"
         }
     ]
    """
    items: List = []
    for key, exception_code in exception_codes.items():

        keys: List[str] = parent_key + [key]

        if isinstance(exception_code, dict):
            items.extend(
                _normalize_exception_codes(
                    exception_code.copy(),
                    keys,
                )
            )
        else:
            items.append({"parsed_keys": keys, "exception_code": exception_code})
    return items