'use strict';

var index = require('../../components/src/plugins/message/index.js');
var jsMd5 = require('js-md5');
var elementPlus = require('element-plus');

async function urlToBlob(url) {
  const response = await fetch(url);
  return response.blob();
}
async function urlToFile(url, filename, options) {
  const blob = await urlToBlob(url);
  return new File([blob], filename, { type: options?.mimeType ?? blob.type });
}
function base64ToFile(base64, filename, options) {
  const bolb = base64ToBlob(base64, { mimeType: options?.mimeType });
  return blobToFile(bolb, filename);
}
function fileToBase64(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.onloadend = () => resolve(reader.result);
    reader.onerror = reject;
    reader.readAsDataURL(file);
  });
}
function blobToBase64(blob) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.onloadend = () => resolve(reader.result);
    reader.onerror = reject;
    reader.readAsDataURL(blob);
  });
}
function fileToBlob(file, options) {
  return new Blob([file], { type: options?.mimeType ?? file.type });
}
function blobToFile(blob, filename, options) {
  return new File([blob], filename, { type: options?.mimeType ?? blob.type });
}
function base64ToBlob(base64, options) {
  const matches = base64.match(/^data:(.+);base64,(.*)$/);
  if (!matches || matches.length !== 3) {
    throw new Error("Invalid Base64 string");
  }
  const mimeType = options?.mimeType ?? matches[1];
  const byteString = atob(matches[2]);
  const arrayBuffer = new ArrayBuffer(byteString.length);
  const intArray = new Uint8Array(arrayBuffer);
  for (let i = 0; i < byteString.length; i++) {
    intArray[i] = byteString.charCodeAt(i);
  }
  return new Blob([intArray], { type: mimeType });
}
const canvasToBlob = (canvas, type, quality) => {
  type = type ?? "image/png";
  return new Promise((resolve, reject) => {
    canvas.toBlob(
      (blob) => {
        if (!blob) {
          return reject();
        }
        resolve(blob);
      },
      type,
      quality
    );
  });
};
function fileToUrl(file) {
  return URL.createObjectURL(file);
}
function fileValidType(file, validTypes) {
  const fileType = file.type || "";
  const fileExtension = file.name.slice(Math.max(0, file.name.lastIndexOf(".")));
  const acceptTypes = validTypes.split(",").map((type) => type.trim());
  return acceptTypes.some((type) => {
    if (type.startsWith(".")) {
      return fileExtension.toLowerCase() === type.toLowerCase();
    } else if (type.includes("/*")) {
      const [mainType] = type.split("/");
      return fileType.startsWith(`${mainType}/`);
    } else {
      return fileType === type;
    }
  });
}
const fileMd5 = (file) => {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.onload = (e) => {
      try {
        const binary = e.target.result;
        if (binary) {
          resolve(jsMd5.md5(binary));
        } else {
          reject(new Error("\u8BFB\u53D6\u6587\u4EF6\u5185\u5BB9\u5931\u8D25"));
        }
      } catch (error) {
        reject(new Error(`\u8BA1\u7B97MD5\u5931\u8D25: ${error instanceof Error ? error.message : String(error)}`));
      }
    };
    reader.onerror = () => {
      reject(new Error("\u8BFB\u53D6\u6587\u4EF6\u5931\u8D25"));
    };
    reader.readAsArrayBuffer(file);
  });
};
function getImageInfo(source, options) {
  return new Promise((resolve, reject) => {
    const img = new Image();
    let url;
    let shouldRevokeUrl = false;
    if (source instanceof File) {
      url = URL.createObjectURL(source);
      shouldRevokeUrl = true;
    } else {
      url = source;
    }
    img.onload = () => {
      if (shouldRevokeUrl) {
        URL.revokeObjectURL(url);
      }
      resolve(img);
    };
    img.onerror = () => {
      if (shouldRevokeUrl) {
        URL.revokeObjectURL(url);
      }
      if (!options?.hideMessage) {
        index.GmMessage.error("\u56FE\u7247\u4FE1\u606F\u83B7\u53D6\u5931\u8D25");
      }
      reject();
    };
    img.src = url;
  });
}
const getVideoInfo = (url) => {
  return new Promise((resolve, reject) => {
    const video = document.createElement("video");
    video.onloadedmetadata = () => resolve({ width: video.videoWidth, height: video.videoHeight, duration: video.duration });
    video.onerror = reject;
    video.src = url;
  });
};

Object.defineProperty(exports, "genFileId", {
  enumerable: true,
  get: function () { return elementPlus.genFileId; }
});
exports.base64ToBlob = base64ToBlob;
exports.base64ToFile = base64ToFile;
exports.blobToBase64 = blobToBase64;
exports.blobToFile = blobToFile;
exports.canvasToBlob = canvasToBlob;
exports.fileMd5 = fileMd5;
exports.fileToBase64 = fileToBase64;
exports.fileToBlob = fileToBlob;
exports.fileToUrl = fileToUrl;
exports.fileValidType = fileValidType;
exports.getImageInfo = getImageInfo;
exports.getVideoInfo = getVideoInfo;
exports.urlToBlob = urlToBlob;
exports.urlToFile = urlToFile;
//# sourceMappingURL=file.js.map
