/**
 * When running tests the code executes in a React Native environment which apparently does not provide
 * crypto.getRandomValues().  This provides a fallback which uses Math.random() as a source for random
 * values.
 * See: https://github.com/uuidjs/uuid#getrandomvalues-not-supported
 */
if (globalThis.crypto?.getRandomValues == null) {
    let getRandomValues;
    if (typeof require === 'function') {
        const nodeCrypto = require('crypto');
        getRandomValues = (array) => {
            if (!isTypedArray(array) || array instanceof BigInt64Array || array instanceof BigUint64Array)
                throw Error('Expected number TypedArray.');
            // getRandomValues is defined as pseudo random, favoring performance:
            // https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
            // eslint-disable-next-line security/detect-pseudoRandomBytes
            array.set(nodeCrypto.pseudoRandomBytes(array.length));
            return array;
        };
    }
    else {
        getRandomValues = (array) => {
            if (!isTypedArray(array) || array instanceof BigInt64Array || array instanceof BigUint64Array)
                throw Error('Expected number TypedArray.');
            array.set(array.map(() => Math.floor(Math.random() * 256)));
            return array;
        };
    }
    if (globalThis.crypto == null) {
        globalThis.crypto = {};
    }
    globalThis.crypto.getRandomValues = getRandomValues;
}
export {};
function isTypedArray(val) {
    return ArrayBuffer.isView(val) && !(val instanceof DataView);
}
