from __future__ import annotations

from typing import Union, Optional, cast

RedactOption = Union[bool, list[str]]


def normalize_redact_option(redact_option: Optional[RedactOption]) -> RedactOption:
    """Normalize optional redaction input into canonical internal representation."""
    if redact_option is None:
        return False

    if isinstance(redact_option, bool):
        return redact_option

    return redact_option


def combine_redact_options(
    a: Optional[RedactOption], b: Optional[RedactOption]
) -> RedactOption:
    """Combine two redaction options into a single effective option."""
    a = normalize_redact_option(a)
    b = normalize_redact_option(b)

    if is_redact_option_falsy(a):
        return b
    if is_redact_option_falsy(b):
        return a

    if a is True or b is True:
        return True

    return cast(list[str], a) + cast(list[str], b)


def is_redact_option_falsy(redact: RedactOption | None) -> bool:
    """Return ``True`` when a redaction option semantically means no redaction."""
    return redact in [None, False, []]
