import EventEmitter from "events";
import windowEvents from "./windowEvents";
import webviewEvents from "./webviewEvents";
import trayEvents from "./trayEvents";
import applicationEvents from "./ApplicationEvents";
import SparkBunEvent from "./event";

class SparkBunEventEmitter extends EventEmitter {
	constructor() {
		super();
	}

	// optionally pass in a specifier to make the event name specific.
	// eg: will-navigate is listened to globally for all webviews, but
	// will-navigate-1 is listened to for a specific webview with id 1
	emitEvent(
		SparkBunEvent: SparkBunEvent<any, any>,
		specifier?: number | string,
	) {
		if (specifier) {
			this.emit(`${SparkBunEvent.name}-${specifier}`, SparkBunEvent);
		} else {
			this.emit(SparkBunEvent.name, SparkBunEvent);
		}
	}

	events = {
		window: {
			...windowEvents,
		},
		webview: {
			...webviewEvents,
		},
		tray: {
			...trayEvents,
		},
		app: {
			...applicationEvents,
		},
	};
}

export const sparkBunEventEmitter = new SparkBunEventEmitter();

export default sparkBunEventEmitter;
