import { assert } from "../assert.js";

/**
 *
 * @param {number} byte_size
 * @param {boolean} [shared]
 * @return {ArrayBuffer|SharedArrayBuffer}
 */
export function makeArrayBuffer(byte_size, shared = false) {
    assert.isNonNegativeInteger(byte_size, 'size');
    assert.isBoolean(shared, 'shared');

    if (shared) {
        if (crossOriginIsolated) {
            return new SharedArrayBuffer(byte_size);
        } else {
            console.error(`SharedArrayBuffer not supported because origin is not isolated, defaulting to ArrayBuffer instead`);
        }
    }

    return new ArrayBuffer(byte_size);
}