import {MenuItem, subMenu} from "../menus/Menu";

export class EventBus<DetailType = any> {
    private eventTarget: EventTarget;

    constructor(name = "") {
        this.eventTarget = document.appendChild(document.createComment(name));
    }

    on(type: TEventBus, listener: (event: CustomEvent<DetailType>) => void) {
        if (type ==="loaded-protyle") {
            console.warn("0.8.8 将移除 loaded-protyle, 请使用 loaded-protyle-static 进行替代");
        }
        this.eventTarget.addEventListener(type, listener);
    }

    once(type: TEventBus, listener: (event: CustomEvent<DetailType>) => void) {
        this.eventTarget.addEventListener(type, listener, {once: true});
    }

    off(type: TEventBus, listener: (event: CustomEvent<DetailType>) => void) {
        this.eventTarget.removeEventListener(type, listener);
    }

    emit(type: TEventBus, detail?: DetailType) {
        return this.eventTarget.dispatchEvent(new CustomEvent(type, {detail, cancelable: true}));
    }
}

export const emitOpenMenu = (options: {
    plugins: import("./index").Plugin[],
    type: TEventBus,
    detail: any,
    separatorPosition?: "top" | "bottom",
}) => {
    const pluginSubMenu = new subMenu();
    options.detail.menu = pluginSubMenu;
    options.plugins.forEach((plugin) => {
        plugin.eventBus.emit(options.type, options.detail);
    });
    if (pluginSubMenu.menus.length > 0) {
        if (options.separatorPosition === "top") {
            window.siyuan.menus.menu.append(new MenuItem({type: "separator"}).element);
        }
        window.siyuan.menus.menu.append(new MenuItem({
            label: window.siyuan.languages.plugin,
            icon: "iconPlugin",
            type: "submenu",
            submenu: pluginSubMenu.menus,
        }).element);
        if (options.separatorPosition === "bottom") {
            window.siyuan.menus.menu.append(new MenuItem({type: "separator"}).element);
        }
    }
};
