from pathlib import Path


def find_suitable_output_name(path: Path) -> Path:
    """Return a valid, currently non-existent path.

    The current function will return the given path as is if it doesn't exist. Otherwise, it
    will append a suffix to it, such as "_0", and check whether it exists again. if it does it
    will continue appending the trailing digit until it finds a path that doesn't exist.
    """
    # if path doesn't exist, just return it as is.
    if not path.exists():
        return path

    # path exists, amend its suffix and check again
    i = 0
    path = path.parent / (str(path.name) + f"_{i}" + path.suffix)
    while path.exists():
        i += 1
        # Use the path until last "_" as the new name
        # this handles both output_name_0, output_name_1, ...
        # AND output_name_10, output_name_100, ...
        new_name = "_".join(str(path.name).split("_")[:-1])
        path = path.parent / (new_name + f"_{i}" + path.suffix)

    return path