"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
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 });
exports.Stateful = void 0;
const MethodNotImplementedError_1 = require("../exception/MethodNotImplementedError");
const Primitive_1 = require("../Primitive");
const _1 = require("./$");
class Stateful extends Primitive_1.Primitive {
    constructor({ i = [], o = [] }, opt, system, id) {
        super({
            i,
            o,
        }, opt, system, id);
        this.__ = ['U', 'J', 'V'];
        this.stateful = true;
        this._defaultState = {};
        this._state = {};
        this._set_from_input = false;
    }
    subscribe(path, name, listener) {
        throw new MethodNotImplementedError_1.MethodNotImplementedError();
    }
    onDataInputData(name, data) {
        this.set(name, data, true);
    }
    onDataInputDrop(name) {
        this._set_from_input = false;
        if (!this._backwarding) {
            this.set(name, undefined);
        }
    }
    onRefInputData(name, data) {
        super.onRefInputData(name, data);
        this._forwarding = true;
        this.set(name, data);
        this._forwarding = false;
    }
    onRefInputDrop(name) {
        if (!this._backwarding) {
            this.set(name, undefined);
        }
    }
    get(name) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            return (_a = this._state[name]) !== null && _a !== void 0 ? _a : this._defaultState[name];
        });
    }
    read() {
        return __awaiter(this, void 0, void 0, function* () {
            return this._state;
        });
    }
    write(data) {
        this._state = data;
        return;
    }
    set(name, data, auto = false) {
        return __awaiter(this, void 0, void 0, function* () {
            this._set_from_input = auto;
            this._state[name] = data;
            this.emit('set', name, data);
            // @ts-ignore
            this.emit(name, data);
        });
    }
    hasKey(name) {
        throw new MethodNotImplementedError_1.MethodNotImplementedError();
    }
    delete(name) {
        delete this._state[name];
        return;
    }
    deepSet(path, data) {
        throw new MethodNotImplementedError_1.MethodNotImplementedError();
    }
    deepGet(path) {
        throw new MethodNotImplementedError_1.MethodNotImplementedError();
    }
    deepDelete(path) {
        throw new MethodNotImplementedError_1.MethodNotImplementedError();
    }
    keys() {
        throw new MethodNotImplementedError_1.MethodNotImplementedError();
    }
    snapshotSelf() {
        const _state = {};
        for (const key in this._state) {
            const value = this._state[key];
            if (value instanceof _1.$) {
                _state[key] = null;
            }
            else {
                _state[key] = value;
            }
        }
        return Object.assign(Object.assign({}, super.snapshotSelf()), { _state });
    }
    restoreSelf(state) {
        const { _state } = state, rest = __rest(state, ["_state"]);
        super.restoreSelf(rest);
        this._state = _state || this._defaultState;
    }
}
exports.Stateful = Stateful;
//# sourceMappingURL=Stateful.js.map