export class KeyBoardListener {
    static _listenersKeyUp = {};
    static _listenersKeyDown = {};
    static ready = false;
    static _listenCount = 0;
    static init() {
        document.addEventListener("keydown", (event) => {
            for (const listener of Object.values(
                KeyBoardListener._listenersKeyDown,
            )) {
                listener(event);
            }
        });
        document.addEventListener("keyup", (event) => {
            for (const listener of Object.values(
                KeyBoardListener._listenersKeyUp,
            )) {
                listener(event);
            }
        });

        KeyBoardListener.ready = true;
    }

    /* return id used to remove the listener */
    static onKeyDown(callback) {
        const id = KeyBoardListener._listenCount++;
        KeyBoardListener._listenersKeyDown[id] = callback;
        return id;
    }

    static offKeyDown(id) {
        delete KeyBoardListener._listenersKeyDown[id];
    }

    /* return id used to remove the listener */
    static onKeyUp(callback) {
        const id = KeyBoardListener._listenCount++;
        KeyBoardListener._listenersKeyUp[id] = callback;
        return id;
    }

    static offKeyUp(id) {
        delete KeyBoardListener._listenersKeyUp[id];
    }
}
