"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Stateful = void 0;
const MethodNotImplementedError_1 = require("../exception/MethodNotImplementedError");
const f_1 = __importDefault(require("../system/f/comparison/Equals/f"));
const _1 = require("./$");
const Component_1 = require("./Component");
class Stateful extends Component_1.Component__ {
    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) {
        void this.set(name, data, true);
    }
    onDataInputDrop(name) {
        this._set_from_input = false;
        if (!this._backwarding) {
            void this.set(name, undefined);
        }
    }
    onRefInputData(name, data) {
        super.onRefInputData(name, data);
        this._forwarding = true;
        void this.set(name, data);
        this._forwarding = false;
    }
    onRefInputDrop(name) {
        if (!this._backwarding) {
            void this.set(name, undefined);
        }
    }
    async get(name) {
        return this._state[name] ?? this._defaultState[name];
    }
    read(callback) {
        callback(this._state);
    }
    write(data, callback) {
        this._state = data;
        callback();
    }
    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();
    }
    setPinIgnored() {
        //
    }
    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(),
            ...(!(0, f_1.default)(this._state, this._defaultState) ? { _state } : {}),
        };
    }
    restoreSelf(state) {
        const { _state, ...rest } = state;
        super.restoreSelf(rest);
        this._state = _state || this._defaultState;
    }
}
exports.Stateful = Stateful;
//# sourceMappingURL=Stateful.js.map