// /AIWK_COMMON/core/aiwk_state.js
// AIWK EXT4 v003
(function () {
    "use strict";

    const AIWK = window.AIWK;
    const data = {};

    AIWK.state = {
        data: data,
        get: function (key, fallbackValue) {
            if (Object.prototype.hasOwnProperty.call(data, key)) {
                return data[key];
            }
            return fallbackValue;
        },
        set: function (key, value) {
            data[key] = value;
            if (AIWK.events) {
                AIWK.events.emit("state:change", { key: key, value: value });
            }
            return value;
        },
        merge: function (key, patch) {
            const base = data[key] || {};
            data[key] = Object.assign({}, base, patch || {});
            if (AIWK.events) {
                AIWK.events.emit("state:change", { key: key, value: data[key] });
            }
            return data[key];
        }
    };
}());
