/**
 * 用于文件上传
 */
import useMessage from "./useMessage";
import onCosUpload from "./useCosUpload";

// type TConfig = {
//   imgMaxSize?: number;
//   videoMaxSize?: number;
// };
const useUpload = () => {
  const { onError } = useMessage();
  const { onStartUpload } = onCosUpload();

  // const max = {
  //   image: config.imgMaxSize || 0.1,
  //   video: config.videoMaxSize || 200,
  // };

  //获取文件最大大小
  // const getSize = (file: File): number => {
  //   const type = file.type;
  //   return max[type] * 1024 * 1024;
  // };

  //文件上传之前的检验
  const onFileBeforeRead = (file: File, accept: string) => {
    if (accept && !accept.includes(file.type)) {
      onError(`请上传${accept.replaceAll("image/", "")}格式图片`);
      return false;
    }
    return true;
  };

  //上传之后的
  const onFileAfterRead = (data: any) => {
    return new Promise((resolve, reject) => {
      onStartUpload(data.file)
        .then((res) => {
          // onNotify("上传成功");
          // reject(false);
          resolve(res);
        })
        .catch(() => {
          reject();
        });
    });
  };

  return {
    onFileBeforeRead,
    onFileAfterRead,
  };
};
export default useUpload;
