import { Action } from "../../../src/core/process/undo/Action.js";
import { array_copy } from "../../../src/core/collection/array/array_copy.js";

export class ArrayCopyAction extends Action {
    /**
     * @template T
     * @param {ArrayLike<T>|T[]|Uint8Array|Float32Array} source
     * @param {ArrayLike<T>|T[]|Uint8Array|Float32Array} destination
     */
    constructor(source, destination) {
        super();

        this.__source = source;
        this.__destination = destination;

        /**
         *
         * @type {ArrayLike<T>|null}
         * @private
         */
        this.__restore_value = null;
    }

    async apply(context) {

        const CTOR = Object.getPrototypeOf(this.__destination).constructor;
        this.__restore_value = new CTOR(this.__destination.length);

        // remember old value
        array_copy(this.__destination, 0, this.__restore_value, 0, this.__destination.length);

        // copy data across
        array_copy(this.__source, 0, this.__destination, 0, this.__source.length);
    }

    async revert(context) {
        array_copy(this.__restore_value, 0, this.__destination, 0, this.__restore_value.length);
    }
}
