from typing import Union


def _prepare_positions(position_start: Union[int, None],
                       position_end: Union[int, None],
                       aln_len: int) -> dict:
    """
    Prepare a dictionary with start and end position for sequence trimming.

    Arguments:
        position_start (int, None): leftmost (5') position; 1-based indexing
        position_end (int, None): rightmost (3') position; 1-based indexing
        aln_len (int): length of the alignment

    Returns:
        (dict): dictionary containing trimming positions
                using 0-based indexing
    """

    if not position_start and not position_end:
        raise ValueError("Neither primers nor sequence positions were "
                         "provided - nothing to be trimmed.")
    if position_start:
        if position_end and position_start >= position_end:
            raise ValueError("Start position should be smaller than end "
                             "position while trimming.")
        elif position_start >= aln_len:
            raise ValueError("Start position should be smaller than "
                             "alignment length.")
        else:
            position_start -= 1
    if position_end and position_end > aln_len:
        position_end = aln_len

    return {'start': position_start, 'end': position_end}