import { toast } from "sonner";
function getHistoryEntries(context, favoritesOnly, options) {
    const browseHistory = context.editContext.browseHistory;
    if (!favoritesOnly)
        return browseHistory;
    const favorites = context.editContext.favorites;
    if (!favorites) {
        if (!options?.silent) {
            toast.info("Favorites not loaded yet");
        }
        return undefined;
    }
    const favoriteIds = new Set(favorites.map((favorite) => `${favorite.itemId}:${favorite.language}`));
    const filteredHistory = browseHistory.filter((historyItem) => favoriteIds.has(`${historyItem.id}:${historyItem.language}`));
    if (filteredHistory.length === 0) {
        if (!options?.silent) {
            toast.info("No favorite items found in browse history");
        }
        return undefined;
    }
    return filteredHistory;
}
async function navigateHistory(context, direction, favoritesOnly) {
    const currentItem = context.editContext.item;
    if (!currentItem)
        return;
    const historyToNavigate = getHistoryEntries(context, favoritesOnly);
    if (!historyToNavigate)
        return;
    const currentIndex = historyToNavigate.findIndex((entry) => entry.id === currentItem.id && entry.language === currentItem.language);
    const targetEntry = direction === "forward"
        ? currentIndex < historyToNavigate.length - 1
            ? historyToNavigate[currentIndex + 1]
            : undefined
        : currentIndex > 0
            ? historyToNavigate[currentIndex - 1]
            : undefined;
    if (!targetEntry)
        return;
    await context.editContext.loadItem({
        id: targetEntry.id,
        language: targetEntry.language,
        version: targetEntry.version || 0,
    }, { addToBrowseHistory: false });
}
export const toggleInsertModeCommand = {
    id: "toggleInsertMode",
    label: "Toggle Insert Mode",
    icon: null,
    keyBinding: {
        key: "Insert",
    },
    disabled: () => false,
    execute: async (context) => {
        context.editContext.setInsertMode((current) => !current);
    },
};
export const historyForwardCommand = {
    id: "historyForward",
    label: "History Forward",
    icon: null,
    keyBinding: {
        key: "ArrowLeft",
        alt: true,
    },
    disabled: (context) => {
        const currentItem = context.editContext.item;
        if (!currentItem)
            return true;
        return (context.editContext.browseHistory.findIndex((entry) => entry.id === currentItem.id &&
            entry.language === currentItem.language) >=
            context.editContext.browseHistory.length - 1);
    },
    execute: async (context) => {
        await navigateHistory(context, "forward", false);
    },
};
export const historyBackCommand = {
    id: "historyBack",
    label: "History Back",
    icon: null,
    keyBinding: {
        key: "ArrowRight",
        alt: true,
    },
    disabled: (context) => {
        const currentItem = context.editContext.item;
        if (!currentItem)
            return true;
        return (context.editContext.browseHistory.findIndex((entry) => entry.id === currentItem.id &&
            entry.language === currentItem.language) <= 0);
    },
    execute: async (context) => {
        await navigateHistory(context, "back", false);
    },
};
export const favoriteHistoryForwardCommand = {
    id: "favoriteHistoryForward",
    label: "Favorite History Forward",
    icon: null,
    keyBinding: {
        key: "ArrowLeft",
        alt: true,
        shift: true,
    },
    disabled: (context) => {
        const historyToNavigate = getHistoryEntries(context, true, {
            silent: true,
        });
        const currentItem = context.editContext.item;
        if (!historyToNavigate || !currentItem)
            return true;
        return (historyToNavigate.findIndex((entry) => entry.id === currentItem.id &&
            entry.language === currentItem.language) >=
            historyToNavigate.length - 1);
    },
    execute: async (context) => {
        await navigateHistory(context, "forward", true);
    },
};
export const favoriteHistoryBackCommand = {
    id: "favoriteHistoryBack",
    label: "Favorite History Back",
    icon: null,
    keyBinding: {
        key: "ArrowRight",
        alt: true,
        shift: true,
    },
    disabled: (context) => {
        const historyToNavigate = getHistoryEntries(context, true, {
            silent: true,
        });
        const currentItem = context.editContext.item;
        if (!historyToNavigate || !currentItem)
            return true;
        return (historyToNavigate.findIndex((entry) => entry.id === currentItem.id &&
            entry.language === currentItem.language) <= 0);
    },
    execute: async (context) => {
        await navigateHistory(context, "back", true);
    },
};
//# sourceMappingURL=keyboardCommands.js.map