/*
 * 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 CorrFootnoteContainer {

    public CorrFootnoteTyps typ = CorrFootnoteTyps.DIGIT;

    public int startNumber = 1;

    public int beginInd;

    public int endInd;

    public java.util.ArrayList<String> items = new java.util.ArrayList<String>();

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < items.size(); i++) {
            res.append("<").append((typ == CorrFootnoteTyps.DIGIT || typ == CorrFootnoteTyps.SUPDIGIT ? ((Integer)(startNumber + i)).toString() : "*")).append(">").append(items.get(i)).append(" \r\n");
        }
        return res.toString();
    }

    public static CorrFootnoteContainer tryParse1(com.pullenti.unitext.UnitextItem it) {
        if (it instanceof com.pullenti.unitext.UnitextPlaintext) {
            com.pullenti.unitext.UnitextPlaintext pl = (com.pullenti.unitext.UnitextPlaintext)com.pullenti.unisharp.Utils.cast(it, com.pullenti.unitext.UnitextPlaintext.class);
            int i = 0;
            com.pullenti.unisharp.Outargwrapper<Integer> wrapi310 = new com.pullenti.unisharp.Outargwrapper<Integer>(i);
            CorrFootnoteTag tag = CorrFootnoteTag.tryParse(pl.text, wrapi310, true, false);
            i = (wrapi310.value != null ? wrapi310.value : 0);
            if (tag == null || tag.number != 1) 
                return null;
            CorrFootnoteContainer res = _new308(tag.typ);
            String txt = pl.text.substring(i + 1).trim();
            for (i = 0; i < txt.length(); i++) {
                int j = i;
                com.pullenti.unisharp.Outargwrapper<Integer> wrapj309 = new com.pullenti.unisharp.Outargwrapper<Integer>(j);
                CorrFootnoteTag tag1 = CorrFootnoteTag.tryParse(txt, wrapj309, true, false);
                j = (wrapj309.value != null ? wrapj309.value : 0);
                if (tag1 == null) 
                    continue;
                if (tag1.typ != tag.typ) 
                    continue;
                if (tag1.number != (res.items.size() + 2)) 
                    continue;
                if (i > 0) 
                    res.items.add(txt.substring(0, 0 + i).trim());
                txt = txt.substring(j + 1).trim();
                i = -1;
            }
            if (!com.pullenti.unisharp.Utils.isNullOrEmpty(txt)) 
                res.items.add(txt);
            return res;
        }
        if (it instanceof com.pullenti.unitext.UnitextContainer) {
            com.pullenti.unitext.UnitextContainer cnt = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(it, com.pullenti.unitext.UnitextContainer.class);
            CorrFootnoteContainer res = tryParse(cnt.children, 0, -1);
            if (res == null) 
                return null;
            if (res.endInd != (cnt.children.size() - 1)) 
                return null;
            return res;
        }
        return null;
    }

    public static CorrFootnoteContainer tryParse(java.util.ArrayList<com.pullenti.unitext.UnitextItem> _items, int ind, int numStart) {
        if ((ind < 1) || (ind + 1) > _items.size()) 
            return null;
        if (!(_items.get(ind - 1) instanceof com.pullenti.unitext.UnitextNewline) && !(_items.get(ind - 1) instanceof com.pullenti.unitext.UnitextPagebreak)) 
            return null;
        int ind0 = ind;
        com.pullenti.unitext.UnitextPlaintext it = (com.pullenti.unitext.UnitextPlaintext)com.pullenti.unisharp.Utils.cast(_items.get(ind), com.pullenti.unitext.UnitextPlaintext.class);
        if (it == null || com.pullenti.unisharp.Utils.isNullOrEmpty(it.text)) 
            return null;
        if (com.pullenti.unisharp.Utils.isNullOrEmpty(it.text.trim()) && ((ind + 1) < _items.size()) && (_items.get(ind + 1) instanceof com.pullenti.unitext.UnitextPlaintext)) {
            ind++;
            it = (com.pullenti.unitext.UnitextPlaintext)com.pullenti.unisharp.Utils.cast(_items.get(ind), com.pullenti.unitext.UnitextPlaintext.class);
        }
        int i = 0;
        com.pullenti.unisharp.Outargwrapper<Integer> wrapi324 = new com.pullenti.unisharp.Outargwrapper<Integer>(i);
        CorrFootnoteTag tag = CorrFootnoteTag.tryParse(it.text, wrapi324, true, false);
        i = (wrapi324.value != null ? wrapi324.value : 0);
        if (tag == null) {
            int nn;
            com.pullenti.unisharp.Outargwrapper<Integer> wrapnn312 = new com.pullenti.unisharp.Outargwrapper<Integer>();
            boolean inoutres313 = com.pullenti.unisharp.Utils.parseInteger(it.text.trim(), 0, null, wrapnn312);
            nn = (wrapnn312.value != null ? wrapnn312.value : 0);
            if (it.typ == com.pullenti.unitext.UnitextPlaintextType.SUP && inoutres313) {
                tag = CorrFootnoteTag._new311(nn, CorrFootnoteTyps.SUPDIGIT);
                if (ind != ind0) {
                    _items.remove(ind0);
                    ind = ind0;
                }
            }
            else 
                return null;
        }
        else if (tag.typ == CorrFootnoteTyps.VERYDOUBT) {
            if (tag.number == 24) {
            }
            boolean ok = false;
            boolean hasLines = false;
            for (int j = ind - 1; j >= 0 && (j > (ind - 10)); j--) {
                com.pullenti.unitext.UnitextItem itt = _items.get(j);
                if (itt.isWhitespaces()) 
                    continue;
                String ttt = itt.getPlaintextString(null);
                if (com.pullenti.unisharp.Utils.isNullOrEmpty(ttt)) 
                    continue;
                if (ttt.startsWith("-----")) {
                    hasLines = true;
                    continue;
                }
                if (!hasLines) 
                    break;
                for (int ii = 0; ii < ttt.length(); ii++) {
                    com.pullenti.unisharp.Outargwrapper<Integer> wrapii314 = new com.pullenti.unisharp.Outargwrapper<Integer>(ii);
                    CorrFootnoteTag tag0 = CorrFootnoteTag.tryParse(ttt, wrapii314, false, false);
                    ii = (wrapii314.value != null ? wrapii314.value : 0);
                    if (tag0 != null) {
                        if (tag0.number == tag.number && tag0.typ == CorrFootnoteTyps.DIGIT) {
                            ok = true;
                            break;
                        }
                    }
                }
            }
            if (!ok) 
                return null;
            tag.typ = CorrFootnoteTyps.DIGIT;
        }
        CorrFootnoteContainer res = _new308(tag.typ);
        res.beginInd = ind0;
        res.endInd = ind;
        if (tag.typ == CorrFootnoteTyps.DIGIT || tag.typ == CorrFootnoteTyps.SUPDIGIT) 
            res.startNumber = tag.number;
        else if (tag.typ == CorrFootnoteTyps.STARS && tag.number > 1) 
            res.startNumber = tag.number;
        for (--ind; ind < _items.size(); ind++) {
            if ((_items.get(ind) instanceof com.pullenti.unitext.UnitextNewline) || (_items.get(ind) instanceof com.pullenti.unitext.UnitextPagebreak)) 
                continue;
            if (!(_items.get(ind - 1) instanceof com.pullenti.unitext.UnitextNewline) && !(_items.get(ind - 1) instanceof com.pullenti.unitext.UnitextPagebreak)) 
                break;
            it = (com.pullenti.unitext.UnitextPlaintext)com.pullenti.unisharp.Utils.cast(_items.get(ind), com.pullenti.unitext.UnitextPlaintext.class);
            if (it == null) 
                break;
            i = 0;
            com.pullenti.unisharp.Outargwrapper<Integer> wrapi323 = new com.pullenti.unisharp.Outargwrapper<Integer>(i);
            CorrFootnoteTag tag1 = CorrFootnoteTag.tryParse(it.text, wrapi323, true, false);
            i = (wrapi323.value != null ? wrapi323.value : 0);
            if (tag1 == null) {
                int nn;
                com.pullenti.unisharp.Outargwrapper<Integer> wrapnn317 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                boolean inoutres318 = com.pullenti.unisharp.Utils.parseInteger(it.text.trim(), 0, null, wrapnn317);
                nn = (wrapnn317.value != null ? wrapnn317.value : 0);
                if (it.typ == com.pullenti.unitext.UnitextPlaintextType.SUP && inoutres318) 
                    tag1 = CorrFootnoteTag._new311(nn, CorrFootnoteTyps.SUPDIGIT);
            }
            if (tag1 == null) 
                break;
            if (tag1.typ == CorrFootnoteTyps.VERYDOUBT) {
                if (ind == res.endInd) 
                    tag1.typ = CorrFootnoteTyps.DIGIT;
                else 
                    break;
            }
            if (tag.typ != tag1.typ) 
                break;
            if ((tag1.number - res.startNumber) != res.items.size()) 
                break;
            res.endInd = ind;
            String txt = (tag.typ == CorrFootnoteTyps.SUPDIGIT ? "" : it.text.substring(i + 1).trim());
            for (int jj = ind + 1; jj < _items.size(); jj++) {
                if (_items.get(jj) instanceof com.pullenti.unitext.UnitextNewline) {
                    if (((com.pullenti.unitext.UnitextNewline)com.pullenti.unisharp.Utils.cast(_items.get(jj), com.pullenti.unitext.UnitextNewline.class)).count > 1) 
                        break;
                    continue;
                }
                if (_items.get(jj) instanceof com.pullenti.unitext.UnitextComment) 
                    continue;
                String txt1 = null;
                com.pullenti.unitext.UnitextPlaintext pl = (com.pullenti.unitext.UnitextPlaintext)com.pullenti.unisharp.Utils.cast(_items.get(jj), com.pullenti.unitext.UnitextPlaintext.class);
                if (pl != null) 
                    txt1 = pl.text;
                else if (_items.get(jj) instanceof com.pullenti.unitext.UnitextHyperlink) 
                    txt1 = _items.get(jj).getPlaintextString(null);
                if (txt1 == null) 
                    break;
                if (com.pullenti.unisharp.Utils.startsWithString(txt1, "Текст ", true)) 
                    break;
                i = 0;
                com.pullenti.unisharp.Outargwrapper<Integer> wrapi321 = new com.pullenti.unisharp.Outargwrapper<Integer>(i);
                CorrFootnoteTag inoutres322 = CorrFootnoteTag.tryParse(txt1, wrapi321, true, false);
                i = (wrapi321.value != null ? wrapi321.value : 0);
                if (inoutres322 != null) 
                    break;
                com.pullenti.unisharp.Outargwrapper<Integer> wrapi319 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                boolean inoutres320 = com.pullenti.unisharp.Utils.parseInteger(txt1.trim(), 0, null, wrapi319);
                i = (wrapi319.value != null ? wrapi319.value : 0);
                if (it.typ == com.pullenti.unitext.UnitextPlaintextType.SUP && inoutres320) 
                    break;
                if (((tag.typ == CorrFootnoteTyps.SUPDIGIT || tag.typ == CorrFootnoteTyps.DIGIT)) && jj == (ind + 1)) {
                }
                else if (txt.length() > ((int)((1.2 * ((double)it.text.length()))))) {
                    if (jj > 0 && (_items.get(jj - 1) instanceof com.pullenti.unitext.UnitextComment)) {
                    }
                    else 
                        break;
                }
                int chs = 0;
                int hiphs = 0;
                for (char ch : txt1.toCharArray()) {
                    if (Character.isLetterOrDigit(ch)) 
                        chs++;
                }
                if (txt1.length() > 1 && chs == 0) {
                    res.endInd = (ind = jj);
                    break;
                }
                for (char ch : txt1.toCharArray()) {
                    if (Character.isLetterOrDigit(ch)) 
                        break;
                    else if (ch == '_' || ch == '-') 
                        hiphs++;
                }
                if (hiphs > 10) {
                    res.endInd = (ind = jj);
                    break;
                }
                if (txt.length() == 0) 
                    txt = txt1.trim();
                else 
                    txt = (txt + " " + txt1.trim());
                res.endInd = (ind = jj);
            }
            res.items.add(txt);
        }
        boolean delimLine = false;
        ind = res.beginInd;
        if (ind >= 2 && (_items.get(ind - 1) instanceof com.pullenti.unitext.UnitextNewline)) {
            it = (com.pullenti.unitext.UnitextPlaintext)com.pullenti.unisharp.Utils.cast(_items.get(ind - 2), com.pullenti.unitext.UnitextPlaintext.class);
            if (it != null) {
                int cou = 0;
                i = 0;
                for (i = 0; i < it.text.length(); i++) {
                    if (CorrLine.isHiphen(it.text.charAt(i)) || it.text.charAt(i) == '_') 
                        cou++;
                    else if (!com.pullenti.unisharp.Utils.isWhitespace(it.text.charAt(i))) 
                        break;
                }
                if (cou > 0 && i >= it.text.length()) {
                    res.beginInd -= 2;
                    delimLine = true;
                }
            }
        }
        if (res.startNumber != 1 && res.startNumber != numStart) {
            if (!delimLine && (res.items.size() < 2)) {
                if (res.typ != CorrFootnoteTyps.SUPDIGIT) 
                    return null;
            }
        }
        return res;
    }

    public com.pullenti.unitext.UnitextItem correctItem(com.pullenti.unitext.UnitextItem it, boolean robust) {
        com.pullenti.unitext.UnitextPlaintext pl = (com.pullenti.unitext.UnitextPlaintext)com.pullenti.unisharp.Utils.cast(it, com.pullenti.unitext.UnitextPlaintext.class);
        if (pl != null) {
            if (typ == CorrFootnoteTyps.SUPDIGIT && pl.typ == com.pullenti.unitext.UnitextPlaintextType.SUP) {
                int num;
                com.pullenti.unisharp.Outargwrapper<Integer> wrapnum326 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                boolean inoutres327 = com.pullenti.unisharp.Utils.parseInteger(pl.text.trim(), 0, null, wrapnum326);
                num = (wrapnum326.value != null ? wrapnum326.value : 0);
                if (inoutres327) {
                    int ii = num - startNumber;
                    if ((ii < 0) || ii >= items.size()) 
                        return pl;
                    com.pullenti.unitext.UnitextFootnote fn = new com.pullenti.unitext.UnitextFootnote();
                    fn.content = com.pullenti.unitext.UnitextPlaintext._new57(items.get(ii));
                    fn.customMark = ((Integer)num).toString();
                    return fn;
                }
            }
            com.pullenti.unitext.UnitextContainer cnt = null;
            String txt = pl.text;
            for (int i = 0; i < txt.length(); i++) {
                if (com.pullenti.unisharp.Utils.isWhitespace(txt.charAt(i))) 
                    continue;
                int j = i;
                com.pullenti.unisharp.Outargwrapper<Integer> wrapj331 = new com.pullenti.unisharp.Outargwrapper<Integer>(j);
                CorrFootnoteTag tag = CorrFootnoteTag.tryParse(txt, wrapj331, false, robust);
                j = (wrapj331.value != null ? wrapj331.value : 0);
                if (tag == null) 
                    continue;
                if (tag.typ != typ) 
                    continue;
                int ii = tag.number - startNumber;
                if ((ii < 0) || ii >= items.size()) 
                    continue;
                if (cnt == null) 
                    cnt = new com.pullenti.unitext.UnitextContainer();
                if (i > 0) 
                    cnt.children.add(com.pullenti.unitext.UnitextPlaintext._new57(txt.substring(0, 0 + i)));
                cnt.children.add(com.pullenti.unitext.UnitextFootnote._new330(com.pullenti.unitext.UnitextPlaintext._new57(items.get(ii)), ((Integer)tag.number).toString()));
                j++;
                if (j >= txt.length()) {
                    txt = "";
                    break;
                }
                txt = txt.substring(j);
                i = -1;
            }
            if (!com.pullenti.unisharp.Utils.isNullOrEmpty(txt) && cnt != null) 
                cnt.children.add(com.pullenti.unitext.UnitextPlaintext._new57(txt));
            return cnt;
        }
        if (it instanceof com.pullenti.unitext.UnitextContainer) {
            com.pullenti.unitext.UnitextContainer cnt = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(it, com.pullenti.unitext.UnitextContainer.class);
            boolean isCh = false;
            for (int i = 0; i < cnt.children.size(); i++) {
                com.pullenti.unitext.UnitextItem ch = this.correctItem(cnt.children.get(i), robust);
                if (ch == null) 
                    continue;
                com.pullenti.unisharp.Utils.putArrayValue(cnt.children, i, ch);
                isCh = true;
            }
            return (isCh ? cnt : null);
        }
        if (it instanceof com.pullenti.unitext.UnitextTable) {
            com.pullenti.unitext.UnitextTable tab = (com.pullenti.unitext.UnitextTable)com.pullenti.unisharp.Utils.cast(it, com.pullenti.unitext.UnitextTable.class);
            boolean isCh = 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.getRowBegin() != r || cel.getColBegin() != c || cel.content == null) 
                        continue;
                    com.pullenti.unitext.UnitextItem cnt = this.correctItem(cel.content, robust);
                    if (cnt != null) {
                        cel.content = cnt;
                        isCh = true;
                    }
                }
            }
            return (isCh ? tab : null);
        }
        if (it instanceof com.pullenti.unitext.UnitextList) {
            com.pullenti.unitext.UnitextList li = (com.pullenti.unitext.UnitextList)com.pullenti.unisharp.Utils.cast(it, com.pullenti.unitext.UnitextList.class);
            boolean isCh = false;
            for (com.pullenti.unitext.UnitextListitem ii : li.items) {
                com.pullenti.unitext.UnitextItem cnt = this.correctItem(ii.content, robust);
                if (cnt != null) {
                    isCh = true;
                    ii.content = cnt;
                }
                com.pullenti.unitext.UnitextList sub = (com.pullenti.unitext.UnitextList)com.pullenti.unisharp.Utils.cast(this.correctItem(ii.sublist, robust), com.pullenti.unitext.UnitextList.class);
                if (sub != null) {
                    ii.sublist = sub;
                    isCh = true;
                }
            }
            return (isCh ? li : null);
        }
        return null;
    }

    public static CorrFootnoteContainer _new308(CorrFootnoteTyps _arg1) {
        CorrFootnoteContainer res = new CorrFootnoteContainer();
        res.typ = _arg1;
        return res;
    }
    public CorrFootnoteContainer() {
    }
}
