import { assert } from "../../../assert.js";
import { BinaryDataType } from "../../../binary/type/BinaryDataType.js";

/**
 *
 * @param {*} array
 * @returns {BinaryDataType}
 */
export function compute_binary_data_type_from_typed_array(array) {
    assert.notNull(array, 'array');
    assert.isObject(array, 'array');
    assert.isArrayLike(array, 'array');

    const ctor = Object.getPrototypeOf(array).constructor;

    switch (ctor) {
        case Uint8Array:
        case Uint8ClampedArray:
            return BinaryDataType.Uint8;
        case Uint16Array:
            return BinaryDataType.Uint16;
        case Uint32Array:
            return BinaryDataType.Uint32;

        case Int8Array:
            return BinaryDataType.Int8;
        case Int16Array:
            return BinaryDataType.Int16;
        case Int32Array:
            return BinaryDataType.Int32;

        case Float16Array:
            return BinaryDataType.Float16;
        case Float32Array:
            return BinaryDataType.Float32;
        case Array:
        // fall-through, as JS Array is dynamically typed per element, consider it to be the largest numeric type - Float64, which is also the default numeric JS type
        case Float64Array:
            return BinaryDataType.Float64;
        default:
            throw new Error(`unsupported constructor type ${ctor.name}`);
    }
}
