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

import java.util.regex.Pattern;

import lombok.experimental.UtilityClass;

@UtilityClass
public final class TextNormalizationUtilities {

    public static final Pattern hyphenLineBreaks = Pattern.compile("[-~‐‒⁻−﹣゠⁓‑\\u00AD][\\r\\n]+");
    public static final Pattern linebreaks = Pattern.compile("[\\r\\n]+");
    public static final Pattern doubleWhitespaces = Pattern.compile("\\s{2,}");
    public static final Pattern WHITESPACE_REMOVAL = Pattern.compile("\\s+");


    public String cleanString(String value) {

        String noHyphenLinebreaks = removeHyphenLinebreaks(value);
        String noLinebreaks = removeLinebreaks(noHyphenLinebreaks);
        return removeMultipleWhitespaces(noLinebreaks);
    }


    public String removeHyphenLinebreaks(String value) {

        return hyphenLineBreaks.matcher(value).replaceAll("");
    }


    private String removeMultipleWhitespaces(String value) {

        return doubleWhitespaces.matcher(value).replaceAll(" ");
    }


    private String removeLinebreaks(String value) {

        return linebreaks.matcher(value).replaceAll(" ");
    }


    public String removeAllWhitespaces(String value) {

        return WHITESPACE_REMOVAL.matcher(value).replaceAll("");
    }

}
