"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
const Primitive_1 = require("../../../../Primitive");
const _ids_1 = require("../../../_ids");
class Iterate extends Primitive_1.Primitive {
    constructor(system) {
        super({
            i: ['init', 'next'],
            o: ['local', 'current'],
        }, {}, system, _ids_1.ID_ITERATE);
        this._current = undefined;
        this._next = undefined;
        this.addListener('reset', this._reset);
    }
    _reset() {
        this._current = undefined;
        this._next = undefined;
    }
    onDataInputData(name, data) {
        if (name === 'init') {
            this._current = data;
            this._output.local.push(data);
            this._output.current.push(data);
        }
        else if (name === 'next') {
            this._current = data;
            this._next = true;
            this._output.current.push(data);
            this._input.next.pull();
            if (this._output.local.empty()) {
                this._output.local.push(this._current);
                this._next = undefined;
            }
        }
    }
    onDataInputDrop(name) {
        if (!this._backwarding) {
            if (name === 'init') {
                this._reset();
                this._forward_all_empty();
                return;
            }
        }
    }
    onDataOutputDrop(name) {
        if (!this._forwarding_empty) {
            if (name === 'current') {
                this._output.local.pull();
                this._input.init.pull();
            }
            else if (name === 'local') {
                if (this._next === true) {
                    this._next = false;
                    this._output.local.push(this._current);
                }
            }
        }
    }
    onDataInputInvalid(name) {
        if (name === 'init') {
            this._reset();
            this._output.local.invalidate();
        }
        this._output.current.invalidate();
    }
    snapshotSelf() {
        return Object.assign(Object.assign({}, super.snapshotSelf()), { _current: this._current, _next: this._next });
    }
    restoreSelf(state) {
        const { _current, _next } = state, rest = __rest(state, ["_current", "_next"]);
        super.restoreSelf(rest);
        this._current = _current;
        this._next = _next;
    }
}
exports.default = Iterate;
//# sourceMappingURL=index.js.map