# AUTOGENERATED - modify shared_anyscale_util in root directory to make changes
from typing import Union


def ray_semver_compare(a: str, b: str) -> int:
    """
    A dumb implementation of semver comparison because ray versions
    don't actually follow the semver standard.

    The standard for semver versions is "{MAJOR}.{MINOR}.{PATCH}-{PRE_RELEASE_VERSION}," such as 2.7.0 or 2.7.0-oss for Ray versions.
    However, since we use hyphens in the codebase to split and extract information, our standard for Ray versions is "{MAJOR}.{MINOR}.{PATCH}{PRE_RELEASE_VERSION}," like 2.7.0oss without "-" between the patch and the pre-release version.
    Therefore, when comparing Ray versions, we treat "ray2.7.0" as equivalent to "ray2.7.0oss."

    Example:
        ray_semver_compare("ray2.7.0", "ray2.7.0optimized") == 0
        ray_semver_compare("ray2.0.1rc1", "ray2.0.1rc2") == 0
    """
    a_parts = a.split(".")
    b_parts = b.split(".")
    for i, a_part in enumerate(a_parts):
        if i > len(b_parts):
            # No value in part is always less than any string in a part
            return 1

        try:
            parsed_a_part: Union[str, int] = int(a_part)
            parsed_b_part: Union[str, int] = int(b_parts[i])
        except ValueError:
            parsed_a_part = a_part
            parsed_b_part = b_parts[i]

        if parsed_a_part > parsed_b_part:  # type: ignore
            return 1
        elif parsed_a_part < parsed_b_part:  # type: ignore
            return -1
    if len(b_parts) > len(a_parts):
        # B still has parts, it must be bigger
        return -1

    return 0


SUFFIXES = ("optimized", "oss", "nightly")


def get_byod_ray_version_alias(ray_version: str) -> str:
    """
    Get a modified version alias for the Ray version.

    Args:
        ray_version (str): A string representing the Ray framework version (e.g., "2.7.0").

    Returns:
        str: The modified Ray framework version with the "optimized" suffix, if applicable.
    """

    if ray_version.endswith(SUFFIXES) or "rc" in ray_version:
        return ray_version

    if ray_semver_compare(ray_version, "2.7.0") != -1:  # Ray version >= 2.7.0
        return ray_version.replace(ray_version, f"{ray_version}optimized")

    return ray_version
