class PasteFileError extends Error {
  constructor(message, originalError) {
    super(message);
    this.originalError = originalError;
    this.name = "PasteFileError";
  }
}
const generateFileName = (name, ext, prefix = "clipboard") => {
  const timestamp = Date.now();
  const random = Math.floor(Math.random() * 1e4);
  if (name) {
    const hasExtension = /\.[^.]+$/i.test(name);
    if (hasExtension) {
      return `${prefix}_${timestamp}_${random}_${name.split(".")[0]}${name.match(/\.[^.]+$/i)?.[0] || ""}`;
    }
    return `${name}${ext || ".png"}`;
  }
  return `${prefix}_${timestamp}_${random}${ext || ".png"}`;
};
const extractImagesFromHtml = (html) => {
  const urls = [];
  const imgRegex = /<img[^>]+src="([^">]+)"/gi;
  let match;
  while ((match = imgRegex.exec(html)) !== null) {
    if (match[1]) {
      urls.push(match[1]);
    }
  }
  return urls;
};
const fetchImageAsFile = async (url) => {
  try {
    const response = await fetch(url);
    if (!response.ok) {
      throw new Error(`\u83B7\u53D6\u56FE\u7247\u5931\u8D25: ${response.status} ${response.statusText}`);
    }
    const blob = await response.blob();
    let fileName = url.split("/").pop() || "";
    fileName = fileName.split("?")[0];
    if (!fileName || fileName.length < 3) {
      fileName = generateFileName(void 0, `.${blob.type.split("/")[1] || "png"}`, "image");
    } else {
      fileName = generateFileName(fileName, `.${blob.type.split("/")[1] || fileName.split(".").pop() || "png"}`, "image");
    }
    return new File([blob], fileName, { type: blob.type });
  } catch (error) {
    console.error("\u83B7\u53D6\u56FE\u7247\u6587\u4EF6\u5931\u8D25:", error);
    return null;
  }
};
const handleFileItem = (item) => {
  const file = item.getAsFile();
  if (!file) {
    return null;
  }
  const { name, type } = file;
  const ext = name.match(/\.[^.]+?$/i)?.[0] || `.${type.split("/")[1] || "bin"}`;
  const newName = generateFileName(name, ext);
  return new File([file], newName, { type });
};
const handleHtmlItem = async (item) => {
  try {
    const html = await new Promise((resolve, reject) => {
      try {
        item.getAsString(resolve);
      } catch (error) {
        reject(new PasteFileError("\u83B7\u53D6HTML\u5185\u5BB9\u5931\u8D25", error));
      }
    });
    const imageUrls = extractImagesFromHtml(html);
    if (imageUrls.length === 0) {
      return [];
    }
    const filePromises = imageUrls.map((url) => fetchImageAsFile(url));
    const files = await Promise.all(filePromises);
    return files.filter((file) => file !== null);
  } catch (error) {
    console.error("\u5904\u7406HTML\u56FE\u7247\u5F15\u7528\u5931\u8D25:", error);
    return [];
  }
};
const handleTextItem = async (item) => {
  try {
    const text = await new Promise((resolve, reject) => {
      try {
        item.getAsString(resolve);
      } catch (error) {
        reject(new PasteFileError("\u83B7\u53D6\u6587\u672C\u5185\u5BB9\u5931\u8D25", error));
      }
    });
    const urlRegex = /^(https?:\/\/.*\.(jpeg|jpg|gif|png|webp|bmp|svg))$/i;
    if (urlRegex.test(text.trim())) {
      const file = await fetchImageAsFile(text.trim());
      return file ? [file] : [];
    }
    return [];
  } catch (error) {
    console.error("\u5904\u7406\u6587\u672C\u5185\u5BB9\u5931\u8D25:", error);
    return [];
  }
};
const handlePasteEvent = async (e) => {
  try {
    const files = [];
    const items = e.clipboardData?.items;
    if (!items || items.length === 0) {
      return files;
    }
    const filePromises = [];
    for (const item of items) {
      if (item.kind === "file") {
        const file = handleFileItem(item);
        if (file) {
          files.push(file);
        }
      } else if (item.type === "text/html") {
        filePromises.push(handleHtmlItem(item));
      } else if (item.type === "text/plain") {
        filePromises.push(handleTextItem(item));
      }
    }
    if (filePromises.length > 0) {
      const results = await Promise.all(filePromises);
      for (const result of results) {
        if (result) {
          if (Array.isArray(result)) {
            files.push(...result);
          } else {
            files.push(result);
          }
        }
      }
    }
    return files;
  } catch (error) {
    console.error("\u7C98\u8D34\u6587\u4EF6\u5904\u7406\u5931\u8D25:", error);
    throw new PasteFileError("\u7C98\u8D34\u6587\u4EF6\u5904\u7406\u5931\u8D25", error);
  }
};

export { PasteFileError, generateFileName, handlePasteEvent };
//# sourceMappingURL=index.mjs.map
