export default class State {
    constructor() {
        this.states = new Map();
    }

    get(key, defaultValue = null) {
        if (!this.states.has(key)) {
            this.states.set(key, defaultValue);
            return defaultValue;
        }
        return this.states.get(key);
    }

    set(key, value) {
        return this.states.set(key, value);
    }

    change(key, amount = 1) {
        let val = this.get(key, 0);
        val += amount;
        this.set(key, val);
    }

    serialize() {
        const entries = this.states.entries();
        const entrymap = [];
        for (let state of entries) {
            entrymap.push(state);
        }
        return entrymap;
    }

    deserialize(data) {
        this.states = new Map(data);
    }
}