import { useCallback, ClipboardEventHandler } from 'react';
import { getDataTransferFiles } from '../utils/dom';
import { readClipboardImage, isTauri, isLinux } from '../utils/tauri';

export const useFilePasteHandler = (onPaste: (file: File[]) => void): ClipboardEventHandler =>
  useCallback(
    async (evt) => {
      const files = getDataTransferFiles(evt.clipboardData);
      if (files && files.length > 0) {
        onPaste(files);
        return;
      }

      // On Linux with Tauri, browser clipboard API doesn't work for images
      // Use our custom Tauri command with arboard/Wayland support
      if (isTauri() && isLinux()) {
        const clipboardImage = await readClipboardImage();
        if (clipboardImage) {
          onPaste([clipboardImage]);
        }
      }
    },
    [onPaste]
  );
