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

const UniTextGenNumLevel = require("./../uni/UniTextGenNumLevel");
const UnitextGenNumStyleEx = require("./../uni/UnitextGenNumStyleEx");
const UniTextGenNumType = require("./../uni/UniTextGenNumType");
const UnitextStyle = require("./../../UnitextStyle");
const DocTextStyles = require("./DocTextStyles");
const UnitextGenNumStyle = require("./../uni/UnitextGenNumStyle");

class DocNumStyles {
    
    constructor() {
        this.m_NumStyles = new Hashtable();
    }
    
    getStyle(id) {
        if (id === null) 
            return null;
        if (this.m_NumStyles.containsKey(id)) 
            return this.m_NumStyles.get(id);
        return null;
    }
    
    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;
            }
        }
        if (nam === "val") {
            if (x.attributes !== null) {
                for (const a of x.attributes) {
                    return a.value;
                }
            }
        }
        return null;
    }
    
    _readNumLvl(nlev, xx) {
        for (const xxx of xx.child_nodes) {
            if (xxx.attributes !== null) {
                if (xxx.local_name === "numFmt") 
                    nlev.type = DocNumStyles.getNumTyp(DocNumStyles._readAttrVal(xxx, "val", null));
                else if (xxx.local_name === "lvlText") {
                    nlev.format = DocNumStyles._readAttrVal(xxx, "val", null);
                    let tmp = new StringBuilder();
                    let max = 0;
                    for (let i = 0; i < nlev.format.length; i++) {
                        let ch = nlev.format[i];
                        if (ch !== '%' || (i + 1) >= nlev.format.length) {
                            tmp.append(ch);
                            continue;
                        }
                        i++;
                        let n = ((nlev.format.charCodeAt(i)) - ('0'.charCodeAt(0)));
                        if (n <= max || n > 9) 
                            continue;
                        max = n;
                        tmp.append("%").append(n);
                    }
                    nlev.format = tmp.toString();
                }
                else if (xxx.local_name === "start") {
                    let ii = 0;
                    let wrapii434 = new RefOutArgWrapper();
                    let inoutres435 = Utils.tryParseInt(Utils.notNull(DocNumStyles._readAttrVal(xxx, "val", null), ""), wrapii434);
                    ii = wrapii434.value;
                    if (inoutres435) 
                        nlev.start = ii;
                }
            }
            if (xxx.local_name === "pPr") {
                if (nlev.style === null) 
                    nlev.style = new UnitextStyle();
                DocTextStyles.readUnitextStyleExt(xxx, nlev.style, true, false);
            }
        }
    }
    
    readAllStyles(node) {
        let abstr = new Hashtable();
        for (const xnum of node.child_nodes) {
            if (xnum.local_name === "abstractNum" && xnum.attributes !== null && xnum.attributes.length >= 1) {
                let id = DocNumStyles._readAttrVal(xnum, "abstractNumId", null);
                if (id === null || abstr.containsKey(id)) 
                    continue;
                if (id === "14") {
                }
                let nsty = UnitextGenNumStyle._new110(id);
                abstr.put(id, nsty);
                for (const xx of xnum.child_nodes) {
                    if (xx.local_name === "lvl") {
                        let nlev = new UniTextGenNumLevel();
                        nsty.levels.push(nlev);
                        this._readNumLvl(nlev, xx);
                    }
                    else if (xx.local_name === "numStyleLink") {
                        let lid = DocNumStyles._readAttrVal(xx, "val", null);
                        if (lid !== null && abstr.containsKey(lid)) 
                            nsty.levels.splice(nsty.levels.length, 0, ...abstr.get(lid).levels);
                    }
                }
            }
            else if (xnum.local_name === "num") {
                let id = DocNumStyles._readAttrVal(xnum, "numId", null);
                let intId = null;
                for (const xx of xnum.child_nodes) {
                    if (xx.local_name === "abstractNumId") 
                        intId = DocNumStyles._readAttrVal(xx, "val", null);
                }
                if (intId === null || id === null) 
                    continue;
                let num0 = null;
                let wrapnum0442 = new RefOutArgWrapper();
                let inoutres443 = abstr.tryGetValue(intId, wrapnum0442);
                num0 = wrapnum0442.value;
                if (!inoutres443) 
                    continue;
                let num = UnitextGenNumStyleEx._new437(num0.clone());
                num.id = id;
                if (!this.m_NumStyles.containsKey(id)) 
                    this.m_NumStyles.put(id, num);
                for (const xx of xnum.child_nodes) {
                    if (xx.local_name === "lvlOverride") {
                        let l = 0;
                        let wrapl440 = new RefOutArgWrapper();
                        let inoutres441 = Utils.tryParseInt(Utils.notNull(DocNumStyles._readAttrVal(xx, "val", null), ""), wrapl440);
                        l = wrapl440.value;
                        if (!inoutres441) 
                            continue;
                        if ((l < 0) || l >= num.src.levels.length) 
                            continue;
                        for (const xxx of xx.child_nodes) {
                            if (xxx.local_name === "startOverride") {
                                let s = 0;
                                let wraps438 = new RefOutArgWrapper();
                                let inoutres439 = Utils.tryParseInt(Utils.notNull(DocNumStyles._readAttrVal(xxx, "val", null), ""), wraps438);
                                s = wraps438.value;
                                if (inoutres439) {
                                    if (!num.overrideStarts.containsKey(l)) 
                                        num.overrideStarts.put(l, s);
                                }
                            }
                            else if (xxx.local_name === "lvl") 
                                this._readNumLvl(num.src.levels[l], xxx);
                        }
                    }
                }
            }
        }
    }
    
    static readNumberStyle(xml) {
        let res = null;
        for (const x of xml.child_nodes) {
            if (x.local_name === "ilvl") {
                res = new UnitextGenNumStyle();
                let nn = 0;
                let wrapnn444 = new RefOutArgWrapper();
                let inoutres445 = Utils.tryParseInt(Utils.notNull(DocNumStyles._readAttrVal(x, "val", null), ""), wrapnn444);
                nn = wrapnn444.value;
                if (inoutres445) 
                    res.lvl = nn;
            }
            else if (x.local_name === "ilfo") {
                let vv = DocNumStyles._readAttrVal(x, "val", null);
                if (vv === "2") 
                    res.isBullet = true;
            }
            else if (x.local_name === "t" && res !== null) 
                res.txt = DocNumStyles._readAttrVal(x, "val", null);
        }
        return res;
    }
    
    static getNumTyp(ty) {
        if (ty === "bullet") 
            return UniTextGenNumType.BULLET;
        if (ty.includes("decimal")) 
            return UniTextGenNumType.DECIMAL;
        if (ty === "lowerLetter") 
            return UniTextGenNumType.LOWERLETTER;
        if (ty === "russianLower") 
            return UniTextGenNumType.LOWERCYRLETTER;
        if (ty === "russianUpper") 
            return UniTextGenNumType.UPPERCYRLETTER;
        if (ty === "upperLetter") 
            return UniTextGenNumType.UPPERLETTER;
        if (ty === "lowerRoman") 
            return UniTextGenNumType.LOWERROMAN;
        if (ty.includes("oman")) 
            return UniTextGenNumType.UPPERROMAN;
        return UniTextGenNumType.DECIMAL;
    }
}


module.exports = DocNumStyles