"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Primitive_1 = require("../../../../Primitive");
const _ids_1 = require("../../../_ids");
class Default extends Primitive_1.Primitive {
    constructor(system) {
        super({ i: ['a', 'd'], o: ['a'] }, {}, system, _ids_1.ID_DEFAULT);
        this._current = undefined;
        this._reset = () => {
            this._current = undefined;
        };
        this.addListener('reset', this._reset);
    }
    onDataInputData(name, data) {
        if (name === 'a') {
            this._current = data;
            if (!this._input.d.empty()) {
                this._invalidate();
            }
        }
        else if (name === 'd') {
            if (this._input.a.empty()) {
                this._current = data;
            }
        }
        this._forward_if_ready();
    }
    _forward_if_ready() {
        while (!this._forwarding &&
            !this._backwarding &&
            this._o_active.size - this._o_invalid.size === 0 &&
            this._current !== undefined) {
            this._forward('a', this._current);
        }
    }
    _done() {
        this._backwarding = true;
        if (this._input.a.empty()) {
            this._input.d.pull();
        }
        else {
            this._input.a.pull();
        }
        this._backwarding = false;
    }
    onDataInputDrop(name) {
        if (name === 'a') {
            if (!this._input.d.empty()) {
                this._current = this._input.d.peak();
                this._invalidate();
                this._forward_if_ready();
            }
            else {
                this._current = undefined;
                this._forward_all_empty();
            }
        }
        else {
            if (this._input.a.empty()) {
                this._current = undefined;
                this._forward_all_empty();
            }
        }
    }
    onDataOutputDrop(name) {
        this._current = undefined;
        this._done();
        if (!this._input.a.empty()) {
            this._forward_if_ready();
        }
        else if (!this._input.d.empty()) {
            this._forward_if_ready();
        }
    }
    onDataInputInvalid(name) {
        this._invalidate();
    }
    snapshotSelf() {
        return {
            ...super.snapshotSelf(),
            ...(this._current !== undefined ? { _current: this._current } : {}),
        };
    }
    restoreSelf(state) {
        const { _current, ...rest } = state;
        super.restoreSelf(rest);
        this._current = _current;
    }
}
exports.default = Default;
//# sourceMappingURL=index.js.map