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

/**
 * Разрыв страниц
 */
class UnitextPagebreak extends UnitextItem {
    
    constructor() {
        super();
        this.typ = WhitespaceType.ORIGINAL;
        this.pageNumber = 0;
    }
    
    toString() {
        let res = "PageBreak";
        if (this.pageNumber > 0) 
            res = ("PageBreak N=" + this.pageNumber);
        if (this.typ === WhitespaceType.IGNORABLE) 
            res += " - ignore";
        else if (this.typ === WhitespaceType.ONESPACE) 
            res += " - space";
        return res;
    }
    
    clone() {
        let res = new UnitextPagebreak();
        res._cloneFrom(this);
        res.typ = this.typ;
        res.pageNumber = this.pageNumber;
        return res;
    }
    
    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 {
            res.append((((pars != null ? pars : UnitextItem.m_DefParams)).newLine != null ? ((pars != null ? pars : UnitextItem.m_DefParams)).newLine : ""));
            res.append((((pars != null ? pars : UnitextItem.m_DefParams)).pageBreak != null ? ((pars != null ? pars : UnitextItem.m_DefParams)).pageBreak : ""));
        }
        if (pars !== null && pars.setPositions) 
            this.endChar = res.length - 1;
    }
    
    getHtml(res, par) {
        if (this.typ === WhitespaceType.IGNORABLE) 
            return;
        if (this.typ === WhitespaceType.ONESPACE) {
            res.append(' ');
            return;
        }
        if (!par.callBefore(this, res)) 
            return;
        let k = 0;
        for (let p = this.parent; p !== null && (k < 10); p = p.parent,k++) {
            let cnt = Utils.as(p, UnitextContainer);
            if (cnt !== null) {
                if (cnt.typ !== UnitextContainerType.UNDEFINED && cnt.typ !== UnitextContainerType.HEAD) {
                    res.append("\r\n<BR/>");
                    par.callAfter(this, res);
                    return;
                }
            }
            let db = Utils.as(p, UnitextDocblock);
            if (db !== null) {
                if (Utils.compareStrings((db.typname != null ? db.typname : ""), "Document", true) === 0 || Utils.compareStrings((db.typname != null ? db.typname : ""), "Subdocument", true) === 0 || Utils.compareStrings((db.typname != null ? db.typname : ""), "Appendix", true) === 0) {
                }
                else if (db.typname !== null) {
                    res.append("\r\n<BR/>");
                    par.callAfter(this, res);
                    return;
                }
            }
        }
        if (par !== null) 
            par._outFootnotes(res);
        res.append("\r\n<HR");
        if (this.pageNumber > 0) 
            res.append(" title=\"End of page ").append(this.pageNumber).append("\"");
        res.append("/><br/></br>");
        par.callAfter(this, res);
    }
    
    getXml(xml) {
        xml.writeStartElement("pagebreak");
        this._writeXmlAttrs(xml);
        if (this.typ !== WhitespaceType.ORIGINAL) 
            xml.writeAttributeString("typ", this.typ.toString().toLowerCase());
        if (this.pageNumber > 0) 
            xml.writeAttributeString("pagenum", this.pageNumber.toString());
        xml.writeEndElement();
    }
    
    fromXml(xml) {
        super.fromXml(xml);
        if (xml.attributes !== null) {
            for (const a of xml.attributes) {
                if (a.local_name === "typ") {
                    try {
                        this.typ = WhitespaceType.of(a.value);
                    } catch (ex616) {
                    }
                }
                else if (a.local_name === "pagenum") 
                    this.pageNumber = Utils.parseInt(a.value);
            }
        }
    }
    
    get isWhitespaces() {
        return true;
    }
    
    get isInline() {
        if (this.typ !== WhitespaceType.ORIGINAL) 
            return true;
        return false;
    }
    
    setDefaultTextPos(cp, res) {
        this.beginChar = (this.endChar = cp.value);
        cp.value++;
        if (res !== null) 
            res.append('\f');
    }
    
    static _new350(_arg1, _arg2) {
        let res = new UnitextPagebreak();
        res.pageSectionId = _arg1;
        res.pageNumber = _arg2;
        return res;
    }
    
    static _new369(_arg1) {
        let res = new UnitextPagebreak();
        res.pageSectionId = _arg1;
        return res;
    }
    
    static _new398(_arg1) {
        let res = new UnitextPagebreak();
        res.parent = _arg1;
        return res;
    }
}


module.exports = UnitextPagebreak