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

const RefOutArgWrapper = require("./../../../unisharp/RefOutArgWrapper");
const StringBuilder = require("./../../../unisharp/StringBuilder");

const UnitextStyledFragmentType = require("./../../UnitextStyledFragmentType");
const UnitextPlaintextType = require("./../../UnitextPlaintextType");
const UnitextPlaintext = require("./../../UnitextPlaintext");

class StyleHelper {
    
    static processDoc(doc) {
        StyleHelper.processContent(doc.content);
        for (const s of doc.sections) {
            for (const it of s.items) {
                if (it.content !== null) 
                    StyleHelper.processContent(it.content);
            }
        }
    }
    
    static processContent(cnt) {
        if (cnt === null || cnt.m_StyledFrag === null) 
            return;
        let defTxt = new StringBuilder();
        let cp = 0;
        let wrapcp347 = new RefOutArgWrapper(cp);
        cnt.setDefaultTextPos(wrapcp347, defTxt);
        cp = wrapcp347.value;
        let its = new Array();
        cnt.getAllItems(its, 0);
        for (let i = 0; i < (its.length - 1); i++) {
            if ((its[i] instanceof UnitextPlaintext) && its[i].typ === UnitextPlaintextType.GENERATED) {
                if (its[i].parent === its[i + 1].parent && its[i].m_StyledFrag === null) 
                    its[i].m_StyledFrag = its[i + 1].m_StyledFrag;
            }
        }
        for (const it of its) {
            if (it.m_StyledFrag !== null) {
                let fr = it.m_StyledFrag;
                if (fr.beginChar < 0) 
                    fr.beginChar = it.beginChar;
                if (it.endChar > fr.endChar) 
                    fr.endChar = it.endChar;
            }
        }
        StyleHelper._corrCp(cnt.m_StyledFrag);
        StyleHelper._setText(cnt.m_StyledFrag, defTxt.toString());
        cnt.m_StyledFrag = StyleHelper._optimize(cnt.m_StyledFrag);
        for (const it of its) {
            if (it.m_StyledFrag !== null) {
                if (it.m_StyledFrag.tag !== null) 
                    it.m_StyledFrag = null;
            }
        }
    }
    
    static _corrCp(fr) {
        fr.tag = null;
        for (let i = 0; i < fr.children.length; i++) {
            let ch = fr.children[i];
            ch.parent = fr;
            StyleHelper._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;
            }
        }
    }
    
    static _setText(fr, txt) {
        let outText = true;
        for (let i = 0; i < fr.children.length; i++) {
            let ch = fr.children[i];
            StyleHelper._setText(ch, txt);
            if (ch.typ !== UnitextStyledFragmentType.INLINE && ch.typ !== 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);
    }
    
    static _optimize(fr) {
        if (fr.parent !== null && fr.style === fr.parent.style) 
            fr.style = null;
        for (let i = 0; i < fr.children.length; i++) {
            let ch = fr.children[i];
            if (ch.beginChar === 172) {
            }
            ch = StyleHelper._optimize(ch);
            if (ch === null) {
                fr.children.splice(i, 1);
                i--;
                continue;
            }
            fr.children[i] = ch;
            if (i > 0) {
                let ch0 = fr.children[i - 1];
                if (ch0.style === ch.style && ch0.typ === ch.typ && (ch0.endChar + 1) === ch.beginChar) {
                    ch0.children.splice(ch0.children.length, 0, ...ch.children);
                    for (const ccc of 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.splice(i, 1);
                    i--;
                    ch.tag = ch0;
                    continue;
                }
            }
        }
        if (fr.children.length === 0 && (fr.beginChar < 0)) 
            return null;
        for (let k = 0; k < fr.children.length; k++) {
            let ch = false;
            for (let j = 0; j < (fr.children.length - 1); j++) {
                if (fr.children[j].beginChar > fr.children[j + 1].beginChar) {
                    let frr = fr.children[j];
                    fr.children[j] = fr.children[j + 1];
                    fr.children[j + 1] = frr;
                    ch = true;
                }
            }
            if (!ch) 
                break;
        }
        for (let i = 0; i < (fr.children.length - 1); i++) {
            let ch = fr.children[i];
            let ch1 = fr.children[i + 1];
            if (ch.endChar > ch1.beginChar) {
            }
        }
        return fr;
    }
}


module.exports = StyleHelper