"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.appendDoubleVectorFromBuffer = exports.appendDoubleVectorFromJsArray = exports.memCopyFloat32 = exports.insertDoubleVectorFromJsArray = exports.SIZEOF_FLOAT32 = exports.SIZEOF_NUMBER = void 0;
var Guard_1 = require("../../Core/Guard");
var logger_1 = require("../logger");
exports.SIZEOF_NUMBER = 8;
exports.SIZEOF_FLOAT32 = 4;
/**
 * Inserts the values from the source Js array into the destination wasm array (and resizes) at index destIndex
 * @param wasmContext
 * @param source
 * @param dest
 * @param destIndex
 */
function insertDoubleVectorFromJsArray(wasmContext, source, dest, destIndex) {
    // Sanity checks
    Guard_1.Guard.notNull(wasmContext, "wasmContext");
    Guard_1.Guard.notNull(source, "source");
    Guard_1.Guard.notNull(dest, "dest");
    Guard_1.Guard.isTrue(destIndex >= 0, "destIndex must be greater than or equal to zero");
    Guard_1.Guard.isTrue(destIndex <= dest.size(), "destIndex must be less than or equal to dest.size()");
    var count = source.length;
    if (count === 0)
        return;
    var beforeSize = dest.size();
    // Resize the dest vector to fit memory
    dest.resizeFast(beforeSize + count);
    // Shift memory up for the insert
    if (destIndex < beforeSize) {
        wasmContext.SCRTMemMove(dest.dataPtr(destIndex + count), // Dest
        dest.dataPtr(destIndex), // Src
        (beforeSize - destIndex) * exports.SIZEOF_NUMBER); // count in bytes
    }
    // Overwrite dest memory with Js array
    // @ts-ignore
    wasmContext.HEAPF64.set(source, dest.dataPtr(destIndex) / exports.SIZEOF_NUMBER);
}
exports.insertDoubleVectorFromJsArray = insertDoubleVectorFromJsArray;
/**
 * Copies the values from the source Js array into the destination wasm array (and resizes) at index destIndex
 * @param wasmContext
 * @param source
 * @param dest
 * @param destIndex
 */
function memCopyFloat32(wasmContext, source, dest, destIndex) {
    // Sanity checks
    Guard_1.Guard.notNull(wasmContext, "wasmContext");
    Guard_1.Guard.notNull(source, "source");
    Guard_1.Guard.notNull(dest, "dest");
    Guard_1.Guard.isTrue(destIndex >= 0, "destIndex must be greater than or equal to zero");
    Guard_1.Guard.isTrue(destIndex <= dest.size() + source.length, "destIndex must be less than or equal to dest.size() + source.Length");
    var count = source.length;
    if (count === 0)
        return;
    // Overwrite dest memory with Js array
    // @ts-ignore
    wasmContext.HEAPF32.set(source, dest.dataPtr(destIndex) / exports.SIZEOF_FLOAT32);
}
exports.memCopyFloat32 = memCopyFloat32;
/**
 * Resizes the destinationVector by jsArray.length and copies the values into it
 * @param wasmContext
 * @param destinationVector
 * @param jsArray
 */
function appendDoubleVectorFromJsArray(wasmContext, destinationVector, jsArray) {
    var vectorSize = destinationVector.size();
    var arraySize = jsArray.length;
    var startPosition = vectorSize;
    var endPosition = startPosition + arraySize;
    try {
        // Resize without initializing values
        destinationVector.resizeFast(endPosition);
    }
    catch (error) {
        throw new Error(
        // @ts-ignore
        "Out of memory exception. Heap size: ".concat(wasmContext.HEAPF64.buffer.byteLength / (1024 * 1024), " mb"), 
        // @ts-ignore
        error);
    }
    // Get pointer to destination
    var bufferPointer = destinationVector.dataPtr(startPosition);
    // HEAPF64.set copies js array into buffer pointer
    // @ts-ignore
    wasmContext.HEAPF64.set(jsArray, bufferPointer / exports.SIZEOF_NUMBER);
}
exports.appendDoubleVectorFromJsArray = appendDoubleVectorFromJsArray;
/**
 * Resizes the destinationVector and copies the values into it
 * @param wasmContext
 * @param destinationVector
 * @param jsArray
 * @param experimentalMethod
 */
function appendDoubleVectorFromBuffer(wasmContext, destinationVector, jsArray) {
    try {
        var vectorSize = destinationVector.size();
        var arraySize = jsArray.byteLength / Float64Array.BYTES_PER_ELEMENT;
        var startPosition = vectorSize;
        var endPosition = startPosition + arraySize;
        // Resize without initializing values
        destinationVector.resizeFast(endPosition);
        // Get pointer to destination
        var bufferPointer = destinationVector.dataPtr(startPosition);
        // HEAPF64.set copies the provided ArrayBuffer data into buffer pointer
        // @ts-ignore
        var heap = wasmContext.HEAPF64;
        // @ts-ignore
        heap.set(jsArray, bufferPointer / exports.SIZEOF_NUMBER);
    }
    catch (e) {
        logger_1.Logger.log("Append Error", e);
        throw e;
    }
}
exports.appendDoubleVectorFromBuffer = appendDoubleVectorFromBuffer;
