import appContext from "../../components/app_context";
import FNote from "../../entities/fnote";
import link_context_menu from "../../menus/link_context_menu";
import { escapeHtml, isCtrlKey } from "../../services/utils";
import { LaunchBarActionButton, useLauncherIconAndTitle } from "./launch_bar_widgets";

export function CustomNoteLauncher({ launcherNote, getTargetNoteId, getHoistedNoteId }: {
    launcherNote: FNote,
    getTargetNoteId: (launcherNote: FNote) => string | null | Promise<string | null>,
    getHoistedNoteId?: (launcherNote: FNote) => string | null
}) {
    const { icon, title } = useLauncherIconAndTitle(launcherNote);

    async function launch(evt: MouseEvent) {
        if (evt.which === 3) {
            return;
        }

        const targetNoteId = await getTargetNoteId(launcherNote);
        if (!targetNoteId) return;

        const hoistedNoteIdWithDefault = getHoistedNoteId?.(launcherNote) || appContext.tabManager.getActiveContext()?.hoistedNoteId;
        const ctrlKey = isCtrlKey(evt);

        if ((evt.which === 1 && ctrlKey) || evt.which === 2) {
            const activate = evt.shiftKey ? true : false;
            await appContext.tabManager.openInNewTab(targetNoteId, hoistedNoteIdWithDefault, activate);
        } else {
            await appContext.tabManager.openInSameTab(targetNoteId);
        }
    }

    return (
        <LaunchBarActionButton
            icon={icon}
            text={escapeHtml(title)}
            onClick={launch}
            onAuxClick={launch}
            onContextMenu={async evt => {
                evt.preventDefault();
                const targetNoteId = await getTargetNoteId(launcherNote);
                if (targetNoteId) {
                    link_context_menu.openContextMenu(targetNoteId, evt);
                }
            }}
        />
    )
}
