package com.knecon.fforesight.service.layoutparser.processor.utils;

import java.util.List;
import java.util.stream.Collectors;

import com.knecon.fforesight.service.layoutparser.processor.model.text.TextPageBlock;
import com.knecon.fforesight.service.layoutparser.processor.model.text.TextPositionSequence;

public class TextPositionOperations {

    private static final TextPositionSequenceComparator comparator = new TextPositionSequenceComparator();


    public static List<TextPositionSequence> mergeAndSortTextPositionSequenceByYThenX(List<TextPageBlock> textBlocks) {

        var sequence = textBlocks.stream().flatMap(tb -> tb.getSequences().stream()).collect(Collectors.toList());

        // because the TextPositionSequenceComparator is not transitive, but
        // JDK7+ enforces transitivity on comparators, we need to use
        // a custom quicksort implementation (which is slower, unfortunately).
        QuickSort.sort(sequence, comparator);
        return sequence;
    }

    public static List<TextPositionSequence> mergeTextPositionSequence(List<TextPageBlock> textBlocks) {

       return textBlocks.stream().flatMap(tb -> tb.getSequences().stream()).collect(Collectors.toList());
    }

}
