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


// для корректировки beginchar и endchar у стилевых фрагментов
class ChangeTextPosInfo {
    
    constructor(fr, root) {
        this.bMap = null;
        this.eMap = null;
        this.m_Root = null;
        this.bPos = null;
        this.ePos = null;
        if (fr.endChar === 0) 
            return;
        this.bMap = Array(fr.endChar + 1).fill(null);
        this.eMap = Array(fr.endChar + 1).fill(null);
        let its = new Array();
        root.getAllItems(its, 0);
        for (const it of its) {
            if (it.endChar > 0 && it.beginChar <= it.endChar && (it.endChar < this.bMap.length)) {
                this.bMap[it.beginChar] = it;
                this.eMap[it.endChar] = it;
            }
        }
        this.m_Root = fr;
    }
    
    restore(newLen) {
        if (this.bMap === null) 
            return;
        this.bPos = new Int32Array(this.bMap.length);
        this.ePos = new Int32Array(this.bMap.length);
        let p = 0;
        for (let i = 0; i < this.bMap.length; i++) {
            let it = this.bMap[i];
            if (it !== null) {
                if (it.beginChar < p) {
                }
                p = it.beginChar;
            }
            this.bPos[i] = p;
            p++;
        }
        p = newLen - 1;
        for (let i = this.eMap.length - 1; i >= 0; i--) {
            let it = this.eMap[i];
            if (it !== null) {
                if (p > 0 && it.endChar > p) {
                }
                p = it.endChar;
            }
            this.ePos[i] = p;
            p--;
        }
        this._restore(this.m_Root);
        let ch = this.m_Root;
        for (let kk = 0; kk < 2; kk++) {
            if ((ch.endChar + 1) < newLen) 
                ch.endChar = newLen - 1;
            if (ch.children.length === 0) 
                break;
            ch = ch.children[ch.children.length - 1];
        }
    }
    
    _restore(fr) {
        if (fr.beginChar >= 0 && (fr.beginChar < this.bPos.length)) {
            if (this.bPos[fr.beginChar] < 0) {
            }
            fr.beginChar = this.bPos[fr.beginChar];
        }
        if (fr.endChar >= 0 && (fr.endChar < this.ePos.length)) {
            if (this.ePos[fr.endChar] < 0) {
            }
            fr.endChar = this.ePos[fr.endChar];
        }
        for (let i = 0; i < fr.children.length; i++) {
            let ch = fr.children[i];
            this._restore(ch);
            if (ch.endChar > fr.endChar) 
                fr.endChar = ch.endChar;
            if (ch.beginChar < fr.beginChar) 
                fr.beginChar = ch.beginChar;
            if (i > 0) {
            }
        }
        for (let i = 0; i < fr.children.length; i++) {
            let ch = fr.children[i];
            if (ch.beginChar > ch.endChar) {
                if (i > 0 && ((i + 1) < fr.children.length) && (fr.children[i - 1].endChar + 1) === fr.children[i + 1].beginChar) {
                    fr.children.splice(i, 1);
                    i--;
                    continue;
                }
            }
        }
        for (let i = 0; i < (fr.children.length - 1); i++) {
            let ch = fr.children[i];
            let ch2 = fr.children[i + 1];
            if ((ch.endChar > ch2.beginChar && ch.text !== null && ch.text.trim().length === 0) && ch2.text !== null && ch2.text.trim().length === 0) {
                let txt0 = ch.text + ch2.text;
                for (let j = i + 2; j < fr.children.length; j++) {
                    ch2 = fr.children[j];
                    if ((ch.endChar + 1) === ch2.beginChar) {
                        fr.children.splice(i + 1, j - i - 1);
                        ch.text = txt0;
                        break;
                    }
                    if (ch2.text === null || ch2.text.trim().length > 0) 
                        break;
                    txt0 += ch2.text;
                }
            }
        }
    }
}


module.exports = ChangeTextPosInfo