export enum EventType {
    LevelInit = 'LevelInit',
    InputEnd = 'InputEnd',
}

type Handler = (...args: unknown[]) => void;

const listeners = new Map<EventType, Handler[]>();

export const EventManager = {
    register(type: EventType, handler: Handler) {
        if (!listeners.has(type)) listeners.set(type, []);
        const list = listeners.get(type)!;
        if (!list.includes(handler)) list.push(handler);
    },
    remove(type: EventType, handler: Handler) {
        const list = listeners.get(type);
        if (!list) return;
        const i = list.indexOf(handler);
        if (i >= 0) list.splice(i, 1);
    },
    dispatch(type: EventType, ...args: unknown[]) {
        const list = listeners.get(type);
        if (!list) return;
        for (const h of [...list]) h(...args);
    },
};
