import { seededRandom } from "../../../math/random/seededRandom.js";
import { array_shuffle } from "../array_shuffle.js";
import { AbstractArrayIterator } from "./AbstractArrayIterator.js";

export class ArrayIteratorRandom extends AbstractArrayIterator {
    constructor() {
        super();

        this.__i = 0;
        this.__sequence = [];

        this.__random = seededRandom(0);
    }

    /**
     *
     * @param {number} v
     */
    setSeed(v) {
        this.__random.setCurrentSeed(v);
    }

    initialize(data) {
        super.initialize(data);

        //initialize sequence
        const n = data.length;
        for (let i = 0; i < n; i++) {
            this.__sequence[i] = i;
        }

        array_shuffle(this.__random, this.__sequence);

        this.__i = 0;
    }

    next(result) {
        const sequence = this.__sequence;
        const n = sequence.length;

        if (this.__i >= n) {
            result.value = undefined;
            result.done = true;
        } else {
            const order = sequence[this.__i];

            result.value = this.__data[order];
            result.done = false;

            this.__i++;
        }

    }
}
