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

using System;
using System.Collections;
using System.Collections.Generic;

namespace Pullenti.Unitext.Internal.Uni
{
    // для корректировки beginchar и endchar у стилевых фрагментов
    class ChangeTextPosInfo
    {
        Pullenti.Unitext.UnitextItem[] bMap = null;
        Pullenti.Unitext.UnitextItem[] eMap = null;
        Pullenti.Unitext.UnitextStyledFragment m_Root;
        public ChangeTextPosInfo(Pullenti.Unitext.UnitextStyledFragment fr, Pullenti.Unitext.UnitextItem root)
        {
            if (fr.EndChar == 0) 
                return;
            bMap = new Pullenti.Unitext.UnitextItem[(int)(fr.EndChar + 1)];
            eMap = new Pullenti.Unitext.UnitextItem[(int)(fr.EndChar + 1)];
            List<Pullenti.Unitext.UnitextItem> its = new List<Pullenti.Unitext.UnitextItem>();
            root.GetAllItems(its, 0);
            foreach (Pullenti.Unitext.UnitextItem it in its) 
            {
                if (it.EndChar > 0 && it.BeginChar <= it.EndChar && (it.EndChar < bMap.Length)) 
                {
                    bMap[it.BeginChar] = it;
                    eMap[it.EndChar] = it;
                }
            }
            m_Root = fr;
        }
        int[] bPos;
        int[] ePos;
        public void Restore(int newLen)
        {
            if (bMap == null) 
                return;
            bPos = new int[(int)bMap.Length];
            ePos = new int[(int)bMap.Length];
            int p = 0;
            for (int i = 0; i < bMap.Length; i++) 
            {
                Pullenti.Unitext.UnitextItem it = bMap[i];
                if (it != null) 
                {
                    if (it.BeginChar < p) 
                    {
                    }
                    p = it.BeginChar;
                }
                bPos[i] = p;
                p++;
            }
            p = newLen - 1;
            for (int i = eMap.Length - 1; i >= 0; i--) 
            {
                Pullenti.Unitext.UnitextItem it = eMap[i];
                if (it != null) 
                {
                    if (p > 0 && it.EndChar > p) 
                    {
                    }
                    p = it.EndChar;
                }
                ePos[i] = p;
                p--;
            }
            this._restore(m_Root);
            Pullenti.Unitext.UnitextStyledFragment ch = m_Root;
            for (int kk = 0; kk < 2; kk++) 
            {
                if ((ch.EndChar + 1) < newLen) 
                    ch.EndChar = newLen - 1;
                if (ch.Children.Count == 0) 
                    break;
                ch = ch.Children[ch.Children.Count - 1];
            }
        }
        void _restore(Pullenti.Unitext.UnitextStyledFragment fr)
        {
            if (fr.BeginChar >= 0 && (fr.BeginChar < bPos.Length)) 
            {
                if (bPos[fr.BeginChar] < 0) 
                {
                }
                fr.BeginChar = bPos[fr.BeginChar];
            }
            if (fr.EndChar >= 0 && (fr.EndChar < ePos.Length)) 
            {
                if (ePos[fr.EndChar] < 0) 
                {
                }
                fr.EndChar = ePos[fr.EndChar];
            }
            for (int i = 0; i < fr.Children.Count; i++) 
            {
                Pullenti.Unitext.UnitextStyledFragment 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 (int i = 0; i < fr.Children.Count; i++) 
            {
                Pullenti.Unitext.UnitextStyledFragment ch = fr.Children[i];
                if (ch.BeginChar > ch.EndChar) 
                {
                    if (i > 0 && ((i + 1) < fr.Children.Count) && (fr.Children[i - 1].EndChar + 1) == fr.Children[i + 1].BeginChar) 
                    {
                        fr.Children.RemoveAt(i);
                        i--;
                        continue;
                    }
                }
            }
            for (int i = 0; i < (fr.Children.Count - 1); i++) 
            {
                Pullenti.Unitext.UnitextStyledFragment ch = fr.Children[i];
                Pullenti.Unitext.UnitextStyledFragment 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) 
                {
                    string txt0 = ch.Text + ch2.Text;
                    for (int j = i + 2; j < fr.Children.Count; j++) 
                    {
                        ch2 = fr.Children[j];
                        if ((ch.EndChar + 1) == ch2.BeginChar) 
                        {
                            fr.Children.RemoveRange(i + 1, j - i - 1);
                            ch.Text = txt0;
                            break;
                        }
                        if (ch2.Text == null || ch2.Text.Trim().Length > 0) 
                            break;
                        txt0 += ch2.Text;
                    }
                }
            }
        }
    }
}