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

const MiscHelper = require("./../../../util/MiscHelper");
const UnitextStyle = require("./../../UnitextStyle");

class DocTextStyle {
    
    constructor() {
        this.name = null;
        this.aliases = null;
        this.uStyle = new UnitextStyle();
        this.numId = null;
        this.numLvl = 0;
        this.isDefault = false;
        this.xlsFormat = null;
        this.xlsBorder = null;
        this.xml = null;
        this.basedOn = null;
        this.loaded = false;
    }
    
    get isHeading() {
        if (this.name !== null) {
            if (Utils.startsWithString(this.name, "head", true)) 
                return true;
        }
        if (this.aliases !== null) {
            if (Utils.startsWithString(this.aliases, "head", true)) 
                return true;
            if (this.aliases.includes("аголовок")) 
                return true;
        }
        return false;
    }
    
    calcHeadingLevel() {
        if (!this.isHeading) 
            return 0;
        if (this.aliases !== null) {
            for (let i = this.aliases.length - 1; i >= 0; i--) {
                if (!Utils.isDigit(this.aliases[i])) {
                    if ((i + 1) < this.aliases.length) 
                        return Utils.parseInt(this.aliases.substring(i + 1));
                    break;
                }
            }
        }
        if (this.name !== null) {
            for (let i = this.name.length - 1; i >= 0; i--) {
                if (!Utils.isDigit(this.name[i])) {
                    if ((i + 1) < this.name.length) 
                        return Utils.parseInt(this.name.substring(i + 1));
                    break;
                }
            }
        }
        return 0;
    }
    
    toString() {
        return this.name;
    }
    
    correctXlsText(val) {
        if (Utils.isNullOrEmpty(val) || this.xlsFormat === null) 
            return val;
        if (val === "6965606.333333333") {
        }
        if (this.xlsFormat === "22") {
            let ii = val.indexOf('.');
            let dat = val;
            let tim = null;
            if (ii > 0) {
                dat = val.substring(0, 0 + ii);
                tim = val.substring(ii + 1);
            }
            let lo = 0;
            let wraplo459 = new RefOutArgWrapper();
            let inoutres460 = Utils.tryParseInt(dat, wraplo459);
            lo = wraplo459.value;
            if (!inoutres460) 
                return val;
            let dt = new Date(1900, 1 - 1, 1, 0, 0, 0);
            dt = Utils.addDays(dt, (lo) - 2);
            let res = (String(dt.getFullYear()) + "." + Utils.correctToString((Utils.getMonth(dt)).toString(10), 2, true) + "." + Utils.correctToString((dt.getDate()).toString(10), 2, true));
            if (tim === null) 
                return res;
            let d = 0;
            let wrapd457 = new RefOutArgWrapper();
            let inoutres458 = MiscHelper.tryParseDouble(val, wrapd457);
            d = wrapd457.value;
            if (inoutres458) {
                d *= (86400);
                dt = Utils.addSeconds(dt, Math.floor(d));
                res = (res + " " + Utils.correctToString((dt.getHours()).toString(10), 2, true) + ":" + Utils.correctToString((dt.getMinutes()).toString(10), 2, true) + ":" + Utils.correctToString((dt.getSeconds()).toString(10), 2, true));
            }
            return res;
        }
        if (this.xlsFormat.includes("yyyy")) {
            let d = 0;
            let wrapd461 = new RefOutArgWrapper();
            let inoutres462 = MiscHelper.tryParseDouble(val, wrapd461);
            d = wrapd461.value;
            if (inoutres462) {
                let dt = new Date(1899, 12 - 1, 30, 0, 0, 0);
                dt = Utils.addDays(dt, Math.floor(d));
                d = d - (Math.floor(d));
                d *= (86400);
                dt = Utils.addSeconds(dt, Math.floor(d));
                return MiscHelper.outDateTime(dt, false);
            }
        }
        if ((this.xlsFormat === "18" || this.xlsFormat === "19" || this.xlsFormat === "20") || this.xlsFormat === "21") {
            let d = 0;
            let wrapd463 = new RefOutArgWrapper();
            let inoutres464 = MiscHelper.tryParseDouble(val, wrapd463);
            d = wrapd463.value;
            if (inoutres464) {
                d *= (86400);
                let dt = new Date(1900, 1 - 1, 1, 0, 0, 0);
                dt = Utils.addSeconds(dt, Math.floor(d));
                let res = (Utils.correctToString((dt.getHours()).toString(10), 2, true) + ":" + Utils.correctToString((dt.getMinutes()).toString(10), 2, true));
                if (this.xlsFormat === "19" || this.xlsFormat === "21") 
                    res = (res + ":" + Utils.correctToString((dt.getSeconds()).toString(10), 2, true));
                return res;
            }
        }
        if ((this.xlsFormat === "14" || this.xlsFormat === "15" || this.xlsFormat === "6") || this.xlsFormat === "17" || this.xlsFormat.includes("yy")) {
            let lo = 0;
            let wraplo465 = new RefOutArgWrapper();
            let inoutres466 = Utils.tryParseInt(val, wraplo465);
            lo = wraplo465.value;
            if (!inoutres466) 
                return val;
            let dt = new Date(1900, 1 - 1, 1, 0, 0, 0);
            dt = Utils.addDays(dt, (lo) - 2);
            return (String(dt.getFullYear()) + "." + Utils.correctToString((Utils.getMonth(dt)).toString(10), 2, true) + "." + Utils.correctToString((dt.getDate()).toString(10), 2, true));
        }
        if (this.xlsFormat === "9" || this.xlsFormat === "10") {
            let d = 0;
            let wrapd467 = new RefOutArgWrapper();
            let inoutres468 = MiscHelper.tryParseDouble(val, wrapd467);
            d = wrapd467.value;
            if (inoutres468) 
                return (String(Utils.mathRound(d * (100), 2)) + "%");
        }
        if (this.xlsFormat.includes("##")) {
            let delim = '.';
            let nn = 0;
            if (this.xlsFormat.includes("#.##")) {
                nn = 2;
                if (this.xlsFormat.includes("#.###")) 
                    nn = 3;
            }
            else if (this.xlsFormat.includes("#,##")) {
                delim = ',';
                nn = 2;
                if (this.xlsFormat.includes("#,###")) 
                    nn = 3;
            }
            if (nn > 0) {
                let d = 0;
                let wrapd469 = new RefOutArgWrapper();
                let inoutres470 = MiscHelper.tryParseDouble(val, wrapd469);
                d = wrapd469.value;
                if (inoutres470) {
                    d = Utils.mathRound(d, nn);
                    let val1 = MiscHelper.outDouble(d);
                    let ii = val1.indexOf('.');
                    if (ii < 0) 
                        val1 += (nn === 2 ? ".00" : ".000");
                    else {
                        let kk = val1.length - ii;
                        for (; kk < nn; kk++) {
                            val1 += "0";
                        }
                    }
                    if (delim === ',') 
                        val1 = Utils.replaceString(val1, '.', ',');
                    return val1;
                }
            }
        }
        return val;
    }
}


module.exports = DocTextStyle