"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var ig_utilities_1 = require("@intelligentgraphics/ig.utilities");
exports.createSimpleApplicationState = function (initialState) {
    var listeners = ig_utilities_1.createLinkedList();
    var state = tslib_1.__assign({}, initialState);
    var getState = function () { return state; };
    var getValue = function (key) { return state[key]; };
    var setValue = function (key, value) {
        var _a;
        if (state[key] === value) {
            return;
        }
        state = tslib_1.__assign(tslib_1.__assign({}, state), (_a = {}, _a[key] = value, _a));
        callListeners(key, value);
    };
    var setState = function (update) {
        state = tslib_1.__assign(tslib_1.__assign({}, state), update);
        for (var key in update) {
            callListeners(key, update[key]);
        }
    };
    var callListeners = function (key, value) {
        return listeners.forEach(function (listener) { return listener(key, value); });
    };
    var onValueChange = function (cb) {
        listeners.push(cb);
        return function () {
            return listeners.consumeOne(function (stateListener) { return stateListener === cb; });
        };
    };
    return {
        getState: getState,
        getValue: getValue,
        setState: setState,
        setValue: setValue,
        onValueChange: onValueChange,
    };
};
