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

// Стиль нумерации
class UnitextGenNumStyle extends IUnitextGenNumStyle {
    
    constructor() {
        super();
        this._id = null;
        this.levels = new Array();
        this._lvl = 0;
        this._isbullet = false;
        this._txt = null;
        this.nums = new Array();
        this.style = null;
    }
    
    get id() {
        return this._id;
    }
    set id(value) {
        this._id = value;
        return this._id;
    }
    
    get lvl() {
        return this._lvl;
    }
    set lvl(value) {
        this._lvl = value;
        return this._lvl;
    }
    
    get isBullet() {
        return this._isbullet;
    }
    set isBullet(value) {
        this._isbullet = value;
        return this._isbullet;
    }
    
    get txt() {
        return this._txt;
    }
    set txt(value) {
        this._txt = value;
        return this._txt;
    }
    
    toString() {
        if (this.txt !== null) 
            return ("Level: " + this.lvl + " Val: " + this.txt);
        return ("Id: " + this.id + " Levels: " + this.levels.length + " FirstLevel: " + (this.levels.length === 0 ? "" : this.levels[0].toString()));
    }
    
    clone() {
        let res = new UnitextGenNumStyle();
        res.id = this.id;
        res.lvl = this.lvl;
        res.isBullet = this.isBullet;
        res.txt = this.txt;
        if (this.style !== null) 
            res.style = this.style.clone(false);
        for (const ll of this.levels) {
            res.levels.push(ll.clone());
        }
        return res;
    }
    
    getLevel(lev) {
        if ((lev < 0) || lev >= this.levels.length) 
            return null;
        return this.levels[lev];
    }
    
    process(lev, init = false) {
        if (this.txt !== null) 
            return this.txt;
        if (lev >= this.levels.length) 
            return null;
        if (init) 
            this.nums.splice(0, this.nums.length);
        if (lev >= this.nums.length) {
            if (this.nums.length === 0) {
                for (let ii = 0; ii <= lev; ii++) {
                    if (ii < this.levels.length) 
                        this.nums.push(this.levels[ii].start);
                    else 
                        this.nums.push(1);
                }
            }
            else 
                while (lev >= this.nums.length) {
                    if (lev < this.levels.length) 
                        this.nums.push(this.levels[lev].start);
                    else 
                        this.nums.push(1);
                }
        }
        else if (lev === (this.nums.length - 1)) {
            this.nums[lev]++;
            if (this.nums[lev] < this.levels[lev].start) 
                this.nums[lev] = this.levels[lev].start;
        }
        else {
            if ((lev + 1) < this.nums.length) 
                this.nums.splice(lev + 1, this.nums.length - lev - 1);
            this.nums[lev]++;
        }
        let val = (this.levels[lev].format != null ? this.levels[lev].format : "");
        for (let ii = 0; ii <= lev; ii++) {
            if (val.includes("%" + (ii + 1).toString())) {
                let nn = this.levels[ii].getValue(this.nums[ii]);
                if (nn !== null) 
                    val = Utils.replaceString(val, "%" + (ii + 1).toString(), nn);
            }
        }
        if (val === "1.1.1.") {
        }
        return val;
    }
    
    static _new110(_arg1) {
        let res = new UnitextGenNumStyle();
        res.id = _arg1;
        return res;
    }
}


module.exports = UnitextGenNumStyle