/*
 * 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;
using System.Text;

namespace Pullenti.Unitext.Internal.Uni
{
    static class StyleHelper
    {
        public static void ProcessDoc(Pullenti.Unitext.UnitextDocument doc)
        {
            ProcessContent(doc.Content);
            foreach (Pullenti.Unitext.UnitextPagesection s in doc.Sections) 
            {
                foreach (Pullenti.Unitext.UnitextPagesectionItem it in s.Items) 
                {
                    if (it.Content != null) 
                        ProcessContent(it.Content);
                }
            }
        }
        static void ProcessContent(Pullenti.Unitext.UnitextItem cnt)
        {
            if (cnt == null || cnt.m_StyledFrag == null) 
                return;
            StringBuilder defTxt = new StringBuilder();
            int cp = 0;
            cnt.SetDefaultTextPos(ref cp, defTxt);
            List<Pullenti.Unitext.UnitextItem> its = new List<Pullenti.Unitext.UnitextItem>();
            cnt.GetAllItems(its, 0);
            for (int i = 0; i < (its.Count - 1); i++) 
            {
                if ((its[i] is Pullenti.Unitext.UnitextPlaintext) && (its[i] as Pullenti.Unitext.UnitextPlaintext).Typ == Pullenti.Unitext.UnitextPlaintextType.Generated) 
                {
                    if (its[i].Parent == its[i + 1].Parent && its[i].m_StyledFrag == null) 
                        its[i].m_StyledFrag = its[i + 1].m_StyledFrag;
                }
            }
            foreach (Pullenti.Unitext.UnitextItem it in its) 
            {
                if (it.m_StyledFrag != null) 
                {
                    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);
            foreach (Pullenti.Unitext.UnitextItem it in its) 
            {
                if (it.m_StyledFrag != null) 
                {
                    if (it.m_StyledFrag.Tag != null) 
                        it.m_StyledFrag = null;
                }
            }
        }
        static void _corrCp(Pullenti.Unitext.UnitextStyledFragment fr)
        {
            fr.Tag = null;
            for (int i = 0; i < fr.Children.Count; i++) 
            {
                Pullenti.Unitext.UnitextStyledFragment ch = fr.Children[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;
                }
            }
        }
        static void _setText(Pullenti.Unitext.UnitextStyledFragment fr, string txt)
        {
            bool outText = true;
            for (int i = 0; i < fr.Children.Count; i++) 
            {
                Pullenti.Unitext.UnitextStyledFragment ch = fr.Children[i];
                _setText(ch, txt);
                if (ch.Typ != Pullenti.Unitext.UnitextStyledFragmentType.Inline && ch.Typ != Pullenti.Unitext.UnitextStyledFragmentType.Undefined) 
                    outText = false;
            }
            if (outText && fr.EndChar >= 0 && (fr.EndChar < txt.Length)) 
                fr.Text = txt.Substring(fr.BeginChar, (fr.EndChar + 1) - fr.BeginChar);
        }
        static Pullenti.Unitext.UnitextStyledFragment _optimize(Pullenti.Unitext.UnitextStyledFragment fr)
        {
            if (fr.Parent != null && fr.Style == fr.Parent.Style) 
                fr.Style = null;
            for (int i = 0; i < fr.Children.Count; i++) 
            {
                Pullenti.Unitext.UnitextStyledFragment ch = fr.Children[i];
                if (ch.BeginChar == 172) 
                {
                }
                ch = _optimize(ch);
                if (ch == null) 
                {
                    fr.Children.RemoveAt(i);
                    i--;
                    continue;
                }
                fr.Children[i] = ch;
                if (i > 0) 
                {
                    Pullenti.Unitext.UnitextStyledFragment ch0 = fr.Children[i - 1];
                    if (ch0.Style == ch.Style && ch0.Typ == ch.Typ && (ch0.EndChar + 1) == ch.BeginChar) 
                    {
                        ch0.Children.AddRange(ch.Children);
                        foreach (Pullenti.Unitext.UnitextStyledFragment ccc in 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.RemoveAt(i);
                        i--;
                        ch.Tag = ch0;
                        continue;
                    }
                }
            }
            if (fr.Children.Count == 0 && (fr.BeginChar < 0)) 
                return null;
            for (int k = 0; k < fr.Children.Count; k++) 
            {
                bool ch = false;
                for (int j = 0; j < (fr.Children.Count - 1); j++) 
                {
                    if (fr.Children[j].BeginChar > fr.Children[j + 1].BeginChar) 
                    {
                        Pullenti.Unitext.UnitextStyledFragment frr = fr.Children[j];
                        fr.Children[j] = fr.Children[j + 1];
                        fr.Children[j + 1] = frr;
                        ch = true;
                    }
                }
                if (!ch) 
                    break;
            }
            for (int i = 0; i < (fr.Children.Count - 1); i++) 
            {
                Pullenti.Unitext.UnitextStyledFragment ch = fr.Children[i];
                Pullenti.Unitext.UnitextStyledFragment ch1 = fr.Children[i + 1];
                if (ch.EndChar > ch1.BeginChar) 
                {
                }
            }
            return fr;
        }
    }
}