/*
 * 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 Stream = require("./../../../unisharp/Stream");
const MemoryStream = require("./../../../unisharp/MemoryStream");
const XmlDocument = require("./../../../unisharp/XmlDocument");
const XmlNode = require("./../../../unisharp/XmlNode");

/**
 * Это блок данных
 */
class DocxPart {
    
    constructor() {
        this.name = null;
        this.xml = null;
        this.data = null;
        this.zipEntry = null;
    }
    
    isName(_name) {
        if (Utils.compareStrings(_name, this.name, true) === 0) 
            return true;
        if (this.name.startsWith("/")) {
            if (Utils.compareStrings(_name, this.name.substring(1), true) === 0) 
                return true;
        }
        return false;
    }
    
    isNameStarts(_name) {
        if (Utils.startsWithString(this.name, _name, true)) 
            return true;
        return false;
    }
    
    toString() {
        return this.name;
    }
    
    static _startOfIns(dat, i) {
        if ((i + 7) >= dat.length) 
            return false;
        if ((((String.fromCharCode(dat[i])) === '<' && (String.fromCharCode(dat[i + 1])) === 'w' && (String.fromCharCode(dat[i + 2])) === ':') && (String.fromCharCode(dat[i + 3])) === 'i' && (String.fromCharCode(dat[i + 4])) === 'n') && (String.fromCharCode(dat[i + 5])) === 's') {
            if ((String.fromCharCode(dat[i + 6])) === ' ' || (String.fromCharCode(dat[i + 6])) === '>') 
                return true;
        }
        if ((((String.fromCharCode(dat[i])) === '<' && (String.fromCharCode(dat[i + 1])) === '/' && (String.fromCharCode(dat[i + 2])) === 'w') && (String.fromCharCode(dat[i + 3])) === ':' && (String.fromCharCode(dat[i + 4])) === 'i') && (String.fromCharCode(dat[i + 5])) === 'n' && (String.fromCharCode(dat[i + 6])) === 's') {
            if ((String.fromCharCode(dat[i + 7])) === ' ' || (String.fromCharCode(dat[i + 7])) === '>') 
                return true;
        }
        return false;
    }
    
    getXmlNode(correct = false) {
        if (this.xml !== null) 
            return this.xml;
        if (this.data !== null) 
            return null;
        let _xml = new XmlDocument();
        let dat = this.zipEntry.getData();
        if (dat === null) 
            return null;
        let hasIns = false;
        for (let i = 0; i < (dat.length - 6); i++) {
            if (DocxPart._startOfIns(dat, i)) {
                hasIns = true;
                break;
            }
        }
        if (hasIns) {
            let buf = new Array();
            for (let i = 0; i < dat.length; i++) {
                if (!DocxPart._startOfIns(dat, i)) 
                    buf.push(dat[i]);
                else 
                    for (; i < dat.length; i++) {
                        if ((String.fromCharCode(dat[i])) === '>') 
                            break;
                    }
            }
            dat = buf;
        }
        let documentXml = new MemoryStream(dat); 
        try {
            _xml.preserve_whitespace = true;
            _xml.loadStream(documentXml);
            return _xml.document_element;
        }
        finally {
            documentXml.close();
        }
    }
    
    static _getTagName(_xml, i) {
        for (let j = i; j < _xml.length; j++) {
            if (!Utils.isLetter(_xml[j]) && _xml[j] !== ':') {
                if (j === i) 
                    return null;
                return _xml.substring(i, i + j - i);
            }
        }
        return null;
    }
    
    getBytes() {
        if (this.data !== null) 
            return this.data;
        if (this.xml !== null) 
            return null;
        // всё-равно эти картинки не поддержаны браузером, а то на больших документах памяти не хватает
        if (Utils.endsWithString(this.zipEntry.name, ".emf", true)) 
            return null;
        try {
            return this.zipEntry.getData();
        } catch (ex) {
            return null;
        }
    }
    
    static _new487(_arg1, _arg2) {
        let res = new DocxPart();
        res.name = _arg1;
        res.zipEntry = _arg2;
        return res;
    }
    
    static _new488(_arg1, _arg2) {
        let res = new DocxPart();
        res.name = _arg1;
        res.xml = _arg2;
        return res;
    }
}


module.exports = DocxPart