/*
 * 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 UnitextContainerType = require("./UnitextContainerType");
const MiscHelper = require("./../util/MiscHelper");
const UnitextItem = require("./UnitextItem");
const UnitextContainer = require("./UnitextContainer");
const UnitextPagebreak = require("./UnitextPagebreak");
const UnitextDocument = require("./UnitextDocument");
const UnitextPlaintext = require("./UnitextPlaintext");
const UnitextNewline = require("./UnitextNewline");

/**
 * Список
 */
class UnitextList extends UnitextItem {
    
    constructor() {
        super();
        this.items = new Array();
        this.unorderPrefix = null;
        this.level = 0;
    }
    
    optimize(isContent, pars) {
        const UnitextListitem = require("./UnitextListitem");
        const UnitextHelper = require("./internal/uni/UnitextHelper");
        for (const it of this.items) {
            it.parent = this;
            it.optimize(false, pars);
            if ((it.content instanceof UnitextList) && it.sublist === null && it.content.level === (this.level + 1)) {
                it.sublist = Utils.as(it.content, UnitextList);
                it.content = null;
                continue;
            }
            if (it.sublist !== null) 
                continue;
            let cnt = Utils.as(it.content, UnitextContainer);
            if (cnt !== null && (cnt.children[cnt.children.length - 1] instanceof UnitextList) && cnt.children[cnt.children.length - 1].level === (this.level + 1)) {
                it.sublist = Utils.as(cnt.children[cnt.children.length - 1], UnitextList);
                cnt.children.splice(cnt.children.length - 1, 1);
                it.content = it.content.optimize(true, pars);
            }
        }
        if (this.items.length === 0) 
            return null;
        if (this.unorderPrefix === null) {
            let pre = null;
            let ok = true;
            for (const it of this.items) {
                if (it.prefix !== null) {
                    if (pre === null) 
                        pre = UnitextHelper.getPlaintext(it.prefix, null);
                    else if (pre !== UnitextHelper.getPlaintext(it.prefix, null)) {
                        ok = false;
                        break;
                    }
                }
            }
            if ((ok && pre !== null && pre.length === 1) && !Utils.isLetterOrDigit(pre[0])) {
                this.unorderPrefix = pre;
                for (const it of this.items) {
                    it.prefix = null;
                }
            }
        }
        else 
            for (const it of this.items) {
                it.prefix = null;
            }
        if (this.items.length === 1 && (!(this.parent instanceof UnitextListitem))) {
            if (this._isAllListItemsSingle()) {
                let ccc = new UnitextContainer();
                ccc.id = this.id;
                ccc.m_StyledFrag = this.m_StyledFrag;
                for (let li = this; li !== null; li = li.items[0].sublist) {
                    if (ccc.children.length > 0) 
                        ccc.children.push(new UnitextNewline());
                    let it = li.items[0];
                    if (ccc.id === null) 
                        ccc.id = it.id;
                    if (it.prefix !== null && it.content !== null) {
                        ccc.children.push(it.prefix);
                        ccc.children.push(UnitextPlaintext._new611(" ", it.prefix.endChar, it.prefix.endChar));
                        ccc.children.push(it.content);
                    }
                    else if (it.prefix !== null) 
                        ccc.children.push(it.prefix);
                    else if (it.content !== null) 
                        ccc.children.push(it.content);
                }
                if (this.parent instanceof UnitextContainer) {
                    let par = Utils.as(this.parent, UnitextContainer);
                    let i = par.getChildIndexOf(this);
                    if (i >= 0) {
                        if (((i + 1) < par.children.length) && (((par.children[i + 1] instanceof UnitextNewline) || (par.children[i + 1] instanceof UnitextPagebreak)))) {
                        }
                        else 
                            ccc.children.push(new UnitextNewline());
                    }
                }
                for (const ch of ccc.children) {
                    ch.parent = ccc;
                }
                return ccc.optimize(true, pars);
            }
        }
        if (pars !== null && pars.correctParams.transformListsToContainers) {
            let cnt = new UnitextContainer();
            cnt.id = this.id;
            cnt.m_StyledFrag = this.m_StyledFrag;
            for (const it of this.items) {
                if (it.prefix !== null) {
                    cnt.children.push(it.prefix);
                    it.prefix.parent = cnt;
                }
                if (it.content !== null) {
                    cnt.children.push(it.content);
                    it.content = cnt;
                }
                if (it.content !== null || it.prefix !== null) {
                    let nl = new UnitextNewline();
                    cnt.children.push(nl);
                    nl.parent = cnt;
                }
                if (it.sublist !== null) {
                    let cnt1 = it.sublist.optimize(false, pars);
                    if (cnt1 !== null) {
                        cnt.children.push(cnt1);
                        cnt1.parent = cnt;
                    }
                }
            }
            return cnt.optimize(false, pars);
        }
        return this;
    }
    
    _isAllListItemsSingle() {
        if (this.items.length > 1) 
            return false;
        let it = this.items[0];
        if (it.sublist === null) 
            return true;
        return it.sublist._isAllListItemsSingle();
    }
    
    toString() {
        return ((this.unorderPrefix === null ? "" : ("Unordered (" + this.unorderPrefix + ")")) + "List (" + this.items.length + " items, " + this.level + " level)");
    }
    
    clone() {
        const UnitextListitem = require("./UnitextListitem");
        let res = new UnitextList();
        res._cloneFrom(this);
        for (const it of this.items) {
            res.items.push(Utils.as(it.clone(), UnitextListitem));
        }
        res.unorderPrefix = this.unorderPrefix;
        res.level = this.level;
        return res;
    }
    
    get startsOnNewline() {
        return true;
    }
    
    getAllItems(res, lev) {
        if (res !== null) 
            res.push(this);
        for (const it of this.items) {
            it.parent = this;
            it.getAllItems(res, lev + 1);
        }
    }
    
    get isInline() {
        return false;
    }
    
    get innerTag() {
        return "lst";
    }
    
    findById(_id) {
        if (this.id === _id) 
            return this;
        for (const it of this.items) {
            let res = it.findById(_id);
            if (res !== null) 
                return res;
        }
        return null;
    }
    
    getPlaintext(res, pars = null) {
        if (pars !== null && pars.setPositions) 
            this.beginChar = res.length;
        for (const it of this.items) {
            for (let ii = 0; ii < this.level; ii++) {
                res.append('\t');
            }
            if (it === this.items[0] && pars !== null && pars.setPositions) 
                this.beginChar = res.length;
            if (this.unorderPrefix !== null) 
                res.append(this.unorderPrefix).append(" ");
            it.getPlaintext(res, pars);
        }
        if (pars !== null && pars.setPositions) 
            this.endChar = res.length - 1;
    }
    
    getHtml(res, par) {
        if (!par.callBefore(this, res)) 
            return;
        if ((this.parent instanceof UnitextContainer) && this.parent.typ === UnitextContainerType.HEAD && this.items.length === 1) {
            let it = this.items[0];
            if (it.prefix !== null) {
                it.prefix.getHtml(res, par);
                res.append("&nbsp;");
            }
            if (it.content !== null) 
                it.content.getHtml(res, par);
            if (it.sublist !== null) 
                it.sublist.getHtml(res, par);
        }
        else {
            res.append("\r\n<UL");
            if (this.id !== null) {
                res.append(" id=\"").append(this.id).append("\"");
                if (par !== null && par.styles.containsKey(this.id)) 
                    res.append(" style=\"").append(par.styles.get(this.id)).append("\"");
            }
            res.append(">");
            for (const it of this.items) {
                it.getHtml(res, par);
            }
            res.append("\r\n</UL>");
        }
        par.callAfter(this, res);
    }
    
    getXml(xml) {
        xml.writeStartElement("list");
        this._writeXmlAttrs(xml);
        if (this.unorderPrefix !== null) 
            xml.writeAttributeString("pref", MiscHelper.correctXmlValue(this.unorderPrefix));
        if (this.level > 0) 
            xml.writeAttributeString("level", this.level.toString());
        for (const it of this.items) {
            it.getXml(xml);
        }
        xml.writeEndElement();
    }
    
    fromXml(xml) {
        const UnitextListitem = require("./UnitextListitem");
        const UnitextHelper = require("./internal/uni/UnitextHelper");
        super.fromXml(xml);
        if (xml.attributes !== null) {
            for (const a of xml.attributes) {
                if (a.local_name === "pref") 
                    this.unorderPrefix = a.value;
                else if (a.local_name === "level") {
                    let n = 0;
                    let wrapn612 = new RefOutArgWrapper();
                    let inoutres613 = Utils.tryParseInt(a.value, wrapn612);
                    n = wrapn612.value;
                    if (inoutres613) 
                        this.level = n;
                }
            }
        }
        for (const x of xml.child_nodes) {
            let it = UnitextHelper.createItem(x, this);
            if (it instanceof UnitextListitem) 
                this.items.push(Utils.as(it, UnitextListitem));
        }
    }
    
    addPlainTextPos(d) {
        super.addPlainTextPos(d);
        for (const it of this.items) {
            it.addPlainTextPos(d);
        }
    }
    
    correct(typ, data) {
        for (const it of this.items) {
            it.correct(typ, data);
        }
    }
    
    setDefaultTextPos(cp, res) {
        this.beginChar = cp.value;
        for (const it of this.items) {
            it.setDefaultTextPos(cp, res);
            it.parent = this;
        }
        this.endChar = cp.value - 1;
    }
    
    /**
     * Удалить список из дерева, заменив его на UnitextContainer, развернув его элементы
     * @param newlineLen размер в символах перехода на новую строку
     */
    transformListToContainer(newlineLen) {
        const UnitextTablecell = require("./UnitextTablecell");
        const UnitextListitem = require("./UnitextListitem");
        if (this.parent === null) 
            return false;
        if (this.parent instanceof UnitextContainer) {
            let cnt0 = Utils.as(this.parent, UnitextContainer);
            let i0 = cnt0.getChildIndexOf(this);
            if (i0 < 0) 
                return false;
        }
        else if (this.parent instanceof UnitextListitem) 
            return false;
        let cnt = new UnitextContainer();
        cnt.beginChar = this.beginChar;
        cnt.endChar = this.endChar;
        cnt.parent = this.parent;
        for (const it of this.items) {
            if (it.prefix !== null) {
                cnt.children.push(it.prefix);
                it.prefix.parent = cnt;
            }
            if (it.content !== null) {
                cnt.children.push(it.content);
                it.content = cnt;
            }
            if (it.content !== null || it.prefix !== null) {
                let nl = new UnitextNewline();
                nl.parent = cnt;
                nl.beginChar = cnt.children[cnt.children.length - 1].endChar + 1;
                nl.endChar = (nl.beginChar + newlineLen) - 1;
                cnt.children.push(nl);
            }
            if (it.sublist !== null) {
                cnt.children.push(it.sublist);
                it.sublist.parent = cnt;
            }
        }
        if (this.parent instanceof UnitextContainer) {
            let cnt0 = Utils.as(this.parent, UnitextContainer);
            let i0 = cnt0.getChildIndexOf(this);
            cnt0.children.splice(i0, 1);
            cnt0.children.splice(i0, 0, ...cnt.children);
            for (const it of cnt.children) {
                it.parent = cnt0;
            }
        }
        else if (this.parent instanceof UnitextTablecell) 
            this.parent.content = cnt;
        else if (this.parent instanceof UnitextDocument) 
            this.parent.content = cnt;
        return true;
    }
    
    static _new63(_arg1) {
        let res = new UnitextList();
        res.level = _arg1;
        return res;
    }
    
    static _new373(_arg1, _arg2) {
        let res = new UnitextList();
        res.level = _arg1;
        res.tag = _arg2;
        return res;
    }
    
    static _new374(_arg1, _arg2, _arg3) {
        let res = new UnitextList();
        res.level = _arg1;
        res.tag = _arg2;
        res.pageSectionId = _arg3;
        return res;
    }
}


module.exports = UnitextList