# 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

from pullenti.unisharp.Utils import Utils

class ChangeTextPosInfo:
    # для корректировки beginchar и endchar у стилевых фрагментов
    
    def __init__(self, fr : 'UnitextStyledFragment', root : 'UnitextItem') -> None:
        self.__bmap = None
        self.__emap = None
        self.__m_root = None;
        self.__bpos = None;
        self.__epos = None;
        if (fr.end_char == 0): 
            return
        self.__bmap = Utils.newArray(fr.end_char + 1, None)
        self.__emap = Utils.newArray(fr.end_char + 1, None)
        its = list()
        root.get_all_items(its, 0)
        for it in its: 
            if (it.end_char > 0 and it.begin_char <= it.end_char and (it.end_char < len(self.__bmap))): 
                self.__bmap[it.begin_char] = it
                self.__emap[it.end_char] = it
        self.__m_root = fr
    
    def restore(self, new_len : int) -> None:
        if (self.__bmap is None): 
            return
        self.__bpos = Utils.newArray(len(self.__bmap), 0)
        self.__epos = Utils.newArray(len(self.__bmap), 0)
        p = 0
        i = 0
        while i < len(self.__bmap): 
            it = self.__bmap[i]
            if (it is not None): 
                if (it.begin_char < p): 
                    pass
                p = it.begin_char
            self.__bpos[i] = p
            p += 1
            i += 1
        p = (new_len - 1)
        for i in range(len(self.__emap) - 1, -1, -1):
            it = self.__emap[i]
            if (it is not None): 
                if (p > 0 and it.end_char > p): 
                    pass
                p = it.end_char
            self.__epos[i] = p
            p -= 1
        self.__restore(self.__m_root)
        ch = self.__m_root
        for kk in range(2):
            if ((ch.end_char + 1) < new_len): 
                ch.end_char = (new_len - 1)
            if (len(ch.children) == 0): 
                break
            ch = ch.children[len(ch.children) - 1]
    
    def __restore(self, fr : 'UnitextStyledFragment') -> None:
        if (fr.begin_char >= 0 and (fr.begin_char < len(self.__bpos))): 
            if (self.__bpos[fr.begin_char] < 0): 
                pass
            fr.begin_char = self.__bpos[fr.begin_char]
        if (fr.end_char >= 0 and (fr.end_char < len(self.__epos))): 
            if (self.__epos[fr.end_char] < 0): 
                pass
            fr.end_char = self.__epos[fr.end_char]
        i = 0
        while i < len(fr.children): 
            ch = fr.children[i]
            self.__restore(ch)
            if (ch.end_char > fr.end_char): 
                fr.end_char = ch.end_char
            if (ch.begin_char < fr.begin_char): 
                fr.begin_char = ch.begin_char
            if (i > 0): 
                pass
            i += 1
        i = 0
        first_pass729 = True
        while True:
            if first_pass729: first_pass729 = False
            else: i += 1
            if (not (i < len(fr.children))): break
            ch = fr.children[i]
            if (ch.begin_char > ch.end_char): 
                if (i > 0 and ((i + 1) < len(fr.children)) and (fr.children[i - 1].end_char + 1) == fr.children[i + 1].begin_char): 
                    del fr.children[i]
                    i -= 1
                    continue
        i = 0
        while i < (len(fr.children) - 1): 
            ch = fr.children[i]
            ch2 = fr.children[i + 1]
            if ((ch.end_char > ch2.begin_char and ch.text is not None and len(ch.text.strip()) == 0) and ch2.text is not None and len(ch2.text.strip()) == 0): 
                txt0 = ch.text + ch2.text
                j = i + 2
                while j < len(fr.children): 
                    ch2 = fr.children[j]
                    if ((ch.end_char + 1) == ch2.begin_char): 
                        del fr.children[i + 1:i + 1+j - i - 1]
                        ch.text = txt0
                        break
                    if (ch2.text is None or len(ch2.text.strip()) > 0): 
                        break
                    txt0 += ch2.text
                    j += 1
            i += 1