import { defineStore } from "pinia";

export const useEvent = defineStore('event', () => {
    const events: Partial<Record<EventNames, Function[]>> = {};

    function Subscribe(name: EventNames, callback: Function) {
        events[name] = events[name] || [];
        events[name].push(callback);
    }
    
    function Publish(name: EventNames, ...args: any[]) {
        events[name]?.forEach(e => e(...args));
    }

    function Unsubscribe(name: EventNames, callback: Function) {
        events[name] = events[name]?.filter(e => e !== callback) || [];
    }

    return { Subscribe, Publish, Unsubscribe };
});

export type EventNames = 'submit' | 'update-texture' | 'update-config';