export const enum ChatChangedEvent {
    Start = 1,
    End,
}

export class ChatEventHandler {
    private static actions: {
        key: string;
        action: (e: ChatChangedEvent, chat: number) => void;
    }[] = [];

    public static registerOnChatChanged(
        vue: Vue,
        action: (e: ChatChangedEvent, chat: number) => void
    ) {
        const key = `${new Date().valueOf()}-${Math.random()}`;
        this.actions.push({ key, action });
        vue.$once("hook:beforeDestroy", () => {
            const t = this.actions.find((i) => i.key === key);
            if (t) {
                this.actions = this.actions.filter((i) => i !== t);
            }
        });
    }

    public static raiseChatChanged(e: ChatChangedEvent, chat: number) {
        for (const item of this.actions) {
            item.action(e, chat);
        }
    }
}