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

    public static void processDoc(com.pullenti.unitext.UnitextDocument doc) {
        processContent(doc.content);
        for (com.pullenti.unitext.UnitextPagesection s : doc.sections) {
            for (com.pullenti.unitext.UnitextPagesectionItem it : s.items) {
                if (it.content != null) 
                    processContent(it.content);
            }
        }
    }

    private static void processContent(com.pullenti.unitext.UnitextItem cnt) {
        if (cnt == null || cnt.m_StyledFrag == null) 
            return;
        StringBuilder defTxt = new StringBuilder();
        int cp = 0;
        com.pullenti.unisharp.Outargwrapper<Integer> wrapcp348 = new com.pullenti.unisharp.Outargwrapper<Integer>(cp);
        cnt.setDefaultTextPos(wrapcp348, defTxt);
        cp = (wrapcp348.value != null ? wrapcp348.value : 0);
        java.util.ArrayList<com.pullenti.unitext.UnitextItem> its = new java.util.ArrayList<com.pullenti.unitext.UnitextItem>();
        cnt.getAllItems(its, 0);
        for (int i = 0; i < (its.size() - 1); i++) {
            if ((its.get(i) instanceof com.pullenti.unitext.UnitextPlaintext) && ((com.pullenti.unitext.UnitextPlaintext)com.pullenti.unisharp.Utils.cast(its.get(i), com.pullenti.unitext.UnitextPlaintext.class)).typ == com.pullenti.unitext.UnitextPlaintextType.GENERATED) {
                if (its.get(i).parent == its.get(i + 1).parent && its.get(i).m_StyledFrag == null) 
                    its.get(i).m_StyledFrag = its.get(i + 1).m_StyledFrag;
            }
        }
        for (com.pullenti.unitext.UnitextItem it : its) {
            if (it.m_StyledFrag != null) {
                com.pullenti.unitext.UnitextStyledFragment fr = it.m_StyledFrag;
                if (fr.beginChar < 0) 
                    fr.beginChar = it.beginChar;
                if (it.endChar > fr.endChar) 
                    fr.endChar = it.endChar;
            }
        }
        _corrCp(cnt.m_StyledFrag);
        _setText(cnt.m_StyledFrag, defTxt.toString());
        cnt.m_StyledFrag = _optimize(cnt.m_StyledFrag);
        for (com.pullenti.unitext.UnitextItem it : its) {
            if (it.m_StyledFrag != null) {
                if (it.m_StyledFrag.tag != null) 
                    it.m_StyledFrag = null;
            }
        }
    }

    private static void _corrCp(com.pullenti.unitext.UnitextStyledFragment fr) {
        fr.tag = null;
        for (int i = 0; i < fr.children.size(); i++) {
            com.pullenti.unitext.UnitextStyledFragment ch = fr.children.get(i);
            ch.parent = fr;
            _corrCp(ch);
            if (fr.beginChar < 0) 
                fr.beginChar = ch.beginChar;
            if (ch.endChar > fr.endChar) 
                fr.endChar = ch.endChar;
            if (ch.dispInline >= 0 && ch.lenInline > 0) {
                ch.beginChar = fr.beginChar + ch.dispInline;
                ch.endChar = (ch.beginChar + ch.lenInline) - 1;
            }
        }
    }

    private static void _setText(com.pullenti.unitext.UnitextStyledFragment fr, String txt) {
        boolean outText = true;
        for (int i = 0; i < fr.children.size(); i++) {
            com.pullenti.unitext.UnitextStyledFragment ch = fr.children.get(i);
            _setText(ch, txt);
            if (ch.typ != com.pullenti.unitext.UnitextStyledFragmentType.INLINE && ch.typ != com.pullenti.unitext.UnitextStyledFragmentType.UNDEFINED) 
                outText = false;
        }
        if (outText && fr.endChar >= 0 && (fr.endChar < txt.length())) 
            fr.text = txt.substring(fr.beginChar, fr.beginChar + (fr.endChar + 1) - fr.beginChar);
    }

    private static com.pullenti.unitext.UnitextStyledFragment _optimize(com.pullenti.unitext.UnitextStyledFragment fr) {
        if (fr.parent != null && fr.getStyle() == fr.parent.getStyle()) 
            fr.setStyle(null);
        for (int i = 0; i < fr.children.size(); i++) {
            com.pullenti.unitext.UnitextStyledFragment ch = fr.children.get(i);
            if (ch.beginChar == 172) {
            }
            ch = _optimize(ch);
            if (ch == null) {
                fr.children.remove(i);
                i--;
                continue;
            }
            com.pullenti.unisharp.Utils.putArrayValue(fr.children, i, ch);
            if (i > 0) {
                com.pullenti.unitext.UnitextStyledFragment ch0 = fr.children.get(i - 1);
                if (ch0.getStyle() == ch.getStyle() && ch0.typ == ch.typ && (ch0.endChar + 1) == ch.beginChar) {
                    com.pullenti.unisharp.Utils.addToArrayList(ch0.children, ch.children);
                    for (com.pullenti.unitext.UnitextStyledFragment ccc : ch.children) {
                        ccc.parent = ch0;
                    }
                    if (ch0.text == null) 
                        ch0.text = ch.text;
                    else if (ch.text != null) 
                        ch0.text += ch.text;
                    ch0.endChar = ch.endChar;
                    fr.children.remove(i);
                    i--;
                    ch.tag = ch0;
                    continue;
                }
            }
        }
        if (fr.children.size() == 0 && (fr.beginChar < 0)) 
            return null;
        for (int k = 0; k < fr.children.size(); k++) {
            boolean ch = false;
            for (int j = 0; j < (fr.children.size() - 1); j++) {
                if (fr.children.get(j).beginChar > fr.children.get(j + 1).beginChar) {
                    com.pullenti.unitext.UnitextStyledFragment frr = fr.children.get(j);
                    com.pullenti.unisharp.Utils.putArrayValue(fr.children, j, fr.children.get(j + 1));
                    com.pullenti.unisharp.Utils.putArrayValue(fr.children, j + 1, frr);
                    ch = true;
                }
            }
            if (!ch) 
                break;
        }
        for (int i = 0; i < (fr.children.size() - 1); i++) {
            com.pullenti.unitext.UnitextStyledFragment ch = fr.children.get(i);
            com.pullenti.unitext.UnitextStyledFragment ch1 = fr.children.get(i + 1);
            if (ch.endChar > ch1.beginChar) {
            }
        }
        return fr;
    }
    public StyleHelper() {
    }
}
