/*
 * 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 UnitextStyledFragment = require("./UnitextStyledFragment");
const UnitextHelper = require("./internal/uni/UnitextHelper");
const UnitextDocument = require("./UnitextDocument");
const UnitextPagesectionItemPages = require("./UnitextPagesectionItemPages");
const UnitextItem = require("./UnitextItem");

/**
 * Элемент сегмента страниц
 */
class UnitextPagesectionItem extends UnitextItem {
    
    constructor() {
        super();
        this.isFooter = false;
        this.pages = UnitextPagesectionItemPages.DEFAULT;
        this.content = null;
    }
    
    toString() {
        return ((this.isFooter ? "Footer" : "Header") + " pages=" + this.pages.toString().toLowerCase());
    }
    
    clone() {
        let res = new UnitextPagesectionItem();
        res._cloneFrom(this);
        res.isFooter = this.isFooter;
        res.pages = this.pages;
        if (this.content !== null) {
            res.content = this.content.clone();
            res.content.parent = res;
        }
        return res;
    }
    
    getXml(xml) {
        xml.writeStartElement("sectionitem");
        this._writeXmlAttrs(xml);
        xml.writeAttributeString("type", (this.isFooter ? "footer" : "header"));
        xml.writeAttributeString("pages", this.pages.toString().toLowerCase());
        if (this.content !== null) {
            xml.writeStartElement("content");
            this.content.getXml(xml);
            xml.writeEndElement();
            if (this.content.m_StyledFrag !== null) 
                this.content.m_StyledFrag.getXml(xml, null, false);
        }
        xml.writeEndElement();
    }
    
    fromXml(xml) {
        let doc = null;
        for (let it = this; it !== null; it = it.parent) {
            if (it instanceof UnitextDocument) 
                doc = Utils.as(it, UnitextDocument);
        }
        super.fromXml(xml);
        for (const a of xml.attributes) {
            if (a.local_name === "type") 
                this.isFooter = a.value === "footer";
            else if (a.local_name === "pages") {
                try {
                    this.pages = UnitextPagesectionItemPages.of(a.value);
                } catch (ex619) {
                }
            }
        }
        for (const x of xml.child_nodes) {
            if (x.local_name === "content") {
                for (const xx of x.child_nodes) {
                    this.content = UnitextHelper.createItem(xx, this);
                    if (this.content !== null) 
                        this.content.parent = this;
                    break;
                }
            }
            else if (x.local_name === "stylefrag" && this.content !== null) {
                this.content.m_StyledFrag = new UnitextStyledFragment();
                this.content.m_StyledFrag.doc = doc;
                this.content.m_StyledFrag.fromXml(x);
            }
        }
    }
    
    getAllItems(res, lev) {
        if (res !== null) 
            res.push(this);
        if (this.content !== null) {
            this.content.parent = this;
            this.content.getAllItems(res, lev + 1);
        }
    }
    
    static _new48(_arg1) {
        let res = new UnitextPagesectionItem();
        res.content = _arg1;
        return res;
    }
    
    static _new49(_arg1, _arg2) {
        let res = new UnitextPagesectionItem();
        res.isFooter = _arg1;
        res.content = _arg2;
        return res;
    }
    
    static _new276(_arg1, _arg2) {
        let res = new UnitextPagesectionItem();
        res.content = _arg1;
        res.isFooter = _arg2;
        return res;
    }
}


module.exports = UnitextPagesectionItem