from pathlib import Path
import re


def get_compressed_path(path, is_obs=None, compression='gz'):
    """Get the compressed path corresponding to a RINEX file after compression.

    Parameters
    ----------
    path : path or str
        Path to the RINEX file being compressed.
    is_obs : bool, optional
        Whether the RINEX file contains observation data.
        Needed for correct renaming of files with .rnx suffix,
        which will be Hatanaka-compressed if they contain observation data.
    compression : 'gz' (default), 'bz2', 'Z', or 'none'
        Compression (if any) applied in addition to the Hatanaka compression.

    Returns
    -------
    Path
        The path of the resulting RINEX file after compression.
    """
    path = Path(path)
    parts = path.name.split('.')
    if len(parts) < 2:
        raise ValueError(f"'{str(path)}' is not a valid RINEX file name")
    suffix = parts[-1]
    if re.fullmatch(r'\d\do', suffix):
        suffix = suffix[:2] + 'd'
    elif re.fullmatch(r'\d\dO', suffix):
        suffix = suffix[:2] + 'D'
    elif suffix.lower() == 'rnx':
        if is_obs is None:
            raise ValueError(f'whether {path.name} contains observation data is ambiguous, '
                             'need to specify is_obs argument')
        elif is_obs:
            if suffix == 'RNX':
                suffix = 'CRX'
            else:
                suffix = 'crx'
    out_parts = parts[:-1] + [suffix]
    if compression != 'none':
        out_parts.append(compression)
    out_path = path.parent / '.'.join(out_parts)
    return out_path