const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_useEngineContext = require("../../../../hooks/useEngineContext.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let _ant_design_icons = require("@ant-design/icons");
let _pisell_materials_src_components_dataSourceComponents_fields_Upload_shopUploadServe = require("@pisell/materials/src/components/dataSourceComponents/fields/Upload/shopUploadServe");
let _pisell_materials_src_components_table_utils = require("@pisell/materials/src/components/table/utils");
//#region src/plus/walletEditor/components/ImageUpload/index.tsx
const getBaseTime = (size) => {
	const MB = 1024 * 1024;
	if (size < MB) return 1e3;
	if (size < 5 * MB) return 2e3;
	if (size < 20 * MB) return 4e3;
	return 8e3;
};
const ImageUpload = ({ value, onChange }) => {
	var _context$appHelper;
	const context = require_useEngineContext.default();
	_pisell_materials_src_components_table_utils.request.setRequest((_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.request);
	const [uploading, setUploading] = (0, react.useState)(false);
	const [fileList, setFileList] = (0, react.useState)([]);
	const [previewImage, setPreviewImage] = (0, react.useState)("");
	const [previewOpen, setPreviewOpen] = (0, react.useState)(false);
	(0, react.useEffect)(() => {
		if (value) setFileList([{
			uid: "image-0",
			name: "image",
			status: "done",
			url: value
		}]);
		else setFileList([]);
	}, [value]);
	const beforeUpload = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (file) {
			if (!file.type.startsWith("image/")) return antd.Upload.LIST_IGNORE;
			return file;
		});
		return function beforeUpload(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	const handleUpload = (options) => {
		const { onSuccess, onError, onProgress, file } = options;
		const fileSize = file.size;
		const baseTime = getBaseTime(fileSize);
		let progress = 0;
		setTimeout(() => {
			onProgress === null || onProgress === 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 += .5;
			if (progress >= 90) clearInterval(timer);
			onProgress === null || onProgress === void 0 || onProgress({ percent: Math.min(90, progress) });
		}, baseTime / 30);
		(0, _pisell_materials_src_components_dataSourceComponents_fields_Upload_shopUploadServe.uploadFile)(file).then((res) => {
			clearInterval(timer);
			onProgress === null || onProgress === void 0 || onProgress({ percent: 95 });
			setTimeout(() => {
				onProgress === null || onProgress === void 0 || onProgress({ percent: 100 });
				onSuccess === null || onSuccess === void 0 || onSuccess(res);
			}, 200);
		}).catch((err) => {
			clearInterval(timer);
			onError === null || onError === void 0 || onError(err);
		});
	};
	const onRemove = () => {
		setFileList([]);
		onChange === null || onChange === 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 _file$response$status, _file$response, _file$response3;
		const { file, fileList: newFileList } = info;
		setFileList(newFileList.map((item) => {
			var _item$response;
			return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, { status: item.status === "done" && item.response && !((_item$response = item.response) === null || _item$response === void 0 ? void 0 : _item$response.status) ? "error" : item.status });
		}));
		if (file.status === "uploading") setUploading(true);
		if (file.status === "done" && ((_file$response$status = (_file$response = file.response) === null || _file$response === void 0 ? void 0 : _file$response.status) !== null && _file$response$status !== void 0 ? _file$response$status : true)) {
			var _file$response2;
			setUploading(false);
			const url = ((_file$response2 = file.response) === null || _file$response2 === void 0 || (_file$response2 = _file$response2.data) === null || _file$response2 === void 0 ? void 0 : _file$response2.url) || file.url || "";
			onChange === null || onChange === void 0 || onChange(url);
		}
		if (file.status === "error" || file.response && ((_file$response3 = file.response) === null || _file$response3 === void 0 ? void 0 : _file$response3.status) === false) setUploading(false);
	};
	return /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement(_pisell_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__ */ react.default.createElement("button", {
		style: {
			color: "inherit",
			cursor: "inherit",
			border: 0,
			background: "none"
		},
		type: "button"
	}, /* @__PURE__ */ react.default.createElement(_ant_design_icons.PlusOutlined, null), /* @__PURE__ */ react.default.createElement("div", { style: { marginTop: 8 } }, _pisell_utils.locales.getText("upload")))), previewImage && /* @__PURE__ */ react.default.createElement(antd.Image, {
		wrapperStyle: { display: "none" },
		preview: {
			visible: previewOpen,
			onVisibleChange: (visible) => setPreviewOpen(visible),
			afterOpenChange: (visible) => !visible && setPreviewImage("")
		},
		src: previewImage
	}));
};
//#endregion
exports.default = ImageUpload;
