from typing import Optional


def select_bucket_region(
    custom_bucket: Optional[str],
    hook_region: Optional[str],
    cfngin_bucket_region: Optional[str],
    provider_region: str,
) -> str:
    """Return the appropriate region to use when uploading functions.

    Select the appropriate region for the bucket where lambdas are uploaded in.

    Args:
        custom_bucket: The custom bucket name provided by the `bucket` kwarg of
            the aws_lambda hook, if provided.
        hook_region: The contents of the `bucket_region` argument to the hook.
        cfngin_bucket_region: The contents of the ``cfngin_bucket_region`` global
            setting.
        provider_region: The region being used by the provider.

    Returns:
        The appropriate region string.

    """
    region = None
    region = hook_region if custom_bucket else cfngin_bucket_region
    return region or provider_region