/*
 * SDK Pullenti Unitext, version 4.33, fabruary 2026. Copyright (c) 2013-2026, Pullenti. All rights reserved. 
 * Non-Commercial Freeware and Commercial Software. 
 * This class is generated using the converter Unisharping (www.unisharping.ru) from Pullenti C# project. 
 * The latest version of the code is available on the site www.pullenti.ru
 */

package com.pullenti.unitext.internal.uni;

public class UnitextCorrHelper {

    public static void removePageBreakNumeration(com.pullenti.unitext.UnitextDocument doc) {
        com.pullenti.unitext.UnitextContainer cnt = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(doc.content, com.pullenti.unitext.UnitextContainer.class);
        if (cnt == null) 
            return;
        StringBuilder tmp = new StringBuilder();
        com.pullenti.unitext.UnitextContainer tmpCnt = new com.pullenti.unitext.UnitextContainer();
        boolean corr = false;
        for (int k = 0; k < 2; k++) {
            java.util.ArrayList<CorrPageText> pages = new java.util.ArrayList<CorrPageText>();
            for (int i = 0; i < cnt.children.size(); i++) {
                int i0;
                int i1;
                tmpCnt.children.clear();
                if (k == 0) {
                    if (i == 0) {
                    }
                    else if ((cnt.children.get(i - 1) instanceof com.pullenti.unitext.UnitextPagebreak) && (cnt.children.get(i) instanceof com.pullenti.unitext.UnitextPlaintext)) {
                    }
                    else 
                        continue;
                    i0 = i;
                    for (i1 = i0; i1 < cnt.children.size(); i1++) {
                        com.pullenti.unitext.UnitextItem chh = cnt.children.get(i1);
                        if (chh instanceof com.pullenti.unitext.UnitextPlaintext) {
                        }
                        else if (chh instanceof com.pullenti.unitext.UnitextHyperlink) {
                        }
                        else 
                            break;
                        tmpCnt.children.add(chh);
                    }
                    i1--;
                }
                else {
                    if (i == (cnt.children.size() - 1)) 
                        i1 = i;
                    else if (cnt.children.get(i) instanceof com.pullenti.unitext.UnitextPagebreak) 
                        i1 = i - 1;
                    else 
                        continue;
                    for (i0 = i1; i0 >= 0; i0--) {
                        com.pullenti.unitext.UnitextItem chh = cnt.children.get(i0);
                        if (chh instanceof com.pullenti.unitext.UnitextPlaintext) {
                        }
                        else if (chh instanceof com.pullenti.unitext.UnitextHyperlink) {
                        }
                        else 
                            break;
                        tmpCnt.children.add(chh);
                    }
                    i0++;
                }
                if (tmpCnt.children.size() == 0) 
                    continue;
                tmp.setLength(0);
                tmpCnt.getPlaintext(tmp, com.pullenti.unitext.GetPlaintextParam._new357(false));
                if (tmp.length() == 0) 
                    continue;
                CorrPageText page = CorrPageText._new358(tmp.toString(), k == 0, i0, i1);
                pages.add(page);
            }
            if (pages.size() < 2) 
                continue;
            String tittext = CorrPageText.process(pages);
            int k1 = k;
            for (int i = pages.size() - 1; i >= 0; i--) {
                if (pages.get(i).corr) {
                    if (i == 0) {
                    }
                    for (int j = pages.get(i).i0; j <= pages.get(i).i1; j++) {
                        com.pullenti.unitext.UnitextPlaintext pl = (com.pullenti.unitext.UnitextPlaintext)com.pullenti.unisharp.Utils.cast(cnt.children.get(j), com.pullenti.unitext.UnitextPlaintext.class);
                        if (pl != null && pl.layout != null) {
                            for (int jj = 0; jj < pl.layout.length; jj++) {
                                if (pl.layout[jj] != null) 
                                    pl.layout[jj].ignored = true;
                            }
                        }
                    }
                    for(int jjj = pages.get(i).i0 + (pages.get(i).i1 + 1) - pages.get(i).i0 - 1, mmm = pages.get(i).i0; jjj >= mmm; jjj--) cnt.children.remove(jjj);
                    corr = true;
                    k1 = k - 1;
                }
            }
            if (tittext != null) {
            }
            k = k1;
        }
        for (int i = cnt.children.size() - 1; i >= 0; i--) {
            if (i >= cnt.children.size()) 
                continue;
            com.pullenti.unitext.UnitextPlaintext pt = (com.pullenti.unitext.UnitextPlaintext)com.pullenti.unisharp.Utils.cast(cnt.children.get(i), com.pullenti.unitext.UnitextPlaintext.class);
            if (pt == null) 
                continue;
            int n;
            com.pullenti.unisharp.Outargwrapper<Integer> wrapn359 = new com.pullenti.unisharp.Outargwrapper<Integer>();
            boolean inoutres360 = com.pullenti.unisharp.Utils.parseInteger(pt.text.trim(), 0, null, wrapn359);
            n = (wrapn359.value != null ? wrapn359.value : 0);
            if (!inoutres360) 
                continue;
            if (n > 3000) 
                continue;
            if (i == 0 || (cnt.children.get(i - 1) instanceof com.pullenti.unitext.UnitextPagebreak) || ((i > 1 && (cnt.children.get(i - 1) instanceof com.pullenti.unitext.UnitextNewline) && (cnt.children.get(i - 2) instanceof com.pullenti.unitext.UnitextPagebreak)))) {
                if (((i + 1) < cnt.children.size()) && (cnt.children.get(i + 1) instanceof com.pullenti.unitext.UnitextNewline)) {
                    if (pt.layout != null) {
                        for (com.pullenti.unitext.UnilayRectangle la : pt.layout) {
                            if (la != null) {
                                la.ignored = true;
                                la.page.topNumber = n;
                            }
                        }
                    }
                    for(int jjj = i + 2 - 1, mmm = i; jjj >= mmm; jjj--) cnt.children.remove(jjj);
                    continue;
                }
            }
            else if ((i + 1) == cnt.children.size() || (cnt.children.get(i + 1) instanceof com.pullenti.unitext.UnitextPagebreak) || ((((i + 2) < cnt.children.size()) && (cnt.children.get(i + 1) instanceof com.pullenti.unitext.UnitextNewline) && (cnt.children.get(i + 2) instanceof com.pullenti.unitext.UnitextPagebreak)))) {
                if (i > 0 && (cnt.children.get(i - 1) instanceof com.pullenti.unitext.UnitextNewline)) {
                    if (pt.layout != null) {
                        for (com.pullenti.unitext.UnilayRectangle la : pt.layout) {
                            if (la != null) {
                                la.ignored = true;
                                la.page.bottomNumber = n;
                            }
                        }
                    }
                    for(int jjj = i - 1 + 2 - 1, mmm = i - 1; jjj >= mmm; jjj--) cnt.children.remove(jjj);
                    continue;
                }
            }
        }
        if (corr) 
            doc.optimize(false, null);
    }

    private static final int minLineLen = 50;

    private static final int maxLineLen = 100;

    public static void removeFalseNewLines(com.pullenti.unitext.UnitextDocument doc, boolean replaceNbsp, com.pullenti.unitext.CorrectDocumentParam pars) {
        if (doc == null || doc.content == null) 
            return;
        com.pullenti.unitext.UnitextContainer cnt = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(doc.content, com.pullenti.unitext.UnitextContainer.class);
        if (cnt == null) 
            return;
        int cr1 = 0;
        int cr2 = 0;
        int crA = 0;
        for (com.pullenti.unitext.UnitextItem v : cnt.children) {
            if (v instanceof com.pullenti.unitext.UnitextNewline) {
                int co = ((com.pullenti.unitext.UnitextNewline)com.pullenti.unisharp.Utils.cast(v, com.pullenti.unitext.UnitextNewline.class)).count;
                if (co == 1) 
                    cr1++;
                else if (co == 2) 
                    cr2++;
                else 
                    crA++;
            }
        }
        if (cr2 > (((cr1 + crA)) * 3) && cr2 > 2) {
            if (doc.sourceFormat == com.pullenti.unitext.FileFormat.DOC || doc.sourceFormat == com.pullenti.unitext.FileFormat.DOCX || doc.sourceFormat == com.pullenti.unitext.FileFormat.DOCXML) {
            }
            else 
                cnt.correct(LocCorrTyp.MERGENEWLINES, null);
        }
        java.util.ArrayList<CorrLine> lines = CorrLine.parseList(cnt);
        int cou = 0;
        int len = 0;
        int nbspCount = 0;
        int spCount = 0;
        for (CorrLine l : lines) {
            if (l.isPureText) {
                for (char ch : l.getText().toCharArray()) {
                    if (ch == ' ') 
                        spCount++;
                    else if (ch == ((char)0xA0)) 
                        nbspCount++;
                }
            }
            if ((l.isPureText && l.newLines == 1) || l.pageBreakAfter) {
                cou++;
                len += l.length;
            }
        }
        if (replaceNbsp && nbspCount > 100 && nbspCount > (spCount * 2)) {
            doc.content.correct(LocCorrTyp.NPSP2SP, null);
            for (com.pullenti.unitext.UnitextPagesection s : doc.sections) {
                for (com.pullenti.unitext.UnitextPagesectionItem it : s.items) {
                    if (it.content != null) 
                        it.content.correct(LocCorrTyp.NPSP2SP, null);
                }
            }
        }
        if (cou == 0) 
            return;
        len /= cou;
        if (cou < 3) 
            return;
        if ((len < minLineLen) || len > maxLineLen) 
            return;
        int minLen = (int)((0.80 * ((double)len)));
        int maxLen = (int)((1.20 * ((double)len)));
        for (int i = 0; i < (lines.size() - 1); i++) {
            CorrLine l1 = lines.get(i);
            CorrLine l2 = lines.get(i + 1);
            if (!l1.isPureText || !l2.isPureText) 
                continue;
            if (l1.length < minLen) 
                continue;
            if (l1.canFollowed(l2)) 
                l2.merge = true;
        }
        boolean changed = false;
        for (int i = lines.size() - 1; i > 0; i--) {
            if (lines.get(i).merge) {
                if (lines.get(i - 1).pageBreakAfter) 
                    continue;
                int i0 = lines.get(i - 1).lastInd + 1;
                int i1 = lines.get(i).firstInd - 1;
                if (i0 <= i1) {
                    changed = true;
                    if (i0 < i1) 
                        for(int jjj = i0 + 1 + i1 - i0 - 1, mmm = i0 + 1; jjj >= mmm; jjj--) cnt.children.remove(jjj);
                    com.pullenti.unisharp.Utils.putArrayValue(cnt.children, i0, com.pullenti.unitext.UnitextPlaintext._new57(" "));
                }
            }
        }
        if (changed) {
            if (pars != null) 
                doc.optimize(false, com.pullenti.unitext.CreateDocumentParam._new362(pars));
            else 
                doc.optimize(false, null);
        }
    }

    public static void restoreTextFootnotes(com.pullenti.unitext.UnitextDocument doc) {
        if (doc == null || doc.content == null) 
            return;
        if (doc.content instanceof com.pullenti.unitext.UnitextTable) {
            restoreTextFootnotesInTable((com.pullenti.unitext.UnitextTable)com.pullenti.unisharp.Utils.cast(doc.content, com.pullenti.unitext.UnitextTable.class));
            return;
        }
        com.pullenti.unitext.UnitextContainer cnt = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(doc.content, com.pullenti.unitext.UnitextContainer.class);
        if (cnt == null) 
            return;
        if (doc.sourceFormat == com.pullenti.unitext.FileFormat.PDF || doc.sourceFormat == com.pullenti.unitext.FileFormat.DJVU) {
            UnitextCorrPdfPage.correct(cnt);
            return;
        }
        java.util.ArrayList<CorrFootnoteContainer> fcl = new java.util.ArrayList<CorrFootnoteContainer>();
        int numStart = 1;
        for (int i = 0; i < cnt.children.size(); i++) {
            if (i == 149) {
            }
            if (cnt.children.get(i) instanceof com.pullenti.unitext.UnitextTable) {
                restoreTextFootnotesInTable((com.pullenti.unitext.UnitextTable)com.pullenti.unisharp.Utils.cast(cnt.children.get(i), com.pullenti.unitext.UnitextTable.class));
                continue;
            }
            CorrFootnoteContainer fc = CorrFootnoteContainer.tryParse(cnt.children, i, numStart);
            if (fc == null) 
                continue;
            fcl.add(fc);
            if (fcl.size() == 19) {
            }
            i = fc.endInd;
            if (fc.typ == CorrFootnoteTyps.DIGIT || fc.typ == CorrFootnoteTyps.SUPDIGIT) 
                numStart = fc.startNumber + fc.items.size();
            else 
                numStart += fc.items.size();
        }
        if (fcl.size() == 0) 
            return;
        boolean isChanged = false;
        for (int i = fcl.size() - 1; i >= 0; i--) {
            int ind0 = 0;
            int ind1 = fcl.get(i).beginInd - 1;
            if (i > 0) 
                ind0 = fcl.get(i - 1).endInd + 1;
            if (i == 8) {
            }
            boolean corr = false;
            for (int k = 0; k < 2; k++) {
                for (int j = ind0; j <= ind1; j++) {
                    if (j == 40) {
                    }
                    com.pullenti.unitext.UnitextItem it = fcl.get(i).correctItem(cnt.children.get(j), k == 1);
                    if (it != null) {
                        com.pullenti.unisharp.Utils.putArrayValue(cnt.children, j, it);
                        corr = true;
                    }
                }
                if (corr || fcl.get(i).typ != CorrFootnoteTyps.STARS) 
                    break;
            }
            if (corr) {
                isChanged = true;
                for(int jjj = fcl.get(i).beginInd + (fcl.get(i).endInd + 1) - fcl.get(i).beginInd - 1, mmm = fcl.get(i).beginInd; jjj >= mmm; jjj--) cnt.children.remove(jjj);
                if (((fcl.get(i).beginInd < cnt.children.size()) && (cnt.children.get(fcl.get(i).beginInd) instanceof com.pullenti.unitext.UnitextNewline) && fcl.get(i).beginInd > 0) && (cnt.children.get(fcl.get(i).beginInd - 1) instanceof com.pullenti.unitext.UnitextNewline)) 
                    cnt.children.remove(fcl.get(i).beginInd);
            }
        }
        if (isChanged) 
            doc.optimize(false, null);
    }

    private static void restoreTextFootnotesInTable(com.pullenti.unitext.UnitextTable tab) {
        boolean isChanged = false;
        for (int r = 0; r < tab.getRowsCount(); r++) {
            for (int c = 0; c < tab.getColsCount(); c++) {
                com.pullenti.unitext.UnitextTablecell cel = tab.getCell(r, c);
                if (cel == null) 
                    continue;
                if (cel.getColBegin() != c || cel.getRowBegin() != r) 
                    continue;
                if (cel.content == null) 
                    continue;
                CorrFootnoteContainer fn = CorrFootnoteContainer.tryParse1(cel.content);
                if (fn == null) 
                    continue;
                for (int cc = cel.getColBegin(); cc <= cel.getColEnd(); cc++) {
                    com.pullenti.unitext.UnitextTablecell cel0 = tab.getCell(r - 1, cc);
                    if (cel0 == null) 
                        continue;
                    if (cel0.content == null) 
                        continue;
                    com.pullenti.unitext.UnitextItem re = fn.correctItem(cel0.content, false);
                    if (re == null) 
                        continue;
                    cel0.content = re;
                    cel.content = null;
                    isChanged = true;
                }
            }
        }
        if (isChanged) 
            tab.optimize(false, null);
    }

    public static boolean restoreTables(com.pullenti.unitext.UnitextDocument doc) {
        com.pullenti.unitext.UnitextContainer cnt = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(doc.content, com.pullenti.unitext.UnitextContainer.class);
        if (cnt != null) 
            return _restoreTables(cnt);
        return false;
    }

    private static boolean _restoreTables(com.pullenti.unitext.UnitextContainer cnt) {
        if (cnt == null) 
            return false;
        boolean ret = false;
        for (com.pullenti.unitext.UnitextItem ch : cnt.children) {
            if (ch instanceof com.pullenti.unitext.UnitextContainer) {
                if (_restoreTables((com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(ch, com.pullenti.unitext.UnitextContainer.class))) 
                    ret = true;
            }
        }
        java.util.ArrayList<CorrLine> lines = CorrLine.parseList(cnt);
        java.util.ArrayList<CorrTable> tabs = new java.util.ArrayList<CorrTable>();
        for (int i = 0; i < lines.size(); i++) {
            int ii;
            com.pullenti.unisharp.Outargwrapper<Integer> wrapii363 = new com.pullenti.unisharp.Outargwrapper<Integer>();
            CorrTable tab = CorrTable.tryParse(lines, i, wrapii363);
            ii = (wrapii363.value != null ? wrapii363.value : 0);
            if (tab != null) {
                tabs.add(tab);
                i = tab.endInd;
            }
            else 
                i = ii;
        }
        if (tabs.size() == 0) 
            return ret;
        for (int i = tabs.size() - 1; i >= 0; i--) {
            int i0 = lines.get(tabs.get(i).beginInd).firstInd;
            int i1 = lines.get(tabs.get(i).endInd).lastInd;
            if (i0 < i1) {
                for(int jjj = i0 + 1 + i1 - i0 - 1, mmm = i0 + 1; jjj >= mmm; jjj--) cnt.children.remove(jjj);
                com.pullenti.unisharp.Utils.putArrayValue(cnt.children, i0, tabs.get(i).result);
            }
        }
        if (cnt.typ == com.pullenti.unitext.UnitextContainerType.MONOSPACE) 
            cnt.typ = com.pullenti.unitext.UnitextContainerType.UNDEFINED;
        return true;
    }

    public static String corrNbsp(String text) {
        if (text.indexOf((char)0xA0) < 0) 
            return text;
        return text.replace((char)0xA0, ' ');
    }
    public UnitextCorrHelper() {
    }
}
