import { array_copy } from "../array_copy.js";
import { array_buffer_copy } from "./array_buffer_copy.js";


/**
 * Assumes arrays have the same type
 * @param {TypedArray|Float32Array|Uint8Array|Uint32Array} source
 * @param {TypedArray|Float32Array|Uint8Array|Uint32Array} destination
 */
export function typed_array_copy(source, destination) {
    const destination_size = destination.length;

    if (destination_size >= source.length) {

        destination.set(source, 0);

    } else if (source.constructor === destination.constructor) {

        // same type
        array_buffer_copy(
            source.buffer,
            source.byteOffset,
            destination.buffer,
            destination.byteOffset,
            Math.min(source.byteLength, destination.byteLength)
        );

    } else {

        // destination is smaller than source, crop source data
        array_copy(source, 0, destination, 0, destination_size);

    }
}
