import { ref } from 'vue';
import { useEventListener } from '@vueuse/core';
import { fileValidType } from '../../../utils/src/file.mjs';
import '../../../utils/index.mjs';
import { handlePasteEvent } from '../../../utils/src/pasteFile/index.mjs';

class PasteFileError extends Error {
  constructor(message) {
    super(message);
    this.name = "PasteFileError";
  }
}
const usePasteFile = (target, options = {}) => {
  const { accept, disabled = false, preventDefault = true } = options;
  const successCallbacks = /* @__PURE__ */ new Set();
  const errorCallbacks = /* @__PURE__ */ new Set();
  const isProcessing = ref(false);
  let isCleanedUp = false;
  const getDisabled = () => {
    if (isCleanedUp) return true;
    return typeof disabled === "boolean" ? disabled : disabled.value;
  };
  const onPaste = async (e) => {
    if (getDisabled()) return;
    if (preventDefault) {
      e.preventDefault();
    }
    isProcessing.value = true;
    try {
      const clipboardEvent = e;
      const files = await handlePasteEvent(clipboardEvent);
      if (files.length === 0) return;
      const filteredFiles = accept && accept !== "*" ? files.filter((file) => fileValidType(file, accept)) : files;
      if (filteredFiles.length === 0) return;
      if (successCallbacks.size > 0) {
        successCallbacks.forEach((callback) => {
          try {
            callback(filteredFiles);
          } catch (callbackError) {
            console.error("\u6267\u884C\u6210\u529F\u56DE\u8C03\u65F6\u51FA\u9519:", callbackError);
          }
        });
      }
    } catch (error) {
      handlePasteError(error);
    } finally {
      isProcessing.value = false;
    }
  };
  const handlePasteError = (error) => {
    const pasteError = error instanceof Error ? error : new PasteFileError(`\u7C98\u8D34\u6587\u4EF6\u5904\u7406\u5931\u8D25: ${String(error)}`);
    if (errorCallbacks.size > 0) {
      errorCallbacks.forEach((callback) => {
        try {
          callback(pasteError);
        } catch (callbackError) {
          console.error("\u6267\u884C\u9519\u8BEF\u56DE\u8C03\u65F6\u51FA\u9519:", callbackError);
        }
      });
    } else {
      console.error("\u7C98\u8D34\u6587\u4EF6\u5904\u7406\u5931\u8D25:", pasteError);
    }
  };
  useEventListener(target, "paste", onPaste);
  const cleanupAll = () => {
    if (isCleanedUp) return;
    successCallbacks.clear();
    errorCallbacks.clear();
    isCleanedUp = true;
  };
  return {
    // 添加成功回调，并返回移除该回调的函数
    onSuccess: (callback) => {
      if (isCleanedUp) {
        console.warn("usePasteFile\u5DF2\u6E05\u7406\uFF0C\u65E0\u6CD5\u6DFB\u52A0\u56DE\u8C03");
        return () => {
        };
      }
      successCallbacks.add(callback);
      return () => {
        successCallbacks.delete(callback);
      };
    },
    // 添加错误回调，并返回移除该回调的函数
    onError: (callback) => {
      if (isCleanedUp) {
        console.warn("usePasteFile\u5DF2\u6E05\u7406\uFF0C\u65E0\u6CD5\u6DFB\u52A0\u56DE\u8C03");
        return () => {
        };
      }
      errorCallbacks.add(callback);
      return () => {
        errorCallbacks.delete(callback);
      };
    },
    // 清理函数
    cleanup: cleanupAll,
    // 是否正在处理粘贴事件
    isProcessing
  };
};

export { usePasteFile };
//# sourceMappingURL=index.mjs.map
