class StateManager {
    constructor(initialState = {}) {
        this.listeners = new Set();
        this.state = new Proxy(initialState, {
            set: (target, property, value) => {
                const oldValue = target[property];
                target[property] = value;
                if (oldValue !== value) {
                    this.notify({ property, oldValue, newValue: value });
                }
                return true;
            }
        });
    }

    subscribe(listener) {
        this.listeners.add(listener);
        return () => this.listeners.delete(listener);
    }

    notify(change) {
        this.listeners.forEach(listener => listener(change));
    }

    setState(partialState) {
        Object.entries(partialState).forEach(([key, value]) => {
            this.state[key] = value;
        });
    }
}


const appState = new StateManager({
    balance: 0,
    transactions: []
});

export { StateManager, appState };