"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.base64Decode = exports.base64Encode = exports.base64ToHex = exports.hexToBase64 = exports.arrayBufferToBase64 = exports.base64ToArrayBuffer = exports.hexStringToArrayBuffer = exports.arrayBufferToHexString = exports.arrayBufferToString = exports.stringToArrayBuffer = exports.generateUUIDSync = exports.getSubtleCrypto = exports.isWebCryptoAvailable = exports.ieOrEdge = exports.getGlobalScope = void 0;
/* eslint-disable camelcase */
const libsodium_1 = require("./libsodium");
const SN_BASE64_VARIANT = libsodium_1.base64_variants.ORIGINAL;
/**
 * Returns `window` if available, or `global` if supported in environment.
 */
function getGlobalScope() {
    return window;
}
exports.getGlobalScope = getGlobalScope;
/**
 * Determines whether we are in an Internet Explorer or Edge environment
 * @access public
 */
function ieOrEdge() {
    return (typeof document !== 'undefined' && !!document.documentMode) || /Edge/.test(navigator.userAgent);
}
exports.ieOrEdge = ieOrEdge;
/**
 * Returns true if WebCrypto is available
 * @access public
 */
function isWebCryptoAvailable() {
    return !ieOrEdge() && getGlobalScope().crypto && !!getGlobalScope().crypto.subtle;
}
exports.isWebCryptoAvailable = isWebCryptoAvailable;
/**
 * Returns the WebCrypto instance
 * @access public
 */
function getSubtleCrypto() {
    if (!getGlobalScope().crypto) {
        throw Error('Could not obtain SubtleCrypto instance');
    }
    return getGlobalScope().crypto.subtle;
}
exports.getSubtleCrypto = getSubtleCrypto;
/**
 * Generates a UUID syncronously
 * @access public
 */
function generateUUIDSync() {
    const globalScope = getGlobalScope();
    const crypto = globalScope.crypto || globalScope.msCrypto;
    if (crypto) {
        const buf = new Uint32Array(4);
        crypto.getRandomValues(buf);
        let idx = -1;
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            idx++;
            const r = (buf[idx >> 3] >> ((idx % 8) * 4)) & 15;
            const v = c === 'x' ? r : (r & 0x3 | 0x8);
            return v.toString(16);
        });
    }
    else {
        let d = new Date().getTime();
        if (globalScope.performance && typeof globalScope.performance.now === "function") {
            d += performance.now(); // use high-precision timer if available
        }
        const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            const r = (d + Math.random() * 16) % 16 | 0;
            d = Math.floor(d / 16);
            return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
        });
        return uuid;
    }
}
exports.generateUUIDSync = generateUUIDSync;
/**
 * Converts a plain string into an ArrayBuffer
 * @param {string} string - A plain string
 */
async function stringToArrayBuffer(string) {
    await libsodium_1.ready;
    return libsodium_1.from_string(string);
}
exports.stringToArrayBuffer = stringToArrayBuffer;
/**
 * Converts an ArrayBuffer into a plain string
 * @param {ArrayBuffer} arrayBuffer
 */
async function arrayBufferToString(arrayBuffer) {
    await libsodium_1.ready;
    return libsodium_1.to_string(arrayBuffer);
}
exports.arrayBufferToString = arrayBufferToString;
/**
 * Converts an ArrayBuffer into a hex string
 * @param arrayBuffer
 */
async function arrayBufferToHexString(arrayBuffer) {
    await libsodium_1.ready;
    return libsodium_1.to_hex(Buffer.from(arrayBuffer));
}
exports.arrayBufferToHexString = arrayBufferToHexString;
/**
 * Converts a hex string into an ArrayBuffer
 * @access public
 * @param hex - A hex string
 */
async function hexStringToArrayBuffer(hex) {
    await libsodium_1.ready;
    return libsodium_1.from_hex(hex);
}
exports.hexStringToArrayBuffer = hexStringToArrayBuffer;
/**
 * Converts a base64 string into an ArrayBuffer
 * @param base64 - A base64 string
 */
async function base64ToArrayBuffer(base64) {
    await libsodium_1.ready;
    return libsodium_1.from_base64(base64, SN_BASE64_VARIANT);
}
exports.base64ToArrayBuffer = base64ToArrayBuffer;
/**
 * Converts an ArrayBuffer into a base64 string
 * @param buffer
 */
async function arrayBufferToBase64(arrayBuffer) {
    await libsodium_1.ready;
    return libsodium_1.to_base64(Buffer.from(arrayBuffer), SN_BASE64_VARIANT);
}
exports.arrayBufferToBase64 = arrayBufferToBase64;
/**
 * Converts a hex string into a base64 string
 * @param hex - A hex string
 */
async function hexToBase64(hex) {
    await libsodium_1.ready;
    return libsodium_1.to_base64(libsodium_1.from_hex(hex), SN_BASE64_VARIANT);
}
exports.hexToBase64 = hexToBase64;
/**
 * Converts a base64 string into a hex string
 * @param base64 - A base64 string
 */
async function base64ToHex(base64) {
    await libsodium_1.ready;
    return libsodium_1.to_hex(libsodium_1.from_base64(base64, SN_BASE64_VARIANT));
}
exports.base64ToHex = base64ToHex;
/**
 * Converts a plain string into base64
 * @param text - A plain string
 * @returns  A base64 encoded string
 */
async function base64Encode(text) {
    await libsodium_1.ready;
    return libsodium_1.to_base64(text, SN_BASE64_VARIANT);
}
exports.base64Encode = base64Encode;
/**
 * Converts a base64 string into a plain string
 * @param base64String - A base64 encoded string
 * @returns A plain string
 */
async function base64Decode(base64String) {
    await libsodium_1.ready;
    return libsodium_1.to_string(libsodium_1.from_base64(base64String, SN_BASE64_VARIANT));
}
exports.base64Decode = base64Decode;
//# sourceMappingURL=data:application/json;base64, 