/*
 * 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 MiscHelper = require("./../util/MiscHelper");
const UnitextItem = require("./UnitextItem");

/**
 * Примечание (аннотация). Оформляется двумя такими объектами - 
 * для начальной позиции и конечной позиции.
 * Примечание (аннотация)
 */
class UnitextComment extends UnitextItem {
    
    constructor() {
        super();
        this.text = null;
        this.author = null;
        this.twinId = null;
        this.isEndOfComment = false;
    }
    
    clone() {
        let res = new UnitextComment();
        res._cloneFrom(this);
        res.text = this.text;
        res.author = this.author;
        res.twinId = this.twinId;
        return res;
    }
    
    toString() {
        return ("Comment: " + this.text + (this.isEndOfComment ? " (end)" : ""));
    }
    
    get innerTag() {
        return "comment";
    }
    
    getPlaintext(res, pars) {
        if (pars !== null && pars.setPositions) {
            this.beginChar = (this.endChar = res.length);
            if (this.isEndOfComment && res.length > 0) 
                this.beginChar = (this.endChar = res.length - 1);
        }
    }
    
    getHtml(res, par) {
        if (par.hideEditionsAndComments) 
            return;
        if (!par.callBefore(this, res)) 
            return;
        if (this.text !== null && !this.isEndOfComment) {
            if (par.outCommentsWithDelTags) {
                res.append("<del");
                if (this.id !== null) 
                    res.append(" id=\"").append(this.id).append("\"");
                res.append(">");
                MiscHelper.correctHtmlValue(res, this.text, false, false, false);
                res.append("</del>");
            }
            else {
                res.append(" <span id=\"").append(this.id).append("\" style=\"font-size:smaller;font-style:italic;color:blue;background:lightyellow\">[<b>Комментарий: </b>");
                MiscHelper.correctHtmlValue(res, this.text, false, false, false);
                res.append("]</span>");
            }
        }
        par.callAfter(this, res);
    }
    
    getXml(xml) {
        xml.writeStartElement("comment");
        this._writeXmlAttrs(xml);
        if (this.author !== null) 
            xml.writeAttributeString("author", MiscHelper.correctXmlValue(this.author));
        if (this.isEndOfComment) 
            xml.writeAttributeString("end", "true");
        if (this.twinId !== null) 
            xml.writeAttributeString("twin", this.twinId);
        xml.writeString(MiscHelper.correctXmlValue(this.text));
        xml.writeEndElement();
    }
    
    fromXml(xml) {
        super.fromXml(xml);
        if (xml.attributes !== null) {
            for (const a of xml.attributes) {
                if (a.local_name === "author") 
                    this.author = a.value;
                else if (a.local_name === "twin") 
                    this.twinId = a.value;
                else if (a.local_name === "end") 
                    this.isEndOfComment = a.value === "true";
            }
        }
        this.text = xml.inner_text;
    }
    
    static _new50(_arg1) {
        let res = new UnitextComment();
        res.text = _arg1;
        return res;
    }
    
    static _new499(_arg1, _arg2, _arg3) {
        let res = new UnitextComment();
        res.id = _arg1;
        res.twinId = _arg2;
        res.isEndOfComment = _arg3;
        return res;
    }
}


module.exports = UnitextComment