from typing import Union
from pathlib import Path


def bump_version(path: Union[str, Path]) -> Path:
    """Bumps the version number for a path if it already exists

    Example::

        bump_version("folder/new_file.json") == Path("folder/new_file.json)
        bump_version("folder/old_file.json") == Path("folder/old_file_1.json)
        bump_version("folder/old_file_1.json") == Path("folder/old_file_2.json)
    """
    path = Path(path)
    if not path.exists():
        return path

    # Check for already bumped versions
    prev_version = None
    try:
        prev_version = max(
            map(
                int,
                filter(
                    lambda s: s.isdigit(),
                    [f.stem.split("_")[-1] for f in path.parent.glob(f"{path.stem}*")],
                ),
            )
        )
        new_version = prev_version + 1
    except ValueError:  # max() arg is an empty sequence
        new_version = 1

    if prev_version and path.stem.endswith(f"_{prev_version}"):
        suffix = f"_{prev_version}"
        new_name = f"{path.stem[:-len(suffix)]}_{new_version}{path.suffix}"
    else:
        new_name = f"{path.stem}_{new_version}{path.suffix}"
    return path.parent / new_name