"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const React = require("react");
const sdk = require("../lib/cos/sdk.js");
const useAppState = require("../../../lib/useAppState.js");
const toast = require("react-hot-toast");
const emotionReactJsxRuntime_browser_esm = require("../../../node_modules/.pnpm/@emotion_react@11.10.4_q4mq7tnhdumujvomvw24zx33y4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js");
const upload = require("../../../node_modules/.pnpm/@arco-design_web-react@2.40.1_biqbaboplfbrettd7655fr4n2y/node_modules/@arco-design/web-react/es/Upload/upload.js");
const modal = require("../../../node_modules/.pnpm/@arco-design_web-react@2.40.1_biqbaboplfbrettd7655fr4n2y/node_modules/@arco-design/web-react/es/Modal/modal.js");
const Upload = ({
  limit = 12,
  name,
  value,
  type,
  ...props
}) => {
  ({
    placeholder: `\u8BF7\u9009\u62E9${props.label || props.title}`,
    ...type === "images" ? {
      maxTagCount: 20,
      mode: "multiple"
    } : {},
    ...props
  });
  const [fileList, setFileList] = React.useState(Array.isArray(value) ? value.map((item) => ({
    url: getReplaceURL(item.url || item)
  })) : value ? [{
    url: getReplaceURL(value)
  }] : []);
  return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(React.Fragment, {
    children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(
      upload,
      {
        limit,
        multiple: limit === 1 ? false : true,
        listType: "picture-card",
        fileList,
        onChange: (fileList2, file) => {
          setFileList(fileList2);
          const values = fileList2.map((item) => ({
            url: item.response || item.url || URL.createObjectURL(item.originFile)
          }));
          console.log("values", values);
          props.onChange && props.onChange(limit === 1 ? fileList2[0]["response"] : fileList2.map((item) => ({
            url: item.response || item.url || URL.createObjectURL(item.originFile)
          })));
        },
        onRemove: (file) => {
          return new Promise((resolve, reject) => {
            modal.confirm({
              title: "onRemove",
              content: `\u786E\u8BA4\u5220\u9664${file.name || "\u8BE5\u56FE\u7247"}\uFF1F`,
              onConfirm: () => {
                sdk.deleteMedia(file.url);
                resolve(true);
              },
              onCancel: () => reject("cancel")
            });
          });
        },
        onPreview: (file) => {
          modal.info({
            title: "\u9884\u89C8",
            content: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
              style: {
                textAlign: "center"
              },
              children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("img", {
                style: {
                  maxWidth: "100%",
                  margin: "0 auto"
                },
                src: (file == null ? void 0 : file.url) || (file == null ? void 0 : file.originFile) && URL.createObjectURL(file.originFile)
              })
            })
          });
        },
        customRequest: async (option) => {
          const {
            onProgress,
            onError,
            onSuccess,
            file
          } = option;
          console.log("file-->", file);
          sdk.cos.putObject({
            Bucket: useAppState.cosConfig.Bucket,
            Region: useAppState.cosConfig.Region,
            Key: file.name,
            FileSize: file.size,
            Body: file,
            onProgress: function(info) {
              const percent = parseInt(info.percent * 1e4) / 100;
              const speed = parseInt(info.speed / 1024 / 1024 * 100) / 100;
              console.log("\u8FDB\u5EA6\uFF1A" + percent + "%; \u901F\u5EA6\uFF1A" + speed + "Mb/s;");
              onProgress(percent, info);
            },
            onFileFinish: function(err, data, options) {
              console.log(options.Key + "\u4E0A\u4F20" + (err ? "\u5931\u8D25" : "\u5B8C\u6210"));
            }
          }, (err, data) => {
            var _a;
            console.log("requestCallback-->", err, data);
            if (err && ((_a = err[0]) == null ? void 0 : _a.error)) {
              onError(new Error("\u3010\u6587\u4EF6\u4E0A\u4F20\u5931\u8D25\u3011\u4E2D\u65AD\u4E0A\u4F20"));
              toast.toast.error("\u6587\u4EF6\u4E0A\u4F20\u5931\u8D25");
            } else if (err) {
              onError(new Error("\u3010\u6587\u4EF6\u4E0A\u4F20\u5931\u8D25\u3011\u7F51\u7EDC\u6216\u5176\u4ED6\u9519\u8BEF"));
              toast.toast.error("\u6587\u4EF6\u4E0A\u4F20\u5931\u8D25");
            } else {
              console.log("--ok--", data == null ? void 0 : data.Location);
              onSuccess("https://" + (data == null ? void 0 : data.Location), 200);
              toast.toast.success("\u5DF2\u6210\u529F\u4E0A\u4F20");
            }
          });
        }
      }
    )
  });
};
function getReplaceURL(item) {
  let trimBucket = "6f6e-one-4gipvd7sc515a40b-1309259962";
  let searchValue = `cloud://one-4gipvd7sc515a40b.${trimBucket}`;
  let replaceValue = `https://${trimBucket}.tcb.qcloud.la`;
  if (/^cloud:\/\//.test(item)) {
    item = item.replace(new RegExp(searchValue, "g"), replaceValue);
  }
  return item;
}
exports.Upload = Upload;
//# sourceMappingURL=Upload.js.map
