"use strict";
const common_vendor = require("../vendor.js");
const gen_Apis = require("../../gen/Apis.js");
const handlePreUpload = async (val) => {
  const res = await gen_Apis.Apis.Login.Auth.PreUpload(val);
  return res;
};
const avatarUpload = (path) => {
  return new Promise(
    (resolve, reject) => {
      if (path.startsWith("wxfile://")) {
        const match = path.match(/[^\/\\]+(?=\.[^\.]+$|$)/);
        const filePathName = match[0];
        const filePath = path;
        handlePreUpload({
          filename: filePathName,
          alc: "public-read"
        }).then((res) => {
          if (res == null ? void 0 : res.success) {
            const wxfs = common_vendor.index.getFileSystemManager();
            wxfs.readFile({
              filePath,
              complete(e) {
                console.log("complete", e);
              },
              success: function(fileRes) {
                var _a, _b;
                console.log(fileRes, "fileRes1");
                common_vendor.index.request({
                  url: (_a = res == null ? void 0 : res.data) == null ? void 0 : _a.url,
                  method: "PUT",
                  header: {
                    ...(_b = res == null ? void 0 : res.data) == null ? void 0 : _b.headers,
                    "x-amz-acl": "public-read"
                    // 明确指定 ACL
                  },
                  data: fileRes == null ? void 0 : fileRes.data,
                  success: function success(json) {
                    var _a2, _b2;
                    console.log(json, "resUpadte");
                    if (json.statusCode !== 200) {
                      common_vendor.index.showToast({
                        title: "上传失败",
                        icon: "none"
                      });
                      reject();
                    } else {
                      let data = {
                        url: (_b2 = (_a2 = res == null ? void 0 : res.data) == null ? void 0 : _a2.url) == null ? void 0 : _b2.split("?")[0],
                        status: "done",
                        type: "image/png",
                        uid: Date.now()
                      };
                      resolve([data]);
                    }
                  },
                  fail: function fail() {
                    common_vendor.index.showToast({
                      title: "上传失败",
                      icon: "none"
                    });
                    reject();
                  }
                });
                console.log(fileRes, "fileRes");
              }
            });
          }
        });
        return;
      }
      common_vendor.index.downloadFile({
        url: path,
        success: async (res) => {
          if (res.statusCode === 200) {
            const match = res.tempFilePath.match(/[^\/\\]+(?=\.[^\.]+$|$)/);
            const filePath = res.tempFilePath;
            const filePathName = match[0];
            console.log(match[0]);
            handlePreUpload({
              filename: filePathName,
              alc: "public-read"
            }).then((res2) => {
              if (res2 == null ? void 0 : res2.success) {
                const wxfs = common_vendor.index.getFileSystemManager();
                wxfs.readFile({
                  filePath,
                  complete(e) {
                    console.log("complete", e);
                  },
                  success: function(fileRes) {
                    var _a, _b;
                    console.log(fileRes, "fileRes1");
                    common_vendor.index.request({
                      url: (_a = res2 == null ? void 0 : res2.data) == null ? void 0 : _a.url,
                      method: "PUT",
                      header: {
                        ...(_b = res2 == null ? void 0 : res2.data) == null ? void 0 : _b.headers,
                        "x-amz-acl": "public-read"
                        // 明确指定 ACL
                      },
                      data: fileRes == null ? void 0 : fileRes.data,
                      success: function success(json) {
                        var _a2, _b2;
                        console.log(json, "resUpadte");
                        if (json.statusCode !== 200) {
                          common_vendor.index.showToast({
                            title: "上传失败",
                            icon: "none"
                          });
                          reject();
                        } else {
                          let data = {
                            url: (_b2 = (_a2 = res2 == null ? void 0 : res2.data) == null ? void 0 : _a2.url) == null ? void 0 : _b2.split("?")[0],
                            status: "done",
                            type: "image/png",
                            uid: Date.now()
                          };
                          resolve([data]);
                        }
                      },
                      fail: function fail() {
                        common_vendor.index.showToast({
                          title: "上传失败",
                          icon: "none"
                        });
                        reject();
                      }
                    });
                    console.log(fileRes, "fileRes");
                  }
                });
              }
            });
          } else {
            console.log(`文件下载失败，状态码: ${res.statusCode}`);
          }
        },
        fail: (err) => {
        }
      });
    }
  );
};
const upload = (count = 1, size = 2, mediaType = ["image"]) => {
  return new Promise(
    (resolve, reject) => {
      let ArrImgList = [];
      common_vendor.index.chooseMedia({
        count: count > 9 ? 9 : count,
        //默认9
        mediaType,
        sizeType: ["original", "compressed"],
        //可以指定是原图还是压缩图，默认二者都有
        success: async function(i) {
          var _a;
          console.log("filePath_i", i);
          (_a = i.tempFiles) == null ? void 0 : _a.map((k) => {
            console.log(k, "??kj");
            let match = k.tempFilePath.match(/[^\/\\]+(?=\.[^\.]+$|$)/);
            const filePath = k.tempFilePath;
            const extIndex = filePath.lastIndexOf(".");
            const filePathName = match[0];
            extIndex >= -1 ? filePath.substr(extIndex + 1) : "";
            const img_size = i.tempFiles[0].size;
            const max_size = 1024 * 1024 * (size || 2);
            if (img_size > max_size) {
              common_vendor.index.showToast({
                title: `${filePathName}文件大小不能超过${size}M`,
                icon: "none"
              });
              return false;
            }
            handlePreUpload({
              filename: filePathName,
              alc: "public-read"
            }).then((res) => {
              if (res == null ? void 0 : res.success) {
                const wxfs = common_vendor.index.getFileSystemManager();
                wxfs.readFile({
                  filePath,
                  complete(e) {
                    console.log("complete", e);
                  },
                  success: function(fileRes) {
                    var _a2, _b;
                    console.log(fileRes, "fileRes1");
                    common_vendor.index.request({
                      url: (_a2 = res == null ? void 0 : res.data) == null ? void 0 : _a2.url,
                      method: "PUT",
                      header: {
                        ...(_b = res == null ? void 0 : res.data) == null ? void 0 : _b.headers,
                        "x-amz-acl": "public-read"
                        // 明确指定 ACL
                      },
                      data: fileRes == null ? void 0 : fileRes.data,
                      success: function success(json) {
                        var _a3, _b2;
                        console.log(json, "resUpadte");
                        if (json.statusCode !== 200) {
                          common_vendor.index.showToast({
                            title: "上传失败",
                            icon: "none"
                          });
                          reject();
                        } else {
                          const timestamp = Date.now();
                          let data = {
                            url: (_b2 = (_a3 = res == null ? void 0 : res.data) == null ? void 0 : _a3.url) == null ? void 0 : _b2.split("?")[0],
                            status: "done",
                            type: `${k == null ? void 0 : k.fileType}/${filePath.split(".")[1]}`,
                            uid: timestamp,
                            size: k == null ? void 0 : k.size
                          };
                          ArrImgList == null ? void 0 : ArrImgList.push(data);
                          resolve(ArrImgList);
                        }
                      },
                      fail: function fail() {
                        common_vendor.index.showToast({
                          title: "上传失败",
                          icon: "none"
                        });
                        reject();
                      }
                    });
                    console.log(fileRes, "fileRes");
                  }
                });
              }
            });
          });
        }
      });
    }
  );
};
const uploadImgWatermark = (data) => {
  return new Promise(
    (fileResolve, fileReject) => {
      const timestamp = Date.now();
      const extIndex = data == null ? void 0 : data.path.lastIndexOf(".");
      const fileExt = extIndex >= -1 ? data == null ? void 0 : data.path.substr(extIndex + 1) : "";
      console.log(extIndex, fileExt, "fileExt");
      handlePreUpload({
        filename: `${(data == null ? void 0 : data.name) || ""}_${timestamp}.${fileExt}`,
        alc: "public-read"
      }).then((res) => {
        if (res == null ? void 0 : res.success) {
          const wxfs = common_vendor.index.getFileSystemManager();
          wxfs.readFile({
            filePath: (data == null ? void 0 : data.path) || "",
            success: function(fileRes) {
              var _a, _b;
              common_vendor.index.request({
                url: (_a = res == null ? void 0 : res.data) == null ? void 0 : _a.url,
                method: "PUT",
                header: {
                  ...(_b = res == null ? void 0 : res.data) == null ? void 0 : _b.headers,
                  "x-amz-acl": "public-read"
                },
                data: fileRes == null ? void 0 : fileRes.data,
                success: function success(json) {
                  if (json.statusCode !== 200) {
                    fileReject(new Error(`上传失败: ${json.statusCode}`));
                  } else {
                    common_vendor.index.getImageInfo({
                      src: data == null ? void 0 : data.path,
                      success: (imgInfo) => {
                        var _a2, _b2;
                        console.log(imgInfo, "imgInfo");
                        let imgJson = {
                          url: (_b2 = (_a2 = res == null ? void 0 : res.data) == null ? void 0 : _a2.url) == null ? void 0 : _b2.split("?")[0],
                          status: "done",
                          type: imgInfo == null ? void 0 : imgInfo.type,
                          uid: timestamp,
                          size: "",
                          name: `${data == null ? void 0 : data.name}-${timestamp}`
                        };
                        fileResolve(imgJson);
                      }
                    });
                  }
                },
                fail: function fail() {
                  fileReject(new Error("网络请求失败"));
                }
              });
            },
            fail: function() {
              fileReject(new Error("读取文件失败"));
            }
          });
        } else {
          fileReject(new Error("获取上传链接失败"));
        }
      }).catch((err) => {
        fileReject(err);
      });
    }
  );
};
exports.avatarUpload = avatarUpload;
exports.upload = upload;
exports.uploadImgWatermark = uploadImgWatermark;
