from typing import Union
from pathlib import Path
import re


def get_decompressed_path(path: Union[Path, str]) -> Path:
    """Get the decompressed path corresponding to a compressed RINEX file after decompression.

    Parameters
    ----------
    path : path or str
        Path to the compressed RINEX file.

    Returns
    -------
    Path
        The path of the resulting RINEX file after decompression.
    """
    path = Path(path)
    parts = path.name.split('.')
    if len(parts) <= 1:
        raise ValueError(f"'{str(path)}' is not a valid RINEX file name")
    if parts[-1].lower() in ['z', 'gz', 'bz2', 'zip']:
        parts.pop()
    suffix = parts[-1]
    if len(parts) == 1:
        return path.parent / (parts[0] + '.rnx')
    elif re.fullmatch(r'\d\dd', suffix):
        suffix = suffix[:2] + 'o'
    elif re.fullmatch(r'\d\dD', suffix):
        suffix = suffix[:2] + 'O'
    elif suffix == 'crx':
        suffix = 'rnx'
    elif suffix == 'CRX':
        suffix = 'RNX'
    out_path = path.parent / '.'.join(parts[:-1] + [suffix])
    return out_path