/*
 * 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 UnitextContainerType = require("./UnitextContainerType");
const LocCorrTyp = require("./internal/uni/LocCorrTyp");
const UnitextPlaintext = require("./UnitextPlaintext");
const WhitespaceType = require("./WhitespaceType");
const UnitextItem = require("./UnitextItem");
const UnitextContainer = require("./UnitextContainer");
const UnitextDocblock = require("./UnitextDocblock");

/**
 * Переход на новую строку
 */
class UnitextNewline extends UnitextItem {
    
    constructor() {
        super();
        this.count = 1;
        this.typ = WhitespaceType.ORIGINAL;
    }
    
    toString() {
        return ("NewLine (" + this.count + ")" + (this.typ === WhitespaceType.IGNORABLE ? " - ignore" : (this.typ === WhitespaceType.ONESPACE ? " - space" : (this.typ === WhitespaceType.SOFT ? " - soft" : ""))));
    }
    
    clone() {
        let res = new UnitextNewline();
        res._cloneFrom(this);
        res.count = this.count;
        res.typ = this.typ;
        return res;
    }
    
    get startsOnNewline() {
        return true;
    }
    
    getPlaintext(res, pars) {
        if (pars !== null && pars.setPositions) 
            this.beginChar = res.length;
        if (this.typ === WhitespaceType.IGNORABLE) {
        }
        else if (this.typ === WhitespaceType.ONESPACE) 
            res.append(' ');
        else 
            for (let i = 0; i < this.count; i++) {
                res.append((((pars != null ? pars : UnitextItem.m_DefParams)).newLine != null ? ((pars != null ? pars : UnitextItem.m_DefParams)).newLine : ""));
            }
        if (pars !== null && pars.setPositions) 
            this.endChar = res.length - 1;
    }
    
    static _isAllUpper(it) {
        if (it === null) 
            return false;
        for (const ch of it.text) {
            if (Utils.isLetter(ch)) {
                if (!Utils.isUpperCase(ch)) 
                    return false;
            }
        }
        return true;
    }
    
    getHtml(res, par) {
        if (this.typ === WhitespaceType.IGNORABLE) 
            return;
        if (this.typ === WhitespaceType.ONESPACE) {
            res.append(' ');
            return;
        }
        if (!par.callBefore(this, res)) 
            return;
        let isDummy = false;
        if (this.parent instanceof UnitextContainer) {
            if (this.parent.children.length < 1000) {
                let ii = this.parent.getChildIndexOf(this);
                if (ii > 0 && (ii < (this.parent.children.length - 1))) {
                    let cnt = Utils.as(this.parent, UnitextContainer);
                    if (cnt.typ === UnitextContainerType.NAME) {
                        isDummy = true;
                        if ((ii === 1 && (cnt.children[ii - 1] instanceof UnitextPlaintext) && ((ii + 1) < cnt.children.length)) && (cnt.children[ii + 1] instanceof UnitextPlaintext)) {
                            if (UnitextNewline._isAllUpper(Utils.as(cnt.children[ii - 1], UnitextPlaintext)) && !UnitextNewline._isAllUpper(Utils.as(cnt.children[ii + 1], UnitextPlaintext))) 
                                isDummy = false;
                        }
                    }
                    else if (this.parent.parent instanceof UnitextDocblock) {
                        let hasDb = false;
                        for (const ch of this.parent.children) {
                            if (ch instanceof UnitextDocblock) 
                                hasDb = true;
                        }
                        if (!hasDb) {
                        }
                    }
                }
            }
        }
        if (isDummy) 
            res.append(" ");
        else {
            for (let i = 0; i < this.count; i++) {
                res.append("\r\n<BR/>");
            }
            if (par !== null) 
                par._outFootnotes(res);
        }
        par.callAfter(this, res);
    }
    
    getXml(xml) {
        xml.writeStartElement("newline");
        this._writeXmlAttrs(xml);
        if (this.count > 1) 
            xml.writeAttributeString("count", this.count.toString());
        if (this.typ !== WhitespaceType.ORIGINAL) 
            xml.writeAttributeString("typ", this.typ.toString().toLowerCase());
        xml.writeEndElement();
    }
    
    fromXml(xml) {
        super.fromXml(xml);
        if (xml.attributes !== null) {
            for (const a of xml.attributes) {
                if (a.local_name === "count") 
                    this.count = Utils.parseInt(Utils.getXmlAttrByName(xml.attributes, "count").value);
                else if (a.local_name === "typ") {
                    try {
                        this.typ = WhitespaceType.of(a.value);
                    } catch (ex615) {
                    }
                }
            }
        }
    }
    
    get isWhitespaces() {
        return true;
    }
    
    get isInline() {
        if (this.typ !== WhitespaceType.ORIGINAL && this.typ !== WhitespaceType.SOFT) 
            return true;
        return false;
    }
    
    correct(_typ, data) {
        if (_typ === LocCorrTyp.MERGENEWLINES) {
            if (this.count === 2) 
                this.count = 1;
        }
    }
    
    setDefaultTextPos(cp, res) {
        this.beginChar = cp.value;
        cp.value += this.count;
        this.endChar = cp.value - 1;
        if (res !== null) {
            for (let i = 0; i < this.count; i++) {
                res.append('\n');
            }
        }
    }
    
    static _new351(_arg1, _arg2) {
        let res = new UnitextNewline();
        res.count = _arg1;
        res.pageSectionId = _arg2;
        return res;
    }
    
    static _new368(_arg1, _arg2) {
        let res = new UnitextNewline();
        res.pageSectionId = _arg1;
        res.typ = _arg2;
        return res;
    }
    
    static _new397(_arg1) {
        let res = new UnitextNewline();
        res.parent = _arg1;
        return res;
    }
    
    static _new633(_arg1) {
        let res = new UnitextNewline();
        res.tag = _arg1;
        return res;
    }
}


module.exports = UnitextNewline