import { useEffect, useCallback } from 'react';
export function useClipboardHandler(state, containerRef) {
    const { cellInteraction, clipboard } = state;
    const handleCopy = useCallback((e) => {
        if (cellInteraction.selectedCells.size === 0) {
            return;
        }
        if (cellInteraction.isEditing) {
            return;
        }
        e.preventDefault();
        const tsv = clipboard.copy();
        e.clipboardData?.setData('text/plain', tsv);
    }, [cellInteraction, clipboard]);
    const handleCut = useCallback((e) => {
        if (cellInteraction.selectedCells.size === 0) {
            return;
        }
        if (cellInteraction.isEditing) {
            return;
        }
        if (state.updateItemDisabled) {
            return;
        }
        e.preventDefault();
        const tsv = clipboard.cut();
        e.clipboardData?.setData('text/plain', tsv);
    }, [cellInteraction, clipboard, state]);
    const handlePaste = useCallback((e) => {
        if (!cellInteraction.focusedCell) {
            return;
        }
        if (cellInteraction.isEditing) {
            return;
        }
        if (state.updateItemDisabled) {
            return;
        }
        e.preventDefault();
        const rawData = e.clipboardData?.getData('text/plain') ?? '';
        if (!rawData) {
            return;
        }
        clipboard.paste(rawData);
    }, [cellInteraction, clipboard, state]);
    useEffect(() => {
        const container = containerRef?.current;
        if (!container) {
            return;
        }
        container.addEventListener('copy', handleCopy);
        container.addEventListener('cut', handleCut);
        container.addEventListener('paste', handlePaste);
        return () => {
            container.removeEventListener('copy', handleCopy);
            container.removeEventListener('cut', handleCut);
            container.removeEventListener('paste', handlePaste);
        };
    }, [handleCopy, handleCut, handlePaste, containerRef]);
}
//# sourceMappingURL=useClipboardHandler.js.map