"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ua2string = ua2string;
exports.string2ua = string2ua;
exports.string2ab = string2ab;
exports.ua2ab = ua2ab;
exports.decodeStringOrArrayBuffer = decodeStringOrArrayBuffer;
exports.b64_2ab = b64_2ab;
exports.b64_2ua = b64_2ua;
exports.ua2b64 = ua2b64;
exports.b2a = b2a;
exports.a2b = a2b;
function ua2string(_ua) {
    let str = '';
    const ab = new Uint8Array(_ua);
    const abLen = ab.length;
    const CHUNK_SIZE = Math.pow(2, 8);
    let offset, len, subab;
    for (offset = 0; offset < abLen; offset += CHUNK_SIZE) {
        len = Math.min(CHUNK_SIZE, abLen - offset);
        subab = ab.subarray(offset, offset + len);
        str += String.fromCharCode.apply(null, subab);
    }
    return str;
}
function string2ua(s) {
    const ua = new Uint8Array(s.length);
    for (let i = 0; i < s.length; i++) {
        ua[i] = s.charCodeAt(i) & 0xff;
    }
    return ua;
}
function string2ab(s) {
    return ua2ab(string2ua(s));
}
// Get an array buffer matching the content of the input, copying only when necessary
function ua2ab(ua) {
    if (ua instanceof ArrayBuffer)
        return ua;
    const { buffer, byteOffset, byteLength } = ua;
    if (buffer instanceof ArrayBuffer) {
        if (byteOffset === 0 && byteLength === buffer.byteLength) {
            // zero-copy fast path
            return buffer;
        }
        return buffer.slice(byteOffset, byteOffset + byteLength);
    }
    // buffer is SharedArrayBuffer → must copy
    const copy = new Uint8Array(byteLength);
    copy.set(ua);
    return copy.buffer;
}
function decodeStringOrArrayBuffer(value) {
    if (typeof value === 'string') {
        return b64_2ab(value);
    }
    else if (value instanceof ArrayBuffer || ArrayBuffer.isView(value)) {
        return value;
    }
    else {
        throw new Error(`Invalid type : ${typeof value}`);
    }
}
function b64_2ab(s) {
    return ua2ab(string2ua(a2b(s)));
}
function b64_2ua(s) {
    return string2ua(a2b(s));
}
function ua2b64(_ua) {
    return b2a(ua2string(_ua));
}
function b2a(a) {
    if (typeof window !== 'undefined') {
        //Favour btoa in browser
        if (typeof btoa !== 'undefined') {
            return btoa(a);
        }
        if (typeof Buffer !== 'undefined') {
            const buf = Buffer.from(a, 'latin1');
            return buf.toString('base64');
        }
    }
    else {
        if (typeof Buffer !== 'undefined') {
            const buf = Buffer.from(a, 'latin1');
            return buf.toString('base64');
        }
        if (typeof btoa !== 'undefined') {
            return btoa(a);
        }
    }
    throw new Error('Unsupported operation b2a');
}
function a2b(s) {
    const urlUnsafeString = s.replace(/_/g, '/').replace(/-/g, '+');
    if (typeof window !== 'undefined') {
        //Favour atob in browser
        if (typeof atob !== 'undefined') {
            return atob(urlUnsafeString);
        }
        if (typeof Buffer !== 'undefined') {
            const buf = new Buffer(urlUnsafeString, 'base64');
            return buf.toString('latin1');
        }
    }
    else {
        if (typeof Buffer !== 'undefined') {
            const buf = new Buffer(urlUnsafeString, 'base64');
            return buf.toString('latin1');
        }
        if (typeof atob !== 'undefined') {
            return atob(urlUnsafeString);
        }
    }
    throw new Error('Unsupported operation a2b');
}
//# sourceMappingURL=ModelHelper.js.map