import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import useEngineContext from "../../../../hooks/useEngineContext.js";
import React, { useEffect, useState } from "react";
import { Image, Upload } from "antd";
import { locales } from "@pisell/utils";
import { PisellUpload } from "@pisell/materials";
import { PlusOutlined } from "@ant-design/icons";
import { uploadFile } from "@pisell/materials/src/components/dataSourceComponents/fields/Upload/shopUploadServe";
import { request } from "@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 = useEngineContext();
	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] = useState(false);
	const [fileList, setFileList] = useState([]);
	const [previewImage, setPreviewImage] = useState("");
	const [previewOpen, setPreviewOpen] = useState(false);
	useEffect(() => {
		if (value) setFileList([{
			uid: "image-0",
			name: "image",
			status: "done",
			url: value
		}]);
		else setFileList([]);
	}, [value]);
	const beforeUpload = function() {
		var _ref = _asyncToGenerator(function* (file) {
			if (!file.type.startsWith("image/")) return 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);
		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 _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.createElement("div", null, /* @__PURE__ */ React.createElement(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.createElement("button", {
		style: {
			color: "inherit",
			cursor: "inherit",
			border: 0,
			background: "none"
		},
		type: "button"
	}, /* @__PURE__ */ React.createElement(PlusOutlined, null), /* @__PURE__ */ React.createElement("div", { style: { marginTop: 8 } }, locales.getText("upload")))), previewImage && /* @__PURE__ */ React.createElement(Image, {
		wrapperStyle: { display: "none" },
		preview: {
			visible: previewOpen,
			onVisibleChange: (visible) => setPreviewOpen(visible),
			afterOpenChange: (visible) => !visible && setPreviewImage("")
		},
		src: previewImage
	}));
};
//#endregion
export { ImageUpload as default };
