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

/**
 * Copy data from one array to another
 * Follows Java System.arraycopy interface
 * @template T
 * @param {T[]|ArrayLike<T>|TypedArray|Uint8ClampedArray|Uint8Array|Uint32Array|Int32Array|Float32Array|Float64Array} source The source array
 * @param {number} source_position Starting position where to copy from inside the source array
 * @param {T[]|ArrayLike<T>|TypedArray|Uint8ClampedArray|Uint8Array|Uint32Array|Int32Array|Float32Array|Float64Array} destination The destination array
 * @param {number} destination_position Starting position where to copy to inside the destination array
 * @param {number} length How many elements should be copied
 */
export function array_copy(
    source,
    source_position,
    destination,
    destination_position,
    length
) {
    assert.isNonNegativeInteger(source_position, 'source_position');
    assert.isNonNegativeInteger(destination_position, 'destination_position');
    assert.isNonNegativeInteger(length, 'length');

    let i, j, k;

    for (k = 0; k < length; k++) {
        i = source_position + k;
        j = destination_position + k;

        destination[j] = source[i];
    }
}
