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

const GetPlaintextParam = require("./GetPlaintextParam");
const UnitextItem = require("./UnitextItem");
const UnitextContainer = require("./UnitextContainer");
const UnitextNewline = require("./UnitextNewline");
const UnitextList = require("./UnitextList");

/**
 * Элемент списка
 */
class UnitextListitem extends UnitextItem {
    
    constructor() {
        super();
        this.prefix = null;
        this.content = null;
        this.sublist = null;
    }
    
    toString() {
        let res = new StringBuilder();
        if (this.prefix !== null) 
            res.append(this.prefix.toString()).append(": ");
        if (this.content !== null) 
            res.append(this.content.toString());
        if (this.sublist !== null) 
            res.append(" + ").append(this.sublist.toString());
        return res.toString();
    }
    
    clone() {
        let res = new UnitextListitem();
        res._cloneFrom(this);
        if (this.prefix !== null) 
            res.prefix = this.prefix.clone();
        if (this.content !== null) 
            res.content = this.content.clone();
        if (this.sublist !== null) 
            res.sublist = Utils.as(this.sublist.clone(), UnitextList);
        return res;
    }
    
    get isInline() {
        return false;
    }
    
    get innerTag() {
        return "litm";
    }
    
    findById(_id) {
        if (this.id === _id) 
            return this;
        let res = null;
        if (this.prefix !== null) {
            if ((((res = this.prefix.findById(_id)))) !== null) 
                return res;
        }
        if (this.content !== null) {
            if ((((res = this.content.findById(_id)))) !== null) 
                return res;
        }
        if (this.sublist !== null) {
            if ((((res = this.sublist.findById(_id)))) !== null) 
                return res;
        }
        return null;
    }
    
    getAllItems(res, lev) {
        if (res !== null) 
            res.push(this);
        if (this.prefix !== null) {
            this.prefix.parent = this;
            this.prefix.getAllItems(res, lev + 1);
        }
        if (this.content !== null) {
            this.content.parent = this;
            this.content.getAllItems(res, lev + 1);
        }
        if (this.sublist !== null) {
            this.sublist.parent = this;
            this.sublist.getAllItems(res, lev + 1);
        }
    }
    
    getPlaintext(res, pars) {
        if (pars !== null && pars.setPositions) 
            this.beginChar = res.length;
        if (pars === null) 
            pars = UnitextItem.m_DefParams;
        if (this.prefix !== null) {
            this.prefix.getPlaintext(res, pars);
            res.append(' ');
        }
        if (this.content !== null) 
            this.content.getPlaintext(res, pars);
        res.append((pars.newLine != null ? pars.newLine : ""));
        if (this.sublist !== null) 
            this.sublist.getPlaintext(res, pars);
        if (pars !== null && pars.setPositions) 
            this.endChar = res.length - 1;
    }
    
    getHtml(res, par) {
        if (!par.callBefore(this, res)) 
            return;
        res.append("\r\n<LI");
        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(">");
        if (this.prefix !== null) {
            this.prefix.getHtml(res, par);
            res.append(' ');
        }
        if (this.content !== null) 
            this.content.getHtml(res, par);
        if (this.sublist !== null) 
            this.sublist.getHtml(res, par);
        res.append("</LI>");
        par.callAfter(this, res);
    }
    
    getXml(xml) {
        xml.writeStartElement("listitem");
        this._writeXmlAttrs(xml);
        if (this.prefix !== null) {
            xml.writeStartElement("prefix");
            this.prefix.getXml(xml);
            xml.writeEndElement();
        }
        if (this.content !== null) 
            this.content.getXml(xml);
        if (this.sublist !== null) 
            this.sublist.getXml(xml);
        xml.writeEndElement();
    }
    
    fromXml(xml) {
        const UnitextHelper = require("./internal/uni/UnitextHelper");
        super.fromXml(xml);
        for (const x of xml.child_nodes) {
            if (x.local_name === "prefix") {
                for (const xx of x.child_nodes) {
                    this.prefix = UnitextHelper.createItem(xx, this);
                    break;
                }
            }
            else {
                let it = UnitextHelper.createItem(x, this);
                if (it instanceof UnitextList) 
                    this.sublist = Utils.as(it, UnitextList);
                else if (it !== null) 
                    this.content = it;
            }
        }
    }
    
    optimize(isContent, pars) {
        if (this.prefix !== null) 
            this.prefix = this.prefix.optimize(true, pars);
        if (this.content !== null) 
            this.content = this.content.optimize(true, pars);
        if (this.content !== null && this.content.isWhitespaces) 
            this.content = null;
        if (this.sublist !== null) {
            if (this.sublist.items.length === 1) {
            }
            this.sublist.parent = this;
            let oo = this.sublist.optimize(false, pars);
            this.sublist = Utils.as(oo, UnitextList);
            if (this.sublist === null && oo !== null) {
                if (this.content === null) 
                    this.content = oo;
                else {
                    let cnt = new UnitextContainer();
                    cnt.children.push(this.content);
                    cnt.children.push(new UnitextNewline());
                    cnt.children.push(oo);
                    this.content = cnt.optimize(isContent, pars);
                }
            }
        }
        return this;
    }
    
    addPlainTextPos(d) {
        super.addPlainTextPos(d);
        if (this.prefix !== null) 
            this.prefix.addPlainTextPos(d);
        if (this.content !== null) 
            this.content.addPlainTextPos(d);
        if (this.sublist !== null) 
            this.sublist.addPlainTextPos(d);
    }
    
    correct(typ, data) {
        if (this.prefix !== null) 
            this.prefix.correct(typ, data);
        if (this.content !== null) 
            this.content.correct(typ, data);
        if (this.sublist !== null) 
            this.sublist.correct(typ, data);
    }
    
    replaceChild(old, ne) {
        if (this.content !== old) 
            return false;
        this.content = ne;
        ne.parent = this;
        return true;
    }
    
    setDefaultTextPos(cp, res) {
        this.beginChar = cp.value;
        if (this.prefix !== null) {
            this.prefix.setDefaultTextPos(cp, res);
            this.prefix.parent = this;
        }
        if (this.content !== null) {
            this.content.setDefaultTextPos(cp, res);
            this.content.parent = this;
        }
        cp.value++;
        if (res !== null) 
            res.append('\n');
        if (this.sublist !== null) {
            this.sublist.setDefaultTextPos(cp, res);
            this.sublist.parent = this;
        }
        this.endChar = cp.value - 1;
    }
    
    static _new371(_arg1) {
        let res = new UnitextListitem();
        res.content = _arg1;
        return res;
    }
    
    static static_constructor() {
        UnitextListitem.m_Pars = new GetPlaintextParam();
    }
}


UnitextListitem.static_constructor();

module.exports = UnitextListitem