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

const UnitextPagesectionItemPages = require("./../../UnitextPagesectionItemPages");
const MiscHelper = require("./../../../util/MiscHelper");
const UnitextPagesection = require("./../../UnitextPagesection");

class DocSection {
    
    constructor() {
        this.uSect = new UnitextPagesection();
        this.titlePg = false;
        this.loaded = false;
        this.headIds = new Hashtable();
        this.footIds = new Hashtable();
    }
    
    static _readDoubleCm(str) {
        let d = 0;
        let wrapd446 = new RefOutArgWrapper();
        let inoutres447 = MiscHelper.tryParseDouble(str, wrapd446);
        d = wrapd446.value;
        if (!inoutres447) 
            return 0;
        d /= (20);
        d *= 2.54;
        d /= (72);
        return Utils.mathRound(d, 2);
    }
    
    static _readAttrVal(x, nam, nam2 = null) {
        if (x.attributes !== null) {
            for (const a of x.attributes) {
                if (a.local_name === nam) 
                    return a.value;
                else if (nam2 !== null && a.local_name === nam2) 
                    return a.value;
            }
        }
        return null;
    }
    
    load(xml) {
        this.loaded = true;
        for (const x of xml.child_nodes) {
            if (x.local_name === "pgSz") {
                this.uSect.width = DocSection._readDoubleCm(DocSection._readAttrVal(x, "w", null));
                this.uSect.height = DocSection._readDoubleCm(DocSection._readAttrVal(x, "h", null));
            }
            else if (x.local_name === "pgMar") {
                this.uSect.left = DocSection._readDoubleCm(DocSection._readAttrVal(x, "left", null));
                this.uSect.right = DocSection._readDoubleCm(DocSection._readAttrVal(x, "right", null));
                this.uSect.top = DocSection._readDoubleCm(DocSection._readAttrVal(x, "top", null));
                this.uSect.bottom = DocSection._readDoubleCm(DocSection._readAttrVal(x, "bottom", null));
                this.uSect.headerHeight = DocSection._readDoubleCm(DocSection._readAttrVal(x, "header", null));
                this.uSect.footerHeight = DocSection._readDoubleCm(DocSection._readAttrVal(x, "footer", null));
            }
            else if (x.local_name === "titlePg") 
                this.titlePg = true;
            else if (x.local_name === "headerReference" || x.local_name === "footerReference") {
                let id = DocSection._readAttrVal(x, "id", null);
                let typ = DocSection._readAttrVal(x, "type", null);
                let ptyp = UnitextPagesectionItemPages.DEFAULT;
                if (Utils.compareStrings(typ, "first", true) === 0) 
                    ptyp = UnitextPagesectionItemPages.FIRST;
                else if (Utils.compareStrings(typ, "even", true) === 0) 
                    ptyp = UnitextPagesectionItemPages.EVEN;
                else if (Utils.compareStrings(typ, "odd", true) === 0) 
                    ptyp = UnitextPagesectionItemPages.ODD;
                if (x.local_name === "headerReference") {
                    if (!this.headIds.containsKey(ptyp)) 
                        this.headIds.put(ptyp, id);
                }
                else if (!this.footIds.containsKey(ptyp)) 
                    this.footIds.put(ptyp, id);
            }
        }
    }
}


module.exports = DocSection