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

/**
 * Гиперссылка
 */
class UnitextHyperlink extends UnitextItem {
    
    constructor() {
        super();
        this.href = null;
        this.content = null;
        this.isInternal = false;
        this.data = null;
    }
    
    toString() {
        return ("Hyperlink " + ((this.href != null ? this.href : "")) + " (" + (Utils.notNull(this.getPlaintextString(null), "")) + ")");
    }
    
    clone() {
        let res = new UnitextHyperlink();
        res._cloneFrom(this);
        res.href = this.href;
        res.data = this.data;
        res.isInternal = this.isInternal;
        if (this.content !== null) 
            res.content = this.content.clone();
        return res;
    }
    
    get innerTag() {
        return "hyplnk";
    }
    
    get isInline() {
        if (this.content === null) 
            return true;
        return this.content.isInline;
    }
    
    getAllItems(res, lev) {
        if (res !== null) 
            res.push(this);
        if (this.content !== null) {
            this.content.parent = this;
            this.content.getAllItems(res, lev + 1);
        }
    }
    
    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) {
        let pos0 = res.length;
        if (pars !== null && pars.setPositions) 
            this.beginChar = res.length;
        if (this.content !== null) 
            this.content.getPlaintext(res, pars);
        if (pars === null) 
            pars = UnitextItem.m_DefParams;
        if (!Utils.isNullOrEmpty(pars.hyperlinksTemplate) && this.href !== null && !this.isInternal) {
            let tmp = new StringBuilder();
            for (let i = this.beginChar; i < res.length; i++) {
                tmp.append(res.charAt(i));
            }
            let txt = tmp.toString();
            if (tmp.length === 0) 
                res.append(this.href);
            else if (Utils.compareStrings(this.href, txt, true) !== 0) 
                res.append(Utils.replaceString(pars.hyperlinksTemplate, "%1", this.href));
        }
        if (pars !== null && pars.setPositions) 
            this.endChar = res.length - 1;
        if (pars !== null && !pars.keepIgnored && ((pars.ignoreLevel > 0 || this.ignorable))) {
            for (let i = pos0; i < res.length; i++) {
                res.setCharAt(i, ' ');
            }
        }
    }
    
    getHtml(res, par) {
        if (!par.callBefore(this, res)) 
            return;
        let tit = null;
        if (this.htmlTitle !== null || this.href !== null) {
            let tmp = new StringBuilder();
            MiscHelper.correctHtmlValue(tmp, (this.htmlTitle != null ? this.htmlTitle : this.href), true, false, false);
            tit = tmp.toString();
        }
        if (this.isInternal) 
            res.append("<i>");
        if (this.href !== null) {
            res.append("<a href=\"");
            if (this.isInternal) 
                res.append('#');
            MiscHelper.correctHtmlValue(res, this.href, true, false, false);
            res.append("\" title=\"").append(((tit != null ? tit : ""))).append("\"").append(((par.hyperlinksTargetBlank || this.href.startsWith("http") ? " target=\"_blank\"" : "")));
        }
        else 
            res.append("<span style=\"text-decoration:underline\" title=\"").append(((tit != null ? tit : ""))).append("\"");
        if (this.id !== null) 
            res.append(" id=\"").append(this.id).append("\"");
        res.append(">");
        if (this.content !== null) 
            this.content.getHtml(res, par);
        if (this.href !== null) 
            res.append("</a>");
        else 
            res.append("</span>");
        if (this.isInternal) 
            res.append("</i>");
        par.callAfter(this, res);
    }
    
    getXml(xml) {
        xml.writeStartElement("hyperlink");
        this._writeXmlAttrs(xml);
        if (this.href !== null) {
            xml.writeAttributeString("href", MiscHelper.correctXmlValue(this.href));
            if (this.isInternal) 
                xml.writeAttributeString("internal", "true");
        }
        if (this.data !== null) 
            xml.writeAttributeString("data", MiscHelper.correctXmlValue(this.data));
        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 === "href") 
                    this.href = a.value;
                else if (a.local_name === "internal") 
                    this.isInternal = a.value === "true";
                else if (a.local_name === "data") 
                    this.data = 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);
    }
    
    optimize(isContent, pars) {
        if (this.content !== null) 
            this.content = this.content.optimize(true, pars);
        if (pars !== null && this.href !== null) {
            if (((this.href.length > 2 && this.href[0] === '<' && this.href[2] === '>')) || ((this.href.length > 3 && this.href[0] === '<' && this.href[3] === '>'))) {
                let _tag = this.href.substring(0, 0 + (this.href[2] === '>' ? 3 : 4));
                let pt = Utils.as(this.content, UnitextPlaintext);
                if (pt !== null && pt.text.startsWith(_tag)) {
                    let foot = new UnitextFootnote();
                    if (this.href[2] === '>') 
                        foot.customMark = this.href.substring(1, 1 + 1);
                    else 
                        foot.customMark = this.href.substring(1, 1 + 2);
                    let txt = this.href.substring(_tag.length).trim();
                    if (txt.includes("\\\"")) 
                        txt = Utils.replaceString(txt, "\\\"", "\"");
                    foot.content = UnitextPlaintext._new57(txt);
                    return foot;
                }
                if (this.content instanceof UnitextFootnote) 
                    return this.content;
            }
        }
        return this;
    }
    
    replaceChild(old, ne) {
        if (this.content !== old) 
            return false;
        this.content = ne;
        ne.parent = this;
        return true;
    }
    
    setDefaultTextPos(cp, res) {
        this.beginChar = cp.value;
        if (this.content !== null) {
            this.content.setDefaultTextPos(cp, res);
            this.content.parent = this;
        }
        this.endChar = cp.value - 1;
    }
    
    static _new59(_arg1) {
        let res = new UnitextHyperlink();
        res.href = _arg1;
        return res;
    }
    
    static _new508(_arg1, _arg2) {
        let res = new UnitextHyperlink();
        res.isInternal = _arg1;
        res.href = _arg2;
        return res;
    }
}


module.exports = UnitextHyperlink