"use strict";

exports.__esModule = true;
exports.useClipboardHandler = useClipboardHandler;
var _react = require("react");
function useClipboardHandler(state, containerRef) {
  const {
    cellInteraction,
    clipboard
  } = state;
  const handleCopy = (0, _react.useCallback)(e => {
    var _e$clipboardData;
    if (cellInteraction.selectedCells.size === 0) {
      return;
    }
    if (cellInteraction.isEditing) {
      return;
    }
    e.preventDefault();
    const tsv = clipboard.copy();
    (_e$clipboardData = e.clipboardData) == null || _e$clipboardData.setData('text/plain', tsv);
  }, [cellInteraction, clipboard]);
  const handleCut = (0, _react.useCallback)(e => {
    var _e$clipboardData2;
    if (cellInteraction.selectedCells.size === 0) {
      return;
    }
    if (cellInteraction.isEditing) {
      return;
    }
    if (state.updateItemDisabled) {
      return;
    }
    e.preventDefault();
    const tsv = clipboard.cut();
    (_e$clipboardData2 = e.clipboardData) == null || _e$clipboardData2.setData('text/plain', tsv);
  }, [cellInteraction, clipboard, state]);
  const handlePaste = (0, _react.useCallback)(e => {
    var _e$clipboardData3;
    if (!cellInteraction.focusedCell) {
      return;
    }
    if (cellInteraction.isEditing) {
      return;
    }
    if (state.updateItemDisabled) {
      return;
    }
    e.preventDefault();
    const rawData = ((_e$clipboardData3 = e.clipboardData) == null ? void 0 : _e$clipboardData3.getData('text/plain')) ?? '';
    if (!rawData) {
      return;
    }
    clipboard.paste(rawData);
  }, [cellInteraction, clipboard, state]);
  (0, _react.useEffect)(() => {
    const container = containerRef == null ? void 0 : 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