from typing import List
from typing import Union
from typing import Optional
from typing import Match
import re


def find_config_value(
        config_list: List[str],
        search_term: str
) -> Union[List, str]:
    """
    Find a config line in a block of config

    Arguments:
        config_list (List[str]):
            All config lines relating to a single vrrp group
        search_term (str):
            The key to look for in the config

    Return:
        Returns either a value for the search string being found
        or raises an error if the value isn't found an Enum that
        can take one of three formats.
        ValueError("Config not found")
        Present but not configured = [None]
        Present and configured = <found string>

    Example:
        config_block = ["vrrp_instance dp0p1s1", "priority 200",
            "use_vmac"]

        _find_config_value(config_block, "preempt")
        # raise ValueError for caller to deal with

        _find_config_value(config_block, "use_vmac")
        # [None]

        _find_config_value(config_block, "priority")
        # "200"
    """

    line: str
    for line in config_list:
        regex_search: Optional[Match[str]] = \
            re.match(fr"^{search_term}(\s+|$|:)", line)
        if regex_search is not None:
            regex_search = re.match(fr"{search_term}\s+(.*)", line)
            if regex_search is not None:
                return regex_search.group(1)
            # Yang JSON representation has single key with no value as
            # <key>: [null]
            return [None]
    raise ValueError(f"Value {search_term} not found in config")