import { createContext, useContext } from "react";

export type UndoRedoAction = {
    undo: () => void;
    do: () => void;
};

export type UndoRedoContextType = {
    addAction: (action: UndoRedoAction) => void;
    undo: () => void;
    redo: () => void;
    canUndo: boolean;
    canRedo: boolean;
    stackCount: { undo: number; redo: number };
    setShortcutKeys?: (undoKey?: string, redoKey?: string) => void;
};

export const UndoRedoContext = createContext<UndoRedoContextType | null>(null);

export const useUndoRedo = () => {
    const ctx = useContext(UndoRedoContext);
    if (!ctx) {
        throw new Error("useUndoRedo must be used inside an UndoRedoProvider");
    }
    return ctx;
};
