"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Primitive_1 = require("../../../../Primitive");
const _ids_1 = require("../../../_ids");
class Next extends Primitive_1.Primitive {
    constructor(system) {
        super({
            i: ['init', 'next'],
            o: ['current'],
        }, {}, system, _ids_1.ID_NEXT);
        this._current = undefined;
        this._next = undefined;
        this.addListener('reset', this._reset);
    }
    _reset() {
        this._current = undefined;
        this._next = undefined;
    }
    onDataInputData(name, data) {
        const { api: { window: { nextTick }, }, } = this.__system;
        if (name === 'init') {
            this._current = data;
            nextTick(() => {
                this._output.current.push(data);
            });
        }
        else if (name === 'next') {
            this._current = data;
            this._next = true;
            this._input.next.pull();
            nextTick(() => {
                this._output.current.push(data);
                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._input.init.pull();
            }
        }
    }
    onDataInputInvalid(name) {
        if (name === 'init') {
            this._reset();
        }
        this._output.current.invalidate();
    }
    snapshotSelf() {
        return {
            ...super.snapshotSelf(),
            ...(this._current !== undefined ? { _current: this._current } : {}),
            ...(this._next !== undefined ? { _next: this._next } : {}),
        };
    }
    restoreSelf(state) {
        const { _current, _next, ...rest } = state;
        super.restoreSelf(rest);
        this._current = _current;
        this._next = _next;
    }
}
exports.default = Next;
//# sourceMappingURL=index.js.map