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

let platform_endianness = EndianType.LittleEndian;
let platform_endianness_inferred = false;

/**
 * Determines underlying native endianness type
 * Useful for knowing byte order in TypedArrays
 * @returns {EndianType}
 */
export function platform_compute_endianness() {

    if (platform_endianness_inferred) {
        return platform_endianness;
    }

    const buffer = new ArrayBuffer(2);

    const uint8 = new Uint8Array(buffer);
    const uint16 = new Uint16Array(buffer);

    uint8[0] = 0x13;

    if ((uint16[0] & 0xFF) === 0x13) {
        platform_endianness = EndianType.LittleEndian;
    } else {
        platform_endianness = EndianType.BigEndian;
    }

    platform_endianness_inferred = true;

    return platform_endianness;
}