import copy


def sanitize_slicing(slice_across, slice_relative_position):
    """
    Return standardized format for `slice_across` and `slice_relative_position`:
    - either `slice_across` and `slice_relative_position` are both `None` (no slicing)
    - or `slice_across` and `slice_relative_position` are both lists,
    with the same number of elements

    Parameters
    ----------
    slice_relative_position : float, or list of float, or None

    slice_across : str, or list of str, or None
       Direction(s) across which the data should be sliced
    """
    # Skip None and empty lists
    if slice_across is None or slice_across == []:
        return None, None

    # Convert to lists
    if not isinstance(slice_across, list):
        slice_across = [slice_across]
    if slice_relative_position is None:
        slice_relative_position = [0]*len(slice_across)
    if not isinstance(slice_relative_position, list):
        slice_relative_position = [slice_relative_position]
    # Check that the length are matching
    if len(slice_across) != len(slice_relative_position):
        raise ValueError(
            'The argument `slice_relative_position` is erroneous: \nIt should have'
            'the same number of elements as `slice_across`.')

    # Return a copy. This is because the rest of the `openPMD-viewer` code
    # sometimes modifies the objects returned by `sanitize_slicing`.
    # Using a copy avoids directly modifying objects that the user may pass
    # to this function (and live outside of openPMD-viewer, e.g. directly in
    # a user's notebook)
    return copy.copy(slice_across), copy.copy(slice_relative_position)