/*
 * 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 Utils = require("./../../../unisharp/Utils");
const RefOutArgWrapper = require("./../../../unisharp/RefOutArgWrapper");
const StringBuilder = require("./../../../unisharp/StringBuilder");

const UnitextContainerType = require("./../../UnitextContainerType");
const HtmlSectionItem = require("./HtmlSectionItem");
const UnitextItem = require("./../../UnitextItem");
const UnitextContainer = require("./../../UnitextContainer");
const UnitextDocblock = require("./../../UnitextDocblock");
const UnitextNewline = require("./../../UnitextNewline");
const UnitextGen = require("./../uni/UnitextGen");

class HtmlSection {
    
    constructor() {
        this.nodes = new Array();
        this.title = null;
        this.level = 0;
        this.children = new Array();
    }
    
    toString() {
        let tmp = new StringBuilder();
        tmp.append("H").append(this.level).append(": ");
        if (this.children.length > 0) 
            tmp.append("Childs=").append(this.children.length).append(" ");
        if (this.nodes.length > 0) 
            tmp.append("Nodes=").append(this.nodes.length).append(" ");
        if (this.title !== null) 
            tmp.append(this.title);
        return tmp.toString();
    }
    
    _generate(blk, lev, hg) {
        let gen = null;
        if (this.title !== null) {
            blk.head = new UnitextContainer();
            gen = new UnitextGen();
            hg.getUniText(this.title, gen, null, 0);
            let hh = gen.finish(true, null);
            if (hh instanceof UnitextContainer) 
                blk.head = Utils.as(hh, UnitextContainer);
            else if (hh !== null) 
                blk.head.children.push(hh);
            blk.head.children.push(new UnitextNewline());
            blk.head.typ = UnitextContainerType.HEAD;
        }
        blk.body = new UnitextContainer();
        if (this.nodes.length > 0) {
            gen = new UnitextGen();
            for (const ch of this.nodes) {
                hg.getUniText(ch, gen, null, 0);
            }
            let hh = gen.finish(true, null);
            if (hh !== null && this.children.length > 0) {
                let pr = UnitextDocblock._new38("Preamble");
                pr.body = hh;
                blk.body.children.push(pr);
            }
            else if (hh instanceof UnitextContainer) 
                blk.body = Utils.as(hh, UnitextContainer);
            else if (hh !== null) 
                blk.body.children.push(hh);
        }
        for (const ch of this.children) {
            let chblk = new UnitextDocblock();
            blk.body.children.push(chblk);
            if (lev === 0) 
                chblk.typname = "Section";
            else if (lev === 1) 
                chblk.typname = "Subsection";
            else 
                chblk.typname = "Chapter";
            ch._generate(chblk, lev + 1, hg);
        }
    }
    
    static create(n) {
        let ttt = new HtmlSectionItem();
        ttt.stack.push(new HtmlSection());
        HtmlSection._create(n, ttt);
        if (ttt.stack.length > 1) 
            ttt.stack.splice(0, ttt.stack.length - 1);
        if (ttt.stack[0].children.length === 0) 
            return null;
        if (ttt.stack[0].children.length === 1) {
            if (ttt.stack[0].nodes.length > 0) 
                ttt.head.splice(ttt.head.length, 0, ...ttt.stack[0].nodes);
            ttt.stack[0] = ttt.stack[0].children[0];
        }
        if (ttt.stack[0].children.length < 2) 
            return null;
        return ttt;
    }
    
    static _create(n, st) {
        let curLev = st.stack.length;
        for (const ch of n.children) {
            if (!Utils.isNullOrEmpty(ch.tagName) && ch.tagName[0] === 'H') {
                let le = 0;
                let wraple40 = new RefOutArgWrapper();
                let inoutres41 = Utils.tryParseInt(ch.tagName.substring(1), wraple40);
                le = wraple40.value;
                if (inoutres41) {
                    if (le > 0) {
                        if (le < st.stack.length) 
                            st.stack.splice(0, st.stack.length - le);
                        while (le > st.stack.length) {
                            if (st.stack[0].children.length === 0) 
                                break;
                            let al = st.stack[0].children[st.stack[0].children.length - 1];
                            st.stack.splice(0, 0, al);
                        }
                        if (le === st.stack.length) {
                            let s = HtmlSection._new39(le, ch);
                            st.stack[0].children.push(s);
                            st.stack.splice(0, 0, s);
                            st.hasBody = true;
                            for (let p = n; p !== null; p = p.parent) {
                                p.misc = p;
                            }
                            continue;
                        }
                        else {
                        }
                    }
                }
            }
            if (ch.text !== null && ch.text.startsWith("Автор:")) {
            }
            HtmlSection._create(ch, st);
            if (ch.misc !== null) 
                continue;
            if (n.misc !== null) {
                if (st.stack.length > 1) 
                    st.stack[0].nodes.push(ch);
                else if (st.hasBody) 
                    st.tail.push(ch);
                else 
                    st.head.push(ch);
            }
        }
        if (curLev < st.stack.length) 
            st.stack.splice(0, st.stack.length - curLev);
    }
    
    static _new39(_arg1, _arg2) {
        let res = new HtmlSection();
        res.level = _arg1;
        res.title = _arg2;
        return res;
    }
}


module.exports = HtmlSection