from typing import List
from typing import Any


def merge_slices_for_single_document(_list: List[List[Any]]) -> List[Any]:
    """
    merges the slices for a single document

    input is
    - either List[List[str]], for tokens & predictions
    - or     List[List[Dict[str, float]]], for predictions with proba = True

    Args:
        _list: predictions for one or more slices that belong to same document,
               e.g. one slice:  [["[CLS]", "this", "is", "one", "slice", "[SEP]"]],
               e.g. two slices: [
                                    ["[CLS]", "this", "is", "one", "slice", "[SEP]"],
                                    ["[CLS]", "and", "a", "second", "one", "[SEP]"]
                                ],

    Returns:
        _list_flat: predictions for one document
               e.g. one slice:  ["[CLS]", "this", "is", "one", "slice", "[SEP]"],
               e.g. two slices: ["[CLS]", "this", "is", "one", "slice", "and", "a", "second", "one", "[SEP]"],
    """
    if len(_list) == 1:  # one slice
        return _list[0]
    else:  # more slices
        _list_flat = list()
        for i, sublist in enumerate(_list):
            if i == 0:
                _list_flat.extend(sublist[:-1])
            elif 0 < i < len(_list) - 1:
                _list_flat.extend(sublist[1:-1])
            else:
                _list_flat.extend(sublist[1:])
        return _list_flat