"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 Plan extends Primitive_1.Primitive {
    constructor(system) {
        super({
            i: ['a'],
            o: ['0', '1'],
        }, {}, system, _ids_1.ID_PLAN);
        this._current = undefined;
        this._looping = false;
    }
    onDataInputData(name, data) {
        if (!this._output[1].empty()) {
            this._forwarding_empty = true;
            this._output[1].pull();
            this._forwarding_empty = false;
        }
        this._current = data;
        this._forward_if_ready();
    }
    _forward_if_ready() {
        while (!this._forwarding &&
            !this._backwarding &&
            !this._looping &&
            this._current !== undefined &&
            this._i_invalid_count === 0) {
            this._looping = true;
            this._forwarding = true;
            this._output[0].push(this._current);
            this._forwarding = false;
        }
    }
    onDataInputDrop(name) {
        if (!this._backwarding) {
            this._current = undefined;
            this._looping = false;
            this._output[0].pull();
            this._output[1].pull();
        }
    }
    onDataInputInvalid(name) {
        this._looping = false;
        this._current = undefined;
        this._output[0].invalidate();
        this._output[1].invalidate();
    }
    onDataOutputDrop(name) {
        // console.log('Plan', 'onDataOutputDrop', name)
        if (name === '0') {
            if (this._current !== undefined) {
                this._forwarding = true;
                this._output[1].push(this._current);
                this._forwarding = false;
            }
        }
        else {
            this._looping = false;
        }
        this._backward_if_ready();
        this._forward_if_ready();
    }
    _backward_if_ready() {
        if (this._current !== undefined &&
            !this._forwarding &&
            !this._forwarding_empty &&
            !this._looping) {
            this._current = undefined;
            this._backward_all();
        }
    }
    snapshotSelf() {
        return Object.assign(Object.assign({}, super.snapshotSelf()), { _looping: this._looping, _current: this._current });
    }
    restoreSelf(state) {
        const { _looping, _current } = state, rest = __rest(state, ["_looping", "_current"]);
        super.restoreSelf(rest);
        this._looping = _looping;
        this._current = _current;
    }
}
exports.default = Plan;
//# sourceMappingURL=index.js.map