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

const MiscHelper = require("./../../../util/MiscHelper");
const PdfObject = require("./PdfObject");

class PdfStringValue extends PdfObject {
    
    constructor() {
        super();
        this.isHex = false;
        this.val = null;
    }
    
    toStringEx(lev) {
        let str = PdfStringValue.getStringByBytes(this.val);
        if (str.length > 100) 
            str = str.substring(0, 0 + 100) + "...";
        return ("\"" + str + "\"");
    }
    
    isSimple(lev) {
        return true;
    }
    
    static getStringByBytes(buf) {
        if (buf === null) 
            return null;
        if (buf.length > 2 && buf[0] === (0xFF) && buf[1] === (0xFE)) 
            return MiscHelper.decodeStringUnicode(buf, 2, buf.length - 2);
        if (buf.length > 2 && buf[0] === (0xFE) && buf[1] === (0xFF)) 
            return MiscHelper.decodeStringUnicodeBE(buf, 2, buf.length - 2);
        return MiscHelper.decodeStringAscii(buf, 0, -1);
    }
    
    static convertGCoding(gstr) {
        let res = new StringBuilder();
        for (let i = 0; i < gstr.length; i++) {
            if (((i + 3) < gstr.length) && gstr[i] === '/' && gstr[i + 1] === 'G') {
            }
            else {
                res.append(gstr[i]);
                continue;
            }
            let ch0 = gstr[i + 2];
            let ch1 = gstr[i + 3];
            let uu = 0;
            if (ch0 >= '0' && ch0 <= '9') 
                uu += (((ch0.charCodeAt(0)) - ('0'.charCodeAt(0))));
            else if (ch0 >= 'A' && ch0 <= 'Z') 
                uu += (10 + (((ch0.charCodeAt(0)) - ('A'.charCodeAt(0)))));
            if (uu > 0) {
                uu *= 16;
                if (ch1 >= '0' && ch1 <= '9') 
                    uu += (((ch1.charCodeAt(0)) - ('0'.charCodeAt(0))));
                else if (ch1 >= 'A' && ch1 <= 'Z') 
                    uu += (10 + (((ch1.charCodeAt(0)) - ('A'.charCodeAt(0)))));
                else 
                    uu = 0;
            }
            if (uu > 0 && (uu < MiscHelper.m_1251_utf.length)) 
                res.append(String.fromCharCode(MiscHelper.m_1251_utf[uu]));
            i += 3;
        }
        return res.toString();
    }
    
    write(s) {
        if (this.isHex) {
            s.writeByte('<'.charCodeAt(0));
            for (let i = 0; i < this.val.length; i++) {
                let v = (((this.val[i]) >>> 4)) & 0xF;
                let b = (v < 10 ? (('0'.charCodeAt(0)) + v) : ((('A'.charCodeAt(0)) + v) - 10));
                s.writeByte(b);
                v = (this.val[i]) & 0xF;
                b = (v < 10 ? (('0'.charCodeAt(0)) + v) : ((('A'.charCodeAt(0)) + v) - 10));
                s.writeByte(b);
            }
            s.writeByte('>'.charCodeAt(0));
        }
        else {
            s.writeByte('('.charCodeAt(0));
            s.write(this.val, 0, this.val.length);
            s.writeByte(')'.charCodeAt(0));
        }
    }
    
    static _new245(_arg1) {
        let res = new PdfStringValue();
        res.isHex = _arg1;
        return res;
    }
}


module.exports = PdfStringValue