"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.a2b = exports.b2a = exports.ua2b64 = exports.b64_2ua = exports.b64_2ab = exports.ua2ab = exports.string2ab = exports.string2ua = exports.ua2string = void 0;
exports.utf8_2ua = utf8_2ua;
exports.ua2utf8 = ua2utf8;
exports.hex2ua = hex2ua;
exports.ua2hex = ua2hex;
exports.b64_2uas = b64_2uas;
exports.ua2b64Url = ua2b64Url;
exports.b64Url2ua = b64Url2ua;
exports.hex2string = hex2string;
exports.string2hex = string2hex;
exports.concat_uas = concat_uas;
const ModelHelper_1 = require("../../icc-api/model/ModelHelper");
Object.defineProperty(exports, "ua2string", { enumerable: true, get: function () { return ModelHelper_1.ua2string; } });
Object.defineProperty(exports, "string2ua", { enumerable: true, get: function () { return ModelHelper_1.string2ua; } });
Object.defineProperty(exports, "string2ab", { enumerable: true, get: function () { return ModelHelper_1.string2ab; } });
Object.defineProperty(exports, "ua2ab", { enumerable: true, get: function () { return ModelHelper_1.ua2ab; } });
Object.defineProperty(exports, "b64_2ab", { enumerable: true, get: function () { return ModelHelper_1.b64_2ab; } });
Object.defineProperty(exports, "b64_2ua", { enumerable: true, get: function () { return ModelHelper_1.b64_2ua; } });
Object.defineProperty(exports, "ua2b64", { enumerable: true, get: function () { return ModelHelper_1.ua2b64; } });
Object.defineProperty(exports, "b2a", { enumerable: true, get: function () { return ModelHelper_1.b2a; } });
Object.defineProperty(exports, "a2b", { enumerable: true, get: function () { return ModelHelper_1.a2b; } });
const textDecoder = typeof TextDecoder !== 'undefined' ? new TextDecoder() : null;
const textEncoder = typeof TextEncoder !== 'undefined' ? new TextEncoder() : null;
function utf8_2ua(str) {
    if (textEncoder) {
        return textEncoder.encode(str);
    }
    const utf8 = new Uint8Array(4 * str.length);
    let j = 0;
    for (let i = 0; i < str.length; i++) {
        let charcode = str.charCodeAt(i);
        if (charcode < 0x80) {
            utf8.set([charcode], j++);
        }
        else if (charcode < 0x800) {
            utf8.set([0xc0 | (charcode >> 6), 0x80 | (charcode & 0x3f)], j);
            j += 2;
        }
        else if (charcode < 0xd800 || charcode >= 0xe000) {
            utf8.set([0xe0 | (charcode >> 12), 0x80 | ((charcode >> 6) & 0x3f), 0x80 | (charcode & 0x3f)], j);
            j += 3;
        }
        else {
            i++;
            // UTF-16 encodes 0x10000-0x10FFFF by
            // subtracting 0x10000 and splitting the
            // 20 bits of 0x0-0xFFFFF into two halves
            charcode = 0x10000 + (((charcode & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff));
            utf8.set([0xf0 | (charcode >> 18), 0x80 | ((charcode >> 12) & 0x3f), 0x80 | ((charcode >> 6) & 0x3f), 0x80 | (charcode & 0x3f)], j);
            j += 4;
        }
    }
    return utf8.subarray(0, j);
}
/**
 * Uint8Array/ArrayBuffer to utf-8 strring
 * @param _ua {Uint8Array} or ArrayBuffer
 * @returns {String} a UTF-8 encoded string
 */
function ua2utf8(_ua) {
    if (textDecoder) {
        // if _ua is undefined, imitate the JS implementation below which returns an empty string
        return _ua ? textDecoder.decode(_ua) : '';
    }
    let out, i, len, c, u;
    let char2, char3, char4;
    // avoid applying the Uint8Array constructor: on ArrayBuffer it creates a
    // view but on Uint8Array it creates a copy
    const array = ArrayBuffer.isView(_ua) ? _ua : new Uint8Array(_ua);
    out = '';
    len = array.length || array.byteLength;
    i = 0;
    while (i < len) {
        c = array[i++];
        switch (c >> 4) {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
                // 0xxxxxxx
                out += String.fromCharCode(c);
                break;
            case 12:
            case 13:
                // 110x xxxx   10xx xxxx
                char2 = array[i++];
                out += String.fromCharCode(((c & 0x1f) << 6) | (char2 & 0x3f));
                break;
            case 14:
                // 1110 xxxx  10xx xxxx  10xx xxxx
                char2 = array[i++];
                char3 = array[i++];
                out += String.fromCharCode(((c & 0x0f) << 12) | ((char2 & 0x3f) << 6) | ((char3 & 0x3f) << 0));
                break;
            case 15:
                // 1111 xxxx  10xx xxxx  10xx xxxx  10xx xxxx
                char2 = array[i++];
                char3 = array[i++];
                char4 = array[i++];
                u = ((c & 0x07) << 18) | ((char2 & 0x3f) << 12) | ((char3 & 0x3f) << 6) | (((char4 & 0x3f) << 0) - 0x10000);
                out += String.fromCharCode(0xd800 + (u >> 10));
                out += String.fromCharCode(0xdc00 + (u & 1023));
                break;
        }
    }
    return out;
}
function hex2ua(s) {
    const ua = new Uint8Array(s.length / 2);
    s = s.toLowerCase();
    for (let i = 0; i < s.length; i += 2) {
        ua[i / 2] =
            (s.charCodeAt(i) < 58 ? s.charCodeAt(i) - 48 : s.charCodeAt(i) - 87) * 16 +
                (s.charCodeAt(i + 1) < 58 ? s.charCodeAt(i + 1) - 48 : s.charCodeAt(i + 1) - 87);
    }
    return ua;
}
/**
 * Uint8Array/ArrayBuffer to hex String
 *
 * @param _ua {Uint8Array} or ArrayBuffer
 * @returns {String} Hex String
 */
function ua2hex(_ua) {
    let s = '';
    const ua = new Uint8Array(_ua);
    for (let i = 0; i < ua.length; i++) {
        const hhb = (ua[i] & 0xf0) >> 4;
        const lhb = ua[i] & 0x0f;
        s += String.fromCharCode(hhb > 9 ? hhb + 87 : hhb + 48);
        s += String.fromCharCode(lhb > 9 ? lhb + 87 : lhb + 48);
    }
    return s;
}
function b64_2uas(s) {
    const sliceSize = 1024;
    const byteCharacters = (0, ModelHelper_1.a2b)(s);
    const bytesLength = byteCharacters.length;
    const slicesCount = Math.ceil(bytesLength / sliceSize);
    const byteArrays = new Array(slicesCount);
    for (let sliceIndex = 0; sliceIndex < slicesCount; ++sliceIndex) {
        const begin = sliceIndex * sliceSize;
        const end = Math.min(begin + sliceSize, bytesLength);
        const bytes = new Array(end - begin);
        for (let offset = begin, i = 0; offset < end; ++i, ++offset) {
            bytes[i] = byteCharacters[offset].charCodeAt(0);
        }
        byteArrays[sliceIndex] = new Uint8Array(bytes);
    }
    return byteArrays;
}
function ua2b64Url(ua) {
    return (0, ModelHelper_1.ua2b64)(ua).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
}
function b64Url2ua(ua) {
    return (0, ModelHelper_1.ua2ab)((0, ModelHelper_1.b64_2ua)(ua.replace(/-/g, '+').replace(/_/g, '/').replace(/=/g, '') + (ua.length % 4 === 3 ? '=' : ua.length % 4 === 2 ? '==' : '')));
}
function hex2string(hexStr) {
    return (0, ModelHelper_1.ua2string)(hex2ua(hexStr));
}
function string2hex(text) {
    return ua2hex((0, ModelHelper_1.string2ua)(text));
}
function concat_uas(ua1, ua2) {
    const newBuffer = new Uint8Array(ua1.length + ua2.length);
    newBuffer.set(ua1, 0);
    newBuffer.set(ua2, ua1.length);
    return newBuffer;
}
//# sourceMappingURL=binary-utils.js.map