import { createLinkedList } from "@intelligentgraphics/ig.utilities";
export const createSimpleApplicationState = (initialState) => {
    const listeners = createLinkedList();
    let state = {
        ...initialState,
    };
    const getState = () => state;
    const getValue = (key) => state[key];
    const setValue = (key, value) => {
        if (state[key] === value) {
            return;
        }
        state = {
            ...state,
            [key]: value,
        };
        callListeners(key, value);
    };
    const setState = (update) => {
        state = {
            ...state,
            ...update,
        };
        for (const key in update) {
            callListeners(key, update[key]);
        }
    };
    const callListeners = (key, value) => listeners.forEach(listener => listener(key, value));
    const onValueChange = (cb) => {
        listeners.push(cb);
        return () => listeners.consumeOne(stateListener => stateListener === cb);
    };
    return {
        getState,
        getValue,
        setState,
        setValue,
        onValueChange,
    };
};
