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

// Provides name class for Compound File Storage.
class ExtendedName {
    
    get name() {
        return this.m_Name;
    }
    
    constructor(sname, _name, offset = 0, count = -1) {
        this.m_Name = null;
        if (sname !== null) 
            this.m_Name = sname.split('');
        else {
            if (_name === null) 
                throw new Error("name");
            if (count < 0) 
                count = _name.length;
            this.m_Name = Array(count).fill('\0');
            for(let iii = 0; iii < (count); iii++) this.m_Name[(0) + iii] = _name[(offset) + iii];
        }
    }
    
    toString() {
        return new String(this.m_Name);
    }
    
    toCharArray() {
        return this.m_Name.clone();
    }
    
    toEscapedString() {
        let sb = new StringBuilder();
        for (const ch of this.m_Name) {
            if ((ch < ' ') || ch === '\\') 
                sb.append("\\u").append(Utils.correctToString((ch.charCodeAt(0)).toString(16).toUpperCase(), 4, false));
            else 
                sb.append(ch);
        }
        return sb.toString();
    }
    
    static fromString(_name) {
        return new ExtendedName(_name, null, 0, -1);
    }
    
    static fromEscapedString(_name) {
        let sb = new StringBuilder();
        let i = 0;
        while (i < _name.length) {
            if (_name[i] !== '\\') 
                sb.append(_name[i]);
            else {
                if ((i + 6) > _name.length || _name[i + 1] !== 'u') 
                    throw new Error("Invalid escaped string format");
                let code = Utils.parseInt(_name.substring(i + 2, i + 2 + 4));
                sb.append(String.fromCharCode(code));
            }
        }
        return new ExtendedName(sb.toString(), null, 0, -1);
    }
    
    GetHashCode() {
        let hash = this.m_Name.length;
        for (let i = 0; i < this.m_Name.length; i++) {
            hash ^= (this.m_Name[i].charCodeAt(0));
        }
        return hash;
    }
    
    equals(obj) {
        return ExtendedName.ooEq(this, obj);
    }
    
    static ooEq(n1, n2) {
        if (n1.m_Name.length !== n2.m_Name.length) 
            return false;
        for (let i = 0; i < n1.m_Name.length; i++) {
            if (n1.m_Name[i] !== n2.m_Name[i]) 
                return false;
        }
        return true;
    }
    
    static ooNoteq(n1, n2) {
        return !(ExtendedName.ooEq(n1, n2));
    }
}


module.exports = ExtendedName