import { jsx as _jsx } from "react/jsx-runtime";
import { RotateCcw, RotateCw } from "lucide-react";
import { toast } from "sonner";
// Helper function to filter operations based on historyMode and showOnlyMyChanges
function filterOperation(context, op) {
    const { editContext } = context;
    const filterBySession = editContext.showOnlyMyChanges;
    const currentItem = editContext.item?.descriptor;
    const historyMode = editContext.historyMode;
    // Session filter (showOnlyMyChanges)
    if (filterBySession && op.sessionId !== editContext.sessionId) {
        return false;
    }
    // Page-centric filter: only include operations for current page and language (any version)
    // EXCEPTION: delete-items operations are always included because after deleting an item,
    // the user navigates to parent but should still be able to undo the deletion
    if (historyMode === "page-centric" && currentItem && op.type !== "delete-items") {
        if (!op.mainItem ||
            op.mainItem.id !== currentItem.id ||
            op.mainItem.language !== currentItem.language) {
            return false;
        }
    }
    // Current-version filter: only include operations for current page and version
    if (historyMode === "current-version" && currentItem) {
        if (!op.mainItem ||
            op.mainItem.id !== currentItem.id ||
            op.mainItem.version !== currentItem.version) {
            return false;
        }
    }
    return true;
}
export function getUndoCommand() {
    return {
        id: "undo",
        label: "Undo",
        icon: _jsx(RotateCcw, { className: "h-4 w-4", strokeWidth: 1 }),
        keyBinding: {
            key: "z",
            ctrlOrMeta: true,
        },
        execute: async (context) => {
            if (context.data?.source === "keyboard" && !context.editContext.showOnlyMyChanges) {
                toast.warning("Undo with Ctrl+Z is disabled when viewing other users' changes. Use the Undo button in the History panel instead.");
                return;
            }
            // Use the operationId passed from the UI (from current React state)
            // This ensures we undo the exact operation the UI shows, avoiding stale ref issues
            const operationId = context.data?.operationId;
            if (operationId) {
                const targetOp = context.editContext.editHistory.find((x) => x.id === operationId && x.canUndo);
                const result = await context.editContext.operations.undo(operationId);
            }
            else {
                // Fallback: find the operation from the command context
                const opToUndo = context.editContext.editHistory.find((x) => x.canUndo && filterOperation(context, x));
                if (opToUndo) {
                    const result = await context.editContext.operations.undo(opToUndo.id);
                }
            }
        },
        disabled: (context) => {
            // Check for undoable operations respecting historyMode and session filters
            return (context.editContext.editHistory.length === 0 ||
                !context.editContext.editHistory.find((x) => x.canUndo && filterOperation(context, x)));
        },
    };
}
export function getRedoCommand() {
    return {
        id: "redo",
        label: "Redo",
        icon: _jsx(RotateCw, { className: "h-4 w-4", strokeWidth: 1 }),
        keyBinding: [
            {
                key: "y",
                ctrlOrMeta: true,
            },
            {
                key: "z",
                ctrlOrMeta: true,
                shift: true,
            },
        ],
        execute: async (context) => {
            if (context.data?.source === "keyboard" && !context.editContext.showOnlyMyChanges) {
                toast.warning("Redo with Ctrl+Y is disabled when viewing other users' changes. Use the Redo button in the History panel instead.");
                return;
            }
            // Use the operationId passed from the UI (from current React state)
            // This ensures we redo the exact operation the UI shows, avoiding stale ref issues
            const operationId = context.data?.operationId;
            if (operationId) {
                await context.editContext.operations.redo(operationId);
            }
            else {
                // Fallback: find the operation from the command context
                const opToRedo = [...context.editContext.editHistory]
                    .reverse()
                    .find((x) => x.canRedo && filterOperation(context, x));
                if (opToRedo) {
                    await context.editContext.operations.redo(opToRedo.id);
                }
            }
        },
        disabled: (context) => {
            // Check for redoable operations respecting historyMode and session filters
            return (context.editContext.editHistory.length === 0 ||
                !context.editContext.editHistory.find((x) => x.canRedo && filterOperation(context, x)));
        },
    };
}
//# sourceMappingURL=undo.js.map