/*
 * 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");

/**
 * Представление стилей. Реализовано пока только для формата DOCX.
 * Стиль
 */
class UnitextStyle {
    
    constructor() {
        this.attrs = new Hashtable();
        this.id = 0;
    }
    
    toString() {
        let tmp = new StringBuilder();
        for (const at of this.attrs.entries) {
            if (tmp.length > 0) 
                tmp.append(" ");
            tmp.append(at.key).append(":").append(at.value);
        }
        return tmp.toString();
    }
    
    /**
     * Получить значение атрибута
     * @param name имя
     * @return значение
     */
    getAttr(name) {
        let res = null;
        let wrapres628 = new RefOutArgWrapper();
        let inoutres629 = this.attrs.tryGetValue(name, wrapres628);
        res = wrapres628.value;
        if (inoutres629) 
            return res;
        return null;
    }
    
    addAttr(name, val, appendVal = false) {
        if (Utils.isNullOrEmpty(val)) 
            return;
        let val0 = null;
        let wrapval0630 = new RefOutArgWrapper();
        let inoutres631 = this.attrs.tryGetValue(name, wrapval0630);
        val0 = wrapval0630.value;
        if (!inoutres631) 
            this.attrs.put(name, val);
        else if (!appendVal || val0 === null) 
            this.attrs.put(name, val);
        else if (val0.indexOf(';') < 0) 
            this.attrs.put(name, (val0 + ";" + val));
        else {
            let vals = Utils.splitString(val0, ';', false);
            for (let i = 0; i < vals.length; i++) {
                if (vals[i] === val) 
                    return;
            }
            this.attrs.put(name, (val0 + ";" + val));
        }
    }
    
    removeInheritAttrs(fr) {
        let dels = null;
        for (const kp of this.attrs.entries) {
            let val = null;
            for (let p = fr; p !== null; p = p.parent) {
                if (p.styleId >= 0 && p.style !== null) {
                    if ((((val = p.style.getAttr(kp.key)))) !== null) 
                        break;
                }
            }
            if (val === null || val !== kp.value) 
                continue;
            if (dels === null) 
                dels = new Array();
            dels.push(kp.key);
        }
        if (dels !== null) {
            for (const k of dels) {
                this.attrs.remove(k);
            }
        }
    }
    
    clone(ignoreBlockAttrs = false) {
        let res = new UnitextStyle();
        this.copyTo(res);
        return res;
    }
    
    copyTo(res) {
        res.id = this.id;
        for (const kp of this.attrs.entries) {
            if (!res.attrs.containsKey(kp.key)) 
                res.addAttr(kp.key, kp.value, false);
            else 
                res.attrs.put(kp.key, kp.value);
        }
    }
    
    getXml(xml) {
        xml.writeStartElement("style");
        xml.writeAttributeString("id", this.id.toString());
        for (const kp of this.attrs.entries) {
            xml.writeAttributeString(kp.key, kp.value);
        }
        xml.writeEndElement();
    }
    
    fromXml(xml) {
        if (xml.attributes !== null) {
            for (const a of xml.attributes) {
                if (a.local_name === "id") 
                    this.id = Utils.parseInt(a.value);
                else 
                    this.addAttr(a.local_name, a.value, false);
            }
        }
    }
    
    /**
     * Вывести в Html значение атрибута style="..."
     * @param res 
     */
    getHtml(res) {
        for (const kp of this.attrs.entries) {
            if (kp.key === "heading-level") 
                continue;
            res.append(kp.key).append(":").append(kp.value).append(";");
        }
    }
}


module.exports = UnitextStyle