import { useHotkeys } from 'react-hotkeys-hook';
import { shallow } from 'zustand/shallow';
import { useGraphOperation } from './useGraphOperation';
import useGraphStore from './store';
import { GraphState } from './types';

const selector = (store: GraphState) => ({
    undo: store.undo,
    redo: store.redo
});

export function useKeyboardCtrl() {
    const { undo, redo } = useGraphStore(selector, shallow);
    const {
        handleCreateChildNodes,
        handleCreateSiblingNodes,
        handleDeleteNodes
    } = useGraphOperation();

    useHotkeys('tab', (e) => {
        e.preventDefault();
        handleCreateChildNodes();
    }, [handleCreateChildNodes]);

    useHotkeys('enter', (e) => {
        e.preventDefault();
        handleCreateSiblingNodes();
    }, [handleCreateSiblingNodes]);

    useHotkeys('ctrl+z', (e) => {
        e.preventDefault();
        undo();
    }, [undo]);

    useHotkeys('ctrl+y', (e) => {
        e.preventDefault();
        redo();
    }, [redo]);

    useHotkeys('delete', (e) => {
        e.preventDefault();
        handleDeleteNodes();
    }, [handleDeleteNodes]);
}