"use strict";
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._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);
        }
    }
    async get(name) {
        var _a;
        return (_a = this._state[name]) !== null && _a !== void 0 ? _a : this._defaultState[name];
    }
    async read() {
        return this._state;
    }
    write(data) {
        this._state = data;
        return;
    }
    async set(name, data, auto = false) {
        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 {
            ...super.snapshotSelf(),
            _state,
        };
    }
    restoreSelf(state) {
        const { _state, ...rest } = state;
        super.restoreSelf(rest);
        this._state = _state || this._defaultState;
    }
}
exports.Stateful = Stateful;
//# sourceMappingURL=Stateful.js.map