var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/walletEditor/components/ImageUpload/index.tsx
var ImageUpload_exports = {};
__export(ImageUpload_exports, {
  default: () => ImageUpload_default,
  getBaseTime: () => getBaseTime
});
module.exports = __toCommonJS(ImageUpload_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_icons = require("@ant-design/icons");
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_useEngineContext = __toESM(require("../../../../hooks/useEngineContext"));
var import_shopUploadServe = require("@pisell/materials/src/components/dataSourceComponents/fields/Upload/shopUploadServe");
var import_utils2 = require("@pisell/materials/src/components/table/utils");
var getBaseTime = (size) => {
  const KB = 1024;
  const MB = KB * 1024;
  if (size < MB) return 1e3;
  if (size < 5 * MB) return 2e3;
  if (size < 20 * MB) return 4e3;
  return 8e3;
};
var ImageUpload = ({ value, onChange }) => {
  var _a, _b;
  const context = (0, import_useEngineContext.default)();
  import_utils2.request.setRequest((_b = (_a = context.appHelper) == null ? void 0 : _a.utils) == null ? void 0 : _b.request);
  const [uploading, setUploading] = (0, import_react.useState)(false);
  const [fileList, setFileList] = (0, import_react.useState)([]);
  const [previewImage, setPreviewImage] = (0, import_react.useState)("");
  const [previewOpen, setPreviewOpen] = (0, import_react.useState)(false);
  (0, import_react.useEffect)(() => {
    if (value) {
      setFileList([
        {
          uid: "image-0",
          name: "image",
          status: "done",
          url: value
        }
      ]);
    } else {
      setFileList([]);
    }
  }, [value]);
  const beforeUpload = async (file) => {
    if (!file.type.startsWith("image/")) {
      return import_antd.Upload.LIST_IGNORE;
    }
    return file;
  };
  const handleUpload = (options) => {
    const { onSuccess, onError, onProgress, file } = options;
    const fileSize = file.size;
    const baseTime = getBaseTime(fileSize);
    let progress = 0;
    setTimeout(() => {
      onProgress == null ? void 0 : onProgress({ percent: 1 });
    }, 200);
    const timer = setInterval(() => {
      if (progress < 30) {
        progress += 5;
      } else if (progress < 60) {
        progress += 3;
      } else if (progress < 80) {
        progress += 2;
      } else if (progress < 90) {
        progress += 0.5;
      }
      if (progress >= 90) {
        clearInterval(timer);
      }
      onProgress == null ? void 0 : onProgress({ percent: Math.min(90, progress) });
    }, baseTime / 30);
    (0, import_shopUploadServe.uploadFile)(file).then((res) => {
      clearInterval(timer);
      onProgress == null ? void 0 : onProgress({ percent: 95 });
      setTimeout(() => {
        onProgress == null ? void 0 : onProgress({ percent: 100 });
        onSuccess == null ? void 0 : onSuccess(res);
      }, 200);
    }).catch((err) => {
      clearInterval(timer);
      onError == null ? void 0 : onError(err);
    });
  };
  const onRemove = () => {
    setFileList([]);
    onChange == null ? void 0 : onChange("");
    setPreviewImage("");
    setPreviewOpen(false);
  };
  const handlePreview = (file) => {
    const url = file.url || file.thumbUrl || "";
    if (!url) return;
    setPreviewImage(url);
    setPreviewOpen(true);
  };
  const handleChange = (info) => {
    var _a2, _b2, _c, _d;
    const { file, fileList: newFileList } = info;
    const updated = newFileList.map((item) => {
      var _a3;
      return {
        ...item,
        status: item.status === "done" && item.response && !((_a3 = item.response) == null ? void 0 : _a3.status) ? "error" : item.status
      };
    });
    setFileList(updated);
    if (file.status === "uploading") {
      setUploading(true);
    }
    if (file.status === "done" && (((_a2 = file.response) == null ? void 0 : _a2.status) ?? true)) {
      setUploading(false);
      const url = ((_c = (_b2 = file.response) == null ? void 0 : _b2.data) == null ? void 0 : _c.url) || file.url || "";
      onChange == null ? void 0 : onChange(url);
    }
    if (file.status === "error" || file.response && ((_d = file.response) == null ? void 0 : _d.status) === false) {
      setUploading(false);
    }
  };
  return /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellUpload,
    {
      uploadType: "custom",
      listType: "picture-card",
      multiple: false,
      maxCount: 1,
      accept: "image/*",
      beforeUpload,
      onChange: handleChange,
      showUploadList: {
        showRemoveIcon: true,
        showPreviewIcon: true,
        showDownloadIcon: false
      },
      fileList,
      onRemove,
      onPreview: handlePreview,
      customRequest: handleUpload
    },
    fileList.length >= 1 || uploading ? null : /* @__PURE__ */ import_react.default.createElement(
      "button",
      {
        style: {
          color: "inherit",
          cursor: "inherit",
          border: 0,
          background: "none"
        },
        type: "button"
      },
      /* @__PURE__ */ import_react.default.createElement(import_icons.PlusOutlined, null),
      /* @__PURE__ */ import_react.default.createElement("div", { style: { marginTop: 8 } }, import_utils.locales.getText("upload"))
    )
  ), previewImage && /* @__PURE__ */ import_react.default.createElement(
    import_antd.Image,
    {
      wrapperStyle: { display: "none" },
      preview: {
        visible: previewOpen,
        onVisibleChange: (visible) => setPreviewOpen(visible),
        afterOpenChange: (visible) => !visible && setPreviewImage("")
      },
      src: previewImage
    }
  ));
};
var ImageUpload_default = ImageUpload;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getBaseTime
});
