from typing import Any, Iterator

from aws_lambda_powertools.utilities.data_classes.ses_event import SESMailHeader


def get_header_value(
    headers: Iterator[SESMailHeader],
    header_name: str,
    *,
    default: Any = None,
    raise_on_missing: bool = True,
) -> str:
    for header in headers:
        if header.name.lower() == header_name:
            return header.value

    if raise_on_missing:
        raise ValueError(f'{header_name} not found.')

    return default
