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

const MiscHelper = require("./../util/MiscHelper");
const GetHtmlParamFootnoteOutType = require("./GetHtmlParamFootnoteOutType");
const GetPlaintextParam = require("./GetPlaintextParam");
const UnitextItem = require("./UnitextItem");
const UnitextContainerType = require("./UnitextContainerType");
const UnitextContainer = require("./UnitextContainer");
const UnitextDocblock = require("./UnitextDocblock");

/**
 * Сноска
 */
class UnitextFootnote extends UnitextItem {
    
    constructor() {
        super();
        this.content = null;
        this.isEndnote = false;
        this.customMark = null;
        this.docBlockId = null;
    }
    
    toString() {
        if (this.docBlockId !== null) {
            let ttt = Utils.replaceString(Utils.replaceString(((this.htmlTitle != null ? this.htmlTitle : "")), '\r', ' '), '\n', ' ');
            if (ttt.length > 100) 
                ttt = ttt.substring(0, 0 + 100) + "...";
            return ("<" + ((this.customMark != null ? this.customMark : "")) + "> -> " + ttt);
        }
        return ((this.isEndnote ? "Endnote" : "Footnote") + ": " + (this.content === null ? "" : this.content.toString()));
    }
    
    clone() {
        let res = new UnitextFootnote();
        res._cloneFrom(this);
        if (this.content !== null) 
            res.content = this.content.clone();
        res.isEndnote = this.isEndnote;
        res.customMark = this.customMark;
        res.docBlockId = this.docBlockId;
        return res;
    }
    
    optimize(isContent, pars) {
        if (this.content !== null) 
            this.content = this.content.optimize(true, pars);
        if (this.content === null && this.htmlTitle === null) 
            return null;
        return this;
    }
    
    getAllItems(res, lev) {
        if (res !== null) 
            res.push(this);
        if (this.content !== null) {
            this.content.parent = this;
            this.content.getAllItems(res, lev + 1);
        }
    }
    
    get innerTag() {
        return "footnote";
    }
    
    findById(_id) {
        if (this.id === _id) 
            return this;
        let res = null;
        if (this.content !== null) {
            if ((((res = this.content.findById(_id)))) !== null) 
                return res;
        }
        return null;
    }
    
    getPlaintext(res, pars) {
        if (pars !== null && pars.setPositions) 
            this.beginChar = res.length;
        if (pars === null) 
            pars = UnitextItem.m_DefParams;
        let txt = null;
        if (!Utils.isNullOrEmpty(pars.footnotesTemplate) && this.content !== null) {
            let tmp = new StringBuilder();
            this.content.getPlaintext(tmp, (pars.setPositions ? UnitextFootnote.m_FootnotesParam : UnitextFootnote.m_FootnotesParam1));
            if (tmp.length > 0 && tmp.charAt(tmp.length - 1) === '.') 
                tmp.length = tmp.length - 1;
            txt = tmp.toString();
            if (((this.customMark !== null && ((this.customMark.length < txt.length)) && txt.startsWith(this.customMark))) && Utils.isWhitespace(txt[this.customMark.length])) {
                tmp.remove(0, this.customMark.length + 1);
                txt = tmp.toString();
            }
            if (txt.startsWith(" ")) 
                txt = txt.trim();
        }
        else if (!Utils.isNullOrEmpty(pars.footnotesTemplate) && this.htmlTitle !== null && this.docBlockId !== null) 
            txt = this.htmlTitle;
        if (!Utils.isNullOrEmpty(pars.footnotesTemplate) && txt !== null) {
            let d = pars.footnotesTemplate.indexOf("%1");
            if (d < 0) {
                if (pars.setPositions) 
                    this.content.addPlainTextPos(res.length);
                res.append(pars.footnotesTemplate);
                if (pars.setPositions) 
                    this.content.endChar = (this.content.beginChar + ((res.length - this.beginChar))) - 1;
            }
            else {
                if (pars.setPositions) 
                    this.content.addPlainTextPos(res.length + ((d < 0 ? 0 : d)));
                if (txt.length > 0) 
                    res.append(Utils.replaceString(pars.footnotesTemplate, "%1", txt));
            }
        }
        if (pars !== null && pars.setPositions) 
            this.endChar = (this.beginChar + ((res.length - this.beginChar))) - 1;
    }
    
    getHtml(res, par) {
        if (this.content === null && this.docBlockId === null) 
            return;
        if (par.hideEditionsAndComments && (this.content instanceof UnitextContainer)) {
            if (this.content.typ === UnitextContainerType.EDITION || this.content.typ === UnitextContainerType.COMMENT) 
                return;
        }
        if (!par.callBefore(this, res)) 
            return;
        let _id = "";
        if (this.id !== null) 
            _id = (" id=\"" + this.id + "\"");
        if (this.docBlockId !== null) {
            let aaa = false;
            if (par !== null) {
                for (let p = this.parent; p !== null; p = p.parent) {
                    let db = Utils.as(p.findById(this.docBlockId), UnitextDocblock);
                    if (db !== null) {
                        if (!par.m_FootnotesDb.includes(db)) 
                            par.m_FootnotesDb.push(db);
                        aaa = true;
                        break;
                    }
                }
            }
            if (!aaa) {
            }
            res.append("<span title=\"");
            MiscHelper.correctHtmlValue(res, (this.htmlTitle != null ? this.htmlTitle : ""), true, false, false);
            res.append("\" style=\"color:red\"><sup>").append(((this.customMark != null ? this.customMark : "*"))).append("</sup></span>");
            par.callAfter(this, res);
            return;
        }
        if (par !== null && par.footnotes === GetHtmlParamFootnoteOutType.ENDOFUNIT) {
            if (this.isEndnote) 
                par.m_Endnotes.push(this);
            else 
                par.m_Footnotes.push(this);
            let tmp = new StringBuilder();
            this.content.getPlaintext(tmp, UnitextFootnote.m_FootnotesParam1);
            res.append("<span title=\"");
            MiscHelper.correctHtmlValue(res, tmp.toString(), true, false, false);
            if (this.isEndnote) 
                res.append("\" style=\"color:red\"><sup>&lt;E").append(par.m_Endnotes.length).append("&gt;</sup></span>");
            else if (this.customMark !== null) 
                res.append("\" style=\"color:red\"><sup>&lt;").append(this.customMark).append("&gt;</sup></span>");
            else if ((this.content instanceof UnitextContainer) && this.content.typ === UnitextContainerType.EDITION) 
                res.append("\" style=\"color:lightgray;font-size:smaller\"><sup>&lt;").append(par.m_Footnotes.length).append("&gt;</sup></span>");
            else 
                res.append("\" style=\"color:red\"><sup>&lt;").append(par.m_Footnotes.length).append("&gt;</sup></span>");
            par.callAfter(this, res);
            return;
        }
        if (par !== null && par.footnotes === GetHtmlParamFootnoteOutType.INBRACKETS) {
            res.append(" <i").append(_id).append("><sub title=\"Сноска ").append(((this.customMark != null ? this.customMark : ""))).append("\">(");
            this.content.getHtml(res, par);
            res.append(")</sub></i>");
        }
        else {
            let tmp = new StringBuilder();
            this.content.getPlaintext(tmp, UnitextFootnote.m_FootnotesParam1);
            let txt = tmp.toString().trim();
            res.append("<span title=\"");
            MiscHelper.correctHtmlValue(res, txt, true, false, false);
            res.append("\" style=\"color:red\"").append(_id).append("><sup>").append((this.isEndnote ? "**" : "*")).append(")</sup></span>");
        }
        par.callAfter(this, res);
    }
    
    getXml(xml) {
        xml.writeStartElement("footnote");
        this._writeXmlAttrs(xml);
        if (this.isEndnote) 
            xml.writeAttributeString("endnote", "true");
        if (this.customMark !== null) 
            xml.writeAttributeString("mark", MiscHelper.correctXmlValue(this.customMark));
        if (this.docBlockId !== null) 
            xml.writeAttributeString("docblockid", this.docBlockId);
        if (this.content !== null) 
            this.content.getXml(xml);
        xml.writeEndElement();
    }
    
    fromXml(xml) {
        const UnitextHelper = require("./internal/uni/UnitextHelper");
        super.fromXml(xml);
        if (xml.attributes !== null) {
            for (const a of xml.attributes) {
                if (a.local_name === "endnote") 
                    this.isEndnote = a.value === "true";
                else if (a.local_name === "mark") 
                    this.customMark = a.value;
                else if (a.local_name === "docblockid") 
                    this.docBlockId = a.value;
            }
        }
        for (const x of xml.child_nodes) {
            this.content = UnitextHelper.createItem(x, this);
            break;
        }
    }
    
    addPlainTextPos(d) {
        super.addPlainTextPos(d);
        if (this.content !== null) 
            this.content.addPlainTextPos(d);
    }
    
    correct(typ, data) {
        if (this.content !== null) 
            this.content.correct(typ, data);
    }
    
    replaceChild(old, ne) {
        if (this.content !== old) 
            return false;
        this.content = ne;
        ne.parent = this;
        return true;
    }
    
    setDefaultTextPos(cp, res) {
        this.beginChar = cp.value;
        cp.value++;
        if (res !== null) 
            res.append('<');
        if (this.content !== null) {
            this.content.setDefaultTextPos(cp, res);
            this.content.parent = this;
        }
        cp.value++;
        if (res !== null) 
            res.append('>');
        this.endChar = cp.value - 1;
    }
    
    static _new100(_arg1) {
        let res = new UnitextFootnote();
        res.customMark = _arg1;
        return res;
    }
    
    static _new284(_arg1, _arg2) {
        let res = new UnitextFootnote();
        res.content = _arg1;
        res.isEndnote = _arg2;
        return res;
    }
    
    static _new329(_arg1, _arg2) {
        let res = new UnitextFootnote();
        res.content = _arg1;
        res.customMark = _arg2;
        return res;
    }
    
    static _new513(_arg1, _arg2) {
        let res = new UnitextFootnote();
        res.isEndnote = _arg1;
        res.content = _arg2;
        return res;
    }
    
    static static_constructor() {
        UnitextFootnote.m_FootnotesParam = GetPlaintextParam._new605(true, " ", " ", " ", " ", " ");
        UnitextFootnote.m_FootnotesParam1 = GetPlaintextParam._new605(false, " ", " ", " ", " ", " ");
    }
}


UnitextFootnote.static_constructor();

module.exports = UnitextFootnote